* IE6 restrict button click to once. *
I have a button, which should be clicked once, and when clicked at first time I need to run few back-end logics(operations) for example save some data in DB, send Email, do some logging, and after the success of these operations I need to redirect to the other page.
But I am using
btnMakeFO.Visible = false ;
after the button's click event, the user is still able to click it more than twice.
I have also used the:
btnMakeFO.Enabled = false ;
but it is not working too.
I have also used the following at the button's(btnMakeFO_Click) click Event Handler :
btnMakeFO.Attributes.Add("onclick", "this.disabled=true;" + Page.ClientScript.GetPostBackEventReference(btnMakeFO, "").ToString());
I have searched a lot regarding this issue, but I am unable to find the solution customized to my problem.
NOTE: I am developing on the Windows 7 x-64 bit machine and IE8, here the button is clicked at maximum twice. In FireFox8 it is clicked only once.
But at the Production Environment there is Windows 2003 Server, Internet Explorer 6, at this server the button is able to be clicked even more than twice i.e. the btnMakeFO button can be clicked about 10 to 15 times consecutively. (The Internet Explorer 6 at Production Server cannot be upgraded because few older applications will not run correctly in the newer versions of Internet Explorer.)
I need to fix this issue at earliest by tomorrow. Kindly help me in this regards at your earliest.
BTW I have used the following (which I found from Code Project), it is working greatly and I am happy with this solution.
<asp:Button OnClick="btnMakeFO_Click" OnClientClick="clickOnce(this, 'ABC...')">
<script type="text/javascript">
function clickOnce(btn, msg)
{
// Test if we are doing a validation
if (typeof(Page_ClientValidate) == 'function')
{
// if we are doing a validation, return if it's false
if (Page_ClientValidate() == false)
{
return false;
}
}
// Ensure that the button is not of type "submit"
if (btn.getAttribute('type') == 'button')
{
// The msg attibute is absolutely optional
// msg will be the text of the button while it's disabled
if (!msg || (msg='undefined'))
{
msg = 'Saving.......';
}
btn.value = msg;
// The magic :D
btn.disabled = true;
}
return true;
}
</script>
The javascript approach should work.
You are injecting the code from the server-side, which can be challenging. You should still be able to see the results when you run the app and View Source in the browser. You should see the javascript in the tag, like this:
<input type="submit" value="Submit" onclick="this.disabled=true" />
For ASP.Net, you might also try the OnClientClick event, which you can add directly to the HTML, instead of injecting it from your page code-behind.
Related
I have a content management page on my CMS on which I have a popup that warns the user on page close. It is a very simple warning implemented like so:
window.onbeforeunload = function (e) {
return 'Are you sure you want to close?';
}
It works, but the problem is that the popup is shown even when the user clicks the same button. I am very new to document events so I have no idea how to do this, but I want the popup to only show when the page is being closed and not when it is being posted back.
Also I have not used JQuery in my website so please don't suggest it; I don't want to use a whole framework just for a popup.
.
Thanks.
I was able to get it to work by using a global variable, like so:
var check = true;
Which is checked for when showing the popup:
window.onbeforeunload = function (e) {
if (check) {
return 'Are you sure you want to close?';
}
}
Then you simply set check to false when you click a button that does a postback. Hope this helps someone.
Does anybody know how to open an URL in a new tab from C# code?
I have tried with
Response.Write("<script type='text/javascript'>window.location.href('../Documents/doc.pdf','_blank'); </script>");
Response.Write("<script type='text/javascript'>window.open('../Documents/doc.pdf','_blank'); </script>");
Response.Write("$('#pageContent_Send').click();");
with
$("#pageContent_Send").click(function () {
window.open("../Documents/doc.pdf");
return false;
});
and it did not work, using "window.open" I get an "Pop-up Blocker" browser warning.
There are a few options for opening a new tab that won't get blocked.
You can have it open the URL with an anchor click, like so click me
You can submit a form to a blank target, giving a similar result, but from a form. This is useful if you need to post, but can also be useful for get. Like so <form method="get" action="<your destination>" target="_blank"><button type="submit">Click Me</button></form>
You can use JS with window.open but it has to be tied to an active click event. I see in your original post that this got blocked, but if you're triggering it on a click event directly (i.e. not using a setTimeout or an async call or something else that delays the window opening) then this should work in all browsers. Since you're trying to "fake" it by forcing a click, the browser is going to block this every time unless you explicitly allow it.
<a href='www.xyz.com' target='_blank'>Click me to open me in new tab</a>
I have a web page that contains a textbox and a submit button. When the user edits the text in the textbox and clicks another link (not the submit button) how do I display the 'Are you sure you want to navigate away from this page' popup message?
I have researched this on the net and found a few javascript examples. Is this the only way you can do this? If not, what is the best way to do it?
This is one of the multiple ways to achieve the same thing
function goodbye(e) {
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog
//e.stopPropagation works in Firefox.
if (e.stopPropagation) {
e.stopPropagation();
e.preventDefault();
}
}
window.onbeforeunload=goodbye;
got it from here open js
Only the unload() event will work on JS. You can't manage it on the server.
Check out the answer to this other question on SO, it is very similar to your question
How to show the "Are you sure you want to navigate away from this page?" when changes committed?
Simple solution
window.onbeforeunload = confirmExit;
function confirmExit() {
return "Are you sure you want to leave this page?";
}
4guysFromRolla.com - Prompting a user to Save when Leaving a Page
You cannot use the onbeforeunload window method as it gets triggered by multiple ways like back and forth browser navigation links, refreshing the page, closing of the page, clicking on the links.
What i feel you have to bind the link tag for which you want display the navigation away message and then use the function for the status message display
window.addEvent('domready',function(){
$$('a').addEvent('click', function(e) {
//leaving(); function u wrote for displaying message
});
});
function leaving(e) {
if(!e)
e = window.event;
// return code for the displaying message
}
If you want to do this in a way that guarantees it will work on almost all browsers, use the JQuery library. The following describes the unload event.
http://www.w3schools.com/jquery/event_unload.asp
It's exactly for purposes like yours.
Just to elaborate a little, you would have to download the jquery js library and reference it in your project/page, but you'll probably want to do that eventually anyway.
If you want to control this from the server side, you can dynamically emit the jquery call in the OnPreRender.
Look into Jquery's .beforeunload property. Here is an example:
$(window).bind('beforeunload', function(){ return 'Click OK to exit'; });
Please note, beforeunload canot prevent a page from unloading or redirect it to another page for obvious reasons; it would be too easy to abuse. Also if you just want to run a function before unloading, try the following:
$(window).unload(function(){ alert('Bye.'); });
Finally, don't forget to referrence jQuery in your head tag by using:
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
The above gets you the latest version from the internet and saves you the trouble to download it, and of course you can do so optionally, but I am just trying to get your thing to work asap.
Oh, I also found an example for you. Click here to see a page that calls a function before it closes. Hope this helps bud.
I was able to get this to work with Andrei G's answer. I would add on that to get it to work in Chrome, add this to the end of his goodbye function:
return "";
I'm using DevExpress ASPxButton, and I was wondering how can I prevent the user from clicking the button more then once?
Basically it does an update/insert statement.
This can be done using the following code:
var buttonClicked = false;
function MyBtnClick(s,e){
if(buttonClicked) return;
buttonClicked = true;
// do something
}
<ASPxButton id="btn" runat="server">
<ClientSideEvents Click="function(s,e) { MyBtnClick(s,e); } "/>
</ASPxButton>
On the client side, you can disable it when it's clicked.
On the server side, my favourite approach is generally to send a nonce with the form data, and then reject duplicate nonces.
As always, client side validates in a fast and (hopefully) friendly way, server side validates for sure even if client side code suffers from some unexpected browser issue, or someone is deliberately messing things up.
Not sure about aspxbutton specifically but in general you will have something like this on the onclick attribute of the control.
onclick="this.disabled=true;"
In the code where you run the update query simply add to the start of the code:
ButtonNameOrID.Enabled = False // add ; to end if C#
Then at the end of the code, add:
ButtonNameOrID.Enabled = True // add ; to end if C#
This is will disable/enable the button from your C# or VB code.
The __doPostBack is not working in firefox 3 (have not checked 2). Everything is working great in IE 6&7 and it even works in Chrome??
It's a simple asp:LinkButton with an OnClick event
<asp:LinkButton ID="DeleteAllPicturesLinkButton" Enabled="False" OnClientClick="javascript:return confirm('Are you sure you want to delete all pictures? \n This action cannot be undone.');" OnClick="DeletePictureLinkButton_Click" CommandName="DeleteAll" CssClass="button" runat="server">
The javascript confirm is firing so I know the javascript is working, it's specirically the __doPostBack event. There is a lot more going on on the page, just didn't know if it's work it to post the entire page.
I enable the control on the page load event.
Any ideas?
I hope this is the correct way to do this, but I found the answer. I figured I'd put it up here rather then in a stackoverflow "answer"
Seems it had something to do with nesting ajax toolkit UpdatePanel. When I removed the top level panel it was fixed.
Hope this helps if anyone else has the same problem. I still don't know what specifically was causing the problem, but that was the solution for me.
Check your User Agent string. This same thing happened to me one time and I realized it was because I was testing out some pages as "googlebot". The JavaScript that is generated depends on knowing what the user agent is.
From http://support.mozilla.com/tiki-view_forum_thread.php?locale=tr&comments_parentId=160492&forumId=1:
To reset your user agent string type about:config into the location bar and press enter. This brings up a list of preferences. Enter general.useragent into the filter box, this should show a few preferences (probably 4 of them). If any have the status user set, right-click on the preference and choose Reset
I had this same problem (__doPostBack not working) in Firefox- caused a solid hour of wasted time. The problem turned out to be the HTML. If you use HTML like this:
<input type="button" id="yourButton" onclick="doSomethingThenPostBack();" value="Post" />
Where "doSomethingThenPostBack" is just a JavaScript method that calls __doPostBack, the form will not post in Firefox. It will PostBack in IE and Chrome. To solve the problem, make sure your HTML is:
<input type="submit" id="yourButton" ...
The key is the type attribute. It must be "submit" in Firefox for __doPostBack to work. Other browsers don't seem to care. Hope this helps anyone else who hits this problem.
this might seem elemental, but did you verify that your firefox settings aren't set to interfere with the postback? Sometimes I encounter similar problems due to a odd browser configuration I had from a debugging session.
Is it because you are doing return confirm? seems like the return statement should prevent the rest of the code from firing. i would think an if statement would work
if (!confirm(...)) { return false; } _doPostBack(...);
Can you post all the js code in the OnClick of the link?
EDIT: aha, forgot that link button emits code like this
<a href="javascript:__doPostBack()" onclick="return confirm()" />
Are you handling the PageLoad event? If so, try the following
if (!isPostBack)
{
//do something
}
else if (Request.Form["__EVENTTARGET"].ToLower().IndexOf("myevent") >= 0)
{
//call appropriate function.
}
Check if you are getting a call this way, if so then maybe the event is not wired and nedes to be explicitly called.
what do you expect from "Enabled = 'false'" ?
I have had problems with firebug on some web forms, something to do with the network analyser can screw with postbacks.
With or without the OnClientClick event it still doesn't work.
The _doPostBack event is the auto generated javascript that .NET produces.
function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
*The &95; are underscores, seems to be a problem with the stackoverflow code block format.
Now that i think about it, as noted in my last edit, you want to drop the javascript: in the on client click property. It's not needed, because the onclick event is javascript as it is. try that, see if that works.
Seems it had something to do with nesting ajax toolkit UpdatePanel. When I removed the top level panel it was fixed.
Hope this helps if anyone else has the same problem.
I had this exact same issue in a web app I was working on, and I tried solving it for hours.
Eventually, I did a NEW webform, dropped a linkbutton in it, and it worked perfectly!
I then noticed the following issue:
...
I switch the order to the following, and it immediately was fixed:
...
IE had no issue either way (that I noticed anyway).
I had a similar issue. It turned out that Akamai was modifying the user-agent string because an setting was being applied that was not needed.
This meant that some .NET controls did not render __doPostBack code properly. This issue has been blogged here.
#Terrapin: you got this exactly right (for me, anyways).
I was running User Agent Switcher and had inadvertently left the Googlebot 2.1 agent selected.
In Reporting Services 2008 it was causing the iframes that reports are actually rendered in to be about 300x200 px, and in Reporting Services 2008 R2 is was throwing "__doPostBack undefined" errors in the Error Console.
Switching back to the Default User Agent fixed all my issues.
I had the same problem with Firefox. Instead of using __doPostBack() could you use the jQuery .trigger() method to trigger a click action on an element that has your postback event registered as the click action?
For example, if you had this element in your aspx page:
<asp:Button runat="server" ID="btnMyPostback" OnClick="btnMyPostback_Click" CssClass="hide" ToolTip="Click here to submit this transaction." />
And your postback event in your controller:
protected void btnMyPostback_Click(object sender, EventArgs e)
{
//do my postback stuff
}
You could do the postback by calling:
$("#btnMyPostback").trigger("click");
This will cause the Page_Load event to fire if you need to do something on Page_Load.