Click here to Skip to main content
6,594,932 members and growing! (15,977 online)
Email Password   helpLost your password?
Web Development » ASP.NET » General     Intermediate

One Click Button Control

By Alexander Kleshchevnikov

Some users like to click twice or more on submit button especially if postback does not response immediately. This scenario can bring the problem on the server in case, for instance, if the “first click” already disposed some resources. In this article I am going to discuss one of the solution.
Javascript, CSS, HTML, C# 2.0, Windows, .NET 2.0, ASP.NET, VS2005, Dev
Posted:9 Oct 2006
Views:25,503
Bookmarked:30 times
Unedited contribution
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
9 votes for this article.
Popularity: 3.86 Rating: 4.04 out of 5
1 vote, 11.1%
1
1 vote, 11.1%
2

3
1 vote, 11.1%
4
6 votes, 66.7%
5

Introduction

Some users like to click twice or more on submit button especially if postback does not response immediately. This scenario can bring the problem on the server in case, for instance, if the "first click" already disposed some resources in session state or view state. So the next click will cause the exception. With more problems user could be faced if submit button runs some financial transactions. In this case system will do more than user expected.

One of the solution is hiding the button after the first click and give a system to complete the post back. The simple example in JavaScript:

<INPUT onclick="this.disabled = true" type=submit value=Submit />

It works fine for simple html form without ASP.NET whereas implementing this approach on ASP.NET page will enforce us to consider the ability to assign "hiding code" to onclick JavaScript event and run this code in case ASP.NET validation process on the client side is completed successfully. Because if validation on the client side failed form does not post back to the server and we do not need to hide the button yet.

You may say that ASP.NET makes client side validation itself and we do not need to check it manually but we need to do this before to make decision whether we need to make postback or not. This small block of code checks if client side validation is completed successfully:

if (typeof(Page_ClientValidate) == 'function') {
    if (Page_ClientValidate() == false){
        return false;
    }
}

After that we can hide button and even change button value, for instance, "Please wait�" to inform user that page is posting back to the server.

this.value = 'Please wait...';
this.disabled = true;

To combine all this functionality in one control we will use Button control as a base:

public class OneClickButton : Button
{
    private string replaceTitleTo;
    public string ReplaceTitleTo
    {
        get { return this.replaceTitleTo; }
        set { this.replaceTitleTo = value; }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        StringBuilder script = new StringBuilder();
        script.Append("if (typeof(Page_ClientValidate) == 'function') { ");
        script.Append("if (Page_ClientValidate() == false) { return false; }} ");
        if (!String.IsNullOrEmpty(this.replaceTitleTo))
        {
            script.Append("this.value = '");
            script.Append(this.replaceTitleTo);
            script.Append("';");
        }
        script.Append("this.disabled = true;");
        script.Append(this.Page.GetPostBackEventReference(this));
        script.Append(";");
        this.Attributes.Add("onclick", script.ToString());
    }
}

I added property ReplaceTitleTo and check if it is not empty. If yes, I replace value of button to change button title. For example, one I can replace Button control with this one:

<uc:OneClickButton id="butSubmit" ReplaceTitleTo="Please wait..." runat="server" />

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Alexander Kleshchevnikov


Member
Alexander is freelance web developer with 4 years experience. He has skills in ASP.NET/MS SQL Server, PHP/MySQL, Ruby On Rails, XHTML, CSS. You can contact with me by seigo.ua@gmail.com. Also check my homepage at www.klalex.com.
Occupation: Web Developer
Location: Ukraine Ukraine

Other popular ASP.NET articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 5 of 5 (Total in Forum: 5) (Refresh)FirstPrevNext
GeneralPerfect Solution Pinmemberkoese18:51 3 Oct '07  
GeneralGet code! Pinmemberjgoeke10:54 15 Feb '07  
Generalit is good but.. Pinmemberhbn5:27 10 Oct '06  
GeneralRe: it is good but.. Pinmembercowboybvi@yahoo.co.uk23:46 4 Dec '06  
GeneralRe: it is good but.. PinmemberExtra Gravy11:42 13 Sep '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 9 Oct 2006
Editor:
Copyright 2006 by Alexander Kleshchevnikov
Everything else Copyright © CodeProject, 1999-2009
Web17 | Advertise on the Code Project