Disable Submit button in case of multiple click.. (C#)

后端 未结 6 895
一向
一向 2021-01-01 05:55

My Problem is ,I have a simple web form, which contains two textboxes and a button.there are some asp.net validator controls on page.so i want client side disabling of butto

6条回答
  •  旧巷少年郎
    2021-01-01 06:11

    This is the correct and simple way to do this:

    Create a helper method in your application (say in a Utlity Namespace):

        Public Shared Sub PreventMultipleClicks(ByRef button As System.Web.UI.WebControls.Button)
            button.Attributes.Add("onclick", "this.disabled=true;" & button.Page.ClientScript.GetPostBackEventReference(button, String.Empty).ToString)
        End Sub
    

    Now from the code behind of each of your web pages you can simply call:

        Utility.PreventMultipleClicks(button1)
    

    where button1 is the the button you want to prevent multiple clicks.

    What this does is simply sets the on click handler to: this.disabled=true

    and then appends the buttons own post back handler, so we get:

    onclick="this.disabled=true";__doPostBack('ID$ID','');"
    

    This does not break the default behaviour of the page and works in all browsers as expected.

    Enjoy!

提交回复
热议问题