Wednesday, May 13, 2009

Turn the submit button off when it press in ASP.Net

Sometime, we need to turn the button off after user clicking to prevent the double actions. Let's say, when I developed 2C2P payment processor,  I have to disable the "Confirm" button to avoid the double payment transactions when user click the button once. Otherwise, we will have the complaint from user about duplicated transactions when user might click the button double times since page loading is slow (behind scene, it's processing). The following example show you that it turn the submit button off and show message when it press using server-side script.

Disable button after clicking

Here is button control to add <form> tag.
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" Width="97px" />

Put the following code to code-behind as below.
protected void Page_Load(object sender, EventArgs e)
{
            this.Button1.Attributes.Add("onclick", "this.value='Please wait...';this.disabled = true;"
                + Page.ClientScript.GetPostBackEventReference(this.Button1, ""));
}
protected void Button1_Click(object sender, EventArgs e)
{
            //Here go on for click event
}

Now, click the button every times, it disable itself and user don't have a chance to press the button double.


No comments:

Post a Comment