Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

How to work around the "double-click" problem (See related posts)

This is a generic method to avoid "double-click" effect where a user click many times to validate. The button is hidden when the user clicks it. In order to not update all forms, the trick is applied to the Render event of the page.
protected override void Render(HtmlTextWriter output) {
    // Get normal html ouput
    StringBuilder stringBuilder = new StringBuilder();
    StringWriter stringWriter = new StringWriter(stringBuilder);
    HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
    base.Render(htmlWriter);
    string html = stringBuilder.ToString();
    // Enhance submit buttons
    string onclick1 = "\"if (typeof(Page_ClientValidate) == 'function') Page_ClientValidate();";
    string onclick2 = "\"this.style.display='none';";
    html = html.Replace("onclick=" + onclick1, "onclick=" + onclick2 + onclick1.Substring(1));
    // Render updated html
    output.Write(html);
}

You need to create an account or log in to post comments to this site.


Click here to browse all 5143 code snippets

Related Posts