RSS

Tag Archives: how to change a button’s text in javascript.

Disable Button before Page PostBack in ASP.Net


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

 

 
2 Comments

Posted by on August 8, 2011 in Java Script

 

Tags: , ,