How to disable the button after the 1st click

I have the following onclick event for a button. after this event, I want to disable the button. Can someone help me figure out how to do this?

Here is the code that I execute on the buttonclick event.

protected void Button3_Click(object sender, EventArgs e) { if (Session["login"] != null && Session["db"] != null) { digit b = new digit(); String digitformed = b.formdigit(this.DropDownList1, this.TextBox1, this.TextBox2); chekcount c = new chekcount(); int count = c.getmaxcountforjud_no(digitformed); int addtocount = count + 1; String name = Session["login"].ToString(); String databs = Session["db"].ToString(); String complex_name = name + databs; if (DropDownList2.SelectedItem.Text != "New") { update u = new update(); u.update1(this.Editor1, digitformed, this.TextBox3, complex_name, name, this.DropDownList2); Response.Write(@"<script language='javascript'>alert('Updated')</script>"); } else { save d = new save(); d.dosave(this.Editor1, addtocount, digitformed, this.TextBox3, complex_name, name); Response.Write(@"<script language='javascript'>alert('Saved')</script>"); } } else { Response.Redirect("log.aspx"); } } 

Here is the button I want to disable.

 <asp:Button ID="Button3" runat="server" Text="Save" onclick="Button3_Click" Visible="False" /> 
+7
source share
8 answers

You tried?:

 protected void Button3_Click(object sender, EventArgs e) { Button3.Enabled = false; //rest of code } 
+12
source

Use the OnClientClick and UseSubmitBehavior properties to control the button.

 <asp:Button runat="server" ID="BtnSubmit" OnClientClick="this.disabled = true; this.value = 'Submit in progress...';" UseSubmitBehavior="false" OnClick="BtnSubmit_Click" Text="Click to Submit" /> 

OnClientClick allows you to add the client part of the OnClick script. In this case, JavaScript will disable the button element and change its text value to a progress message. After completion of the postback, the newly created page will return the button to its original state without additional work.

The only error that comes with disabling the submit button on the client side is that it cancels the sending of browsers and thus the postback. Setting the UseSubmitBehavior property to false tells .NET to insert the necessary client script to run postback anyway, rather than relying on browser form presentation behavior. In this case, the code that he enters will be as follows:

 __doPostBack('BtnSubmit','') 

Repeated HTML:

 <input type="button" name="BtnSubmit" onclick="this.disabled = true; this.value = 'Submitting...';__doPostBack('BtnSubmit','')" value="Submit Me!" id="BtnSubmit" /> 

This should give you the desired behavior.

From: http://encosia.com/disable-a-button-control-during-postback/ Credit: Dave Ward (Twitter: @Encosia)

+28
source

You will need to set the Enabled property of the button in the server-side code, as described by other posters. However, if you are trying to prevent multiple shipments from the same button, you will need a slightly different one.

Add a method to your class:

 static void DisableButtonDuringPostback(Page page, Button control) { StringBuilder sb = new StringBuilder(); sb.Append("this.disabled = true;"); sb.Append(page.ClientScript.GetPostBackEventReference(control, control.ID.ToString())); sb.Append(";"); control.Attributes.Add("onclick", sb.ToString()); } 

In Page_Load add

 DisableButtonDuringPostback(this.Page, Button3); 
+5
source
 <asp:Button onclick="Button3_Click" ID="Button3" runat="server" Text="Save" OnClientClick="this.disabled = true; this.value = 'please wait ..';" UseSubmitBehavior="false" /> 
+4
source

See tagged code

 protected void Button3_Click(object sender, EventArgs e) { ** if (Session["Clicked"] == null) Session["Clicked"] = true; else { Button3.Enabled = false; return; } ** if (Session["login"] != null && Session["db"] != null) { digit b = new digit(); String digitformed = b.formdigit(this.DropDownList1, this.TextBox1, this.TextBox2); chekcount c = new chekcount(); int count = c.getmaxcountforjud_no(digitformed); int addtocount = count + 1; String name = Session["login"].ToString(); String databs = Session["db"].ToString(); String complex_name = name + databs; if (DropDownList2.SelectedItem.Text != "New") { update u = new update(); u.update1(this.Editor1, digitformed, this.TextBox3, complex_name, name, this.DropDownList2); Response.Write(@"<script language='javascript'>alert('Updated')</script>"); } else { save d = new save(); d.dosave(this.Editor1, addtocount, digitformed, this.TextBox3, complex_name, name); Response.Write(@"<script language='javascript'>alert('Saved')</script>"); } } else { Response.Redirect("log.aspx"); } } 
+2
source
  ///count number of button click var counter = 0; function countclickbutton() { counter++; if (counter > 1) { alert("proessing..please wait."); //do not allow to again click return false; } else { return true; } } 

calling this onClientClick button

+2
source

if you want to do it using jquery than

 $('#button3').attr("disabled", true); 
+1
source

This is an old post, however I do not think that he fully answered. First, in ASP.NET WebForms, you send an HTTP GET to a web server that processes your request and outputs client-side HTML code to render the browser.

When you interact with a server control, the values ​​are contained in a hidden VIEWSTATE input VIEWSTATE for properties (such as a boolean value for Enabled ).

When you click the button, it sends an HTTP POST request to the web server on the same page. This is why the Page_Load event is fired when a button is clicked.

After the HTTP POST request has been processed, it will then return the HTML code to re-render the browser. For this reason, if you have the following code in the Page_Load event:

 if (Page.IsPostBack) { Button3.Enabled = false; } 

It will not show the user that it has been disconnected until the HTTP POST request has been processed and the updated client-side code has been returned.

From the initial question, it turned out that the server received a few seconds to return the answer, so when you clicked the button again, it would be possible to trigger several postback events. A simple (but annoying) way to solve your problem would be to have a regular HTML button that performs a function in JavaScript, which disables it and fires the onclick event on the server side. Then the problem with this would be that when the HTTP POST request returns a response, it would display the regular HTML button as being included. To solve this problem, you can simply disable it in JavaScript using the built-in ASP.NET code. Here is an example:

.Aspx file

 <button id="clientButton" onclick="javascript:update();" /> <asp:Button ID="serverButton" OnClick="serverButton_OnClick" runat="server" /> <script type="text/javascript"> <% if (Page.IsPostBack) { %> document.getElementById("clientButton").enabled = false; <% } %> function update() { document.getElementById("clientButton").enabled = false; document.getElementById("<%= serverButton.ClientID %>").click(); } </script> <style type="text/css"> #<%= serverButton.ClientID %> { visibility: hidden; display: none; } </style> 



.ASPX.CS File

 protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) { // Triggered when serverButton has 'javascript:click()' triggered } } 
0
source

All Articles