RSS

Disable Button before Page PostBack in ASP.Net

08 Aug

In this article I will explain how to disable ASP.Net button control before Page PostBack is done or the form is submitted so that user can’t click the button again.

And Also change the text of  button

To start with I have created a simple ASP.Net Web Page with  ASP.Net Buttons on it.

 

<form id=”form1″ runat=”server”>

  <asp:Button ID=”Button1″ runat=”server” Text=”Submit” OnClick=”Button1_Clicked” />

<asp:Label id=”lblmessage” Text=”Page PostBack after Processing….” runat=”server” Visible=”false” />
    </form>

 

Now here’s the short JavaScript snippet that will disable the button as soon as it is clicked so that when PostBack occurs the button cannot be clicked again.

 

<script type = “text/javascript”>
function DisableButton() {
    document.getElementById(“<%=Button1.ClientID %>”).disabled = true;
     document.getElementById(“<%=Button1.ClientID %>”).value=”Processing….”;
}
window.onbeforeunload = DisableButton;
</script>

 

Write this code on Code-Behind

 

 protected void Button1_Clicked(object sender, EventArgs e)
    {
        System.Threading.Thread.Sleep(1000);

        lblmessage.Visible = true;
    }

 

OutPut

 

Advertisements
 
2 Comments

Posted by on August 8, 2011 in Java Script

 

Tags: , ,

2 responses to “Disable Button before Page PostBack in ASP.Net

  1. web hosting in pakistan

    January 12, 2012 at 10:49 am

    Thanks for the code,

     
  2. women health and fitness

    September 5, 2012 at 4:44 pm

    Really very helpfull , thanks a lot

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: