 |
|
 |
I tried in asp.net using c#. but its not working for me. Its not calling the button click event.
My aspx file is
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TestPage.aspx.cs" Inherits="TestPage" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
<script language="javascript">
<!--
function DisableButton() {
document.forms[0].submit();
window.setTimeout("disableButton('" + window.event.srcElement.id + "')", 0);
}
function disableButton(buttonID) {
document.getElementById(buttonID).disabled=true;
}
</script><pre><pre lang="HTML">
</head>
<body>
<form id="form1" runat="server" method="post">
<div>
<asp:Button ID="btnSave" Text="Save" Runat="server" OnClick="btnSave_Click">
</form>
</body>
</html>
.aspx.cs file is
public partial class TestPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
btnSave.Attributes.Add("onclick", "DisableButton()");
}
protected void btnSave_Click(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(5000);
Response.Write("Page has been saved.");
}
}
|
|
|
|
 |
|
 |
The post is an older one, but this is a way to handle it in the code behind.
BtnSearch.Attributes.Add("onclick", "javascript:"
+ BtnSearch.ClientID + ".disabled=true;"
+ Page.ClientScript.GetPostBackEventReference(BtnSearch, null));
|
|
|
|
 |
|
|
 |
|
|
 |
|
 |
This worked great and was easy to implement.
|
|
|
|
 |
|
|
 |
|
 |
This does indeed work. Our problem is that sometimes in our system, a process never completes so the button never becomes available again. This is because of flaws in our system. The point is, you might run into problems if there is a risk of some process or postback not completing. Be aware that the button will remain disabled if this happens.
|
|
|
|
 |
|
 |
Although the idea is good and perfectly valid with a "clean" HTML solution, it is not really compatible with ASP.NET. I'll try to explain why.
If you debug the flow of events using Fiddler, you'll see that there will be two post to the server (even if you only press the button once), of which the first one will be cancelled because the second one follows almost directly after the first one. Why is this?
ASP.NET automagically appends a call to the DoPostBack JS method on a button which means that the "real" onclick handler will perform the following steps:
1) Execute the DisableButton method
2) Submit the form (invoked from DisableButton)
3) Execute the built-in DoPostBack function, which in turn tries to submit the form
So the form is first submitted in DisableButton, and shortly after also in DoPostBack (which will cancel the first submission).
The user will probably not notice any of this, and to him/her the form will submit as intended, which is why this technique seems to work
I don't have a solution to this, other than using another approach which includes "locking" the entire form with a layer (which has been referred to previously).
|
|
|
|
 |
|
 |
i tried it works on IE but not on fire fox. i really need to fix this issue.
any help greatly appreciated.
|
|
|
|
 |
|
 |
It may be the following line of the script:
window.event.srcElement.id + "')", 0);
My understanding is that event.srcElement is an IE-only term. The Firefox equivalent would be e.target. You can do a check that would be something like the following that I just found:
if (window.event) {
window.event.srcElement.style.backgroundColor="Blue"
}
else {
e.target.style.background.colo="Blue"
}
|
|
|
|
 |
|
 |
<script language="javascript" type="text/javascript">
<!--
function DisableButton(form) {
document.forms[0].submit();
window.setTimeout("disableButton('" +
form.btnSubmit.id + "')", 0);
}
function disableButton(buttonID) {
document.getElementById(buttonID).disabled=true;
}
</script>
and
btnSubmit.Attributes.Add("onclick", "DisableButton(this.form)");
should do it
|
|
|
|
 |
|
 |
this works for me but adding the button attributes bypasses my other validations. i.e. i have a requiredfieldvalidator in my page, even if the textbox being validated is empty, the button still causes postback
|
|
|
|
 |
|
 |
Hi,
Great work!.
it helps me lot, works fine for Internet explorer, but when i am using firefox it's not working. can u help me?
|
|
|
|
 |
|
 |
Hey man thanks for this. I've wasted a lot of time trying to make my code work. Good thing i found this site. Thanks again.
|
|
|
|
 |
|
 |
your code works fine for Internet explorer, but when i am using firefox it's not working .Any suggestions on your part will greatly appreciated .
|
|
|
|
 |
|
 |
I've been racking my brain trying to do this for days with long complicated solutions. How simple and easy. Thanks!!
|
|
|
|
 |
|
 |
Indeed incredible, thnx for sharing this with us!
|
|
|
|
 |
|
 |
this article is very much useful. many thanks to you.
|
|
|
|
 |
|
|
 |
|
|
 |
|
 |
I wrote a little piece that not only disabled the button that was clicked but also disables every other button on the page. That prevents someone from doing something like clicking "OK" and then "Cancel", which would execute the "OK" event without them really knowing it.
The piece even went so far as to disable textboxes and pulldowns to prevent autopostback events in those controls. Point is once a postback starts, the user shouldn't be able to cause another until the page reloads.
Also, this brings up an interesting issue regarding client callbacks in 2.0. If the button causes a callback and not a postback and you decide to disable said button, be sure the button gets re-enabled after the callback!
Steve
|
|
|
|
 |
|
 |
I just added the following code to the onclick event:
onclick="this.disabled=true; __doPostBack('ButtonName',''); return false;"
Often the __doPostBack function is inserted into your page by .NET. If not, put the code in manually, or add a dummy LinkButton and set it's style to be "display:none".
No server side C# required...
This trick is also useful if you want to fake a button click from javascript. Eg if the user presses enter in a text box...
eg:
onKeyPress="if (event.keyCode==13) { __doPostBack('SearchButton',''); return false; }"
|
|
|
|
 |
|
 |
I had an idea that is alot simpler - havent had time to test it out yet but I think it should work just fine
Basically what I was thinking, was just to hook up a javascript function to the button's clientside onclick event - and inside that function you set the buttons clientside onclick event to "return false;" - effectively stopping it from posting the form again on any consecutive clicks
|
|
|
|
 |
|
|
 |
|
|
 |