ASP.Net double-click problem

前端 未结 6 1830
孤城傲影
孤城傲影 2020-12-02 17:27

having a slight problem with an ASP.net page of mine. If a user were to double click on a \"submit\" button it will write to the database twice (i.e. carry out the \'onclick

6条回答
  •  时光说笑
    2020-12-02 18:01

    If you have validation on the page, disabling the button client side gets a little tricky. If validation fails, you don't want to disable the button. Here's a snippet that adds the client side event handler:

    private void BuildClickOnceButton(WebControl ctl)
    
    {
    
    System.Text.StringBuilder sbValid = new System.Text.StringBuilder();
    
    sbValid.Append("if (typeof(Page_ClientValidate) == 'function') { ");
    
    sbValid.Append("if (Page_ClientValidate() == false) { return false; }} ");
    
    sbValid.Append(ctl.ClientID + ".value = 'Please wait...';");
    
    sbValid.Append(ctl.ClientID + ".disabled = true;");
    
    // GetPostBackEventReference obtains a reference to a client-side script 
    // function that causes the server to post back to the page.
    
    sbValid.Append(ClientScript.GetPostBackEventReference(ctl, ""));
    
    sbValid.Append(";");
    
    ctl.Attributes.Add("onclick", sbValid.ToString());
    
    }
    

    See this asp.net thread for more info.

    Update: the above code would be used to add the OnClientClick handler in code behind. You could also write the javascript in your aspx markup like so:

    
    
    
    

提交回复
热议问题