CODEDIGEST
Home » Articles
Search
 

Technologies
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Single Button Click Custom Control

By sameer sayani
Posted On Apr 19,2009
Article Rating:
Average Rating: 2
No of Ratings: 1
No of Comments: 0
Category: ASP.Net
Print this article.

Single Button Click Custom Control

 

     This article demonstrates a custom control for button which gets disabled after user clicks on it so that we can stop user to resubmit the same information again and again intentionally or unintentionally. 

Here, when user clicks on submit button, its first checks the validation part. After passing it, the data gets posted & button gets disabled. After that the button gets enabled for new submission. Also the text written in the textbox gets changes dynamically.

 

For Custom Control, go to new project. Select Web Custom Control & rename it to singleClickBtn.

A custom control for button is developed. Here is the code snippet for it:-

 

public class singleClickBtn : Button

    {

      

        [Bindable(true)]

        [Category("Appearance")]

        [DefaultValue("")]

        [Localizable(true)]

 

 

        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.ClientScript.GetPostBackEventReference(this,String.Empty));

            script.Append(";");

            this.Attributes.Add("onclick", script.ToString());

        }



 

        In above code, I am inheriting Button class so that I can override its methods. After that a property is declared to overwrite the text written in Button. On initialization event of button, I am checking for Client Validate function. If it returns true the button gets disabled else it will remain intact.

        Now our custom control is ready. Build it first in debug mode then in release mode. After that create a new website. Right click on the Toolbox. Click on choose item and add the dll of the custom control we just developed. Drag and drop it on any aspx page. Now, it is just like other webcontrols fully customizable.

Drop a textbox. Place validation on it & check.

    

I have checked the same in FireFox & Safari. It works fine.

 

If you have any queries, just post it to me.

Downloads

Download Source
Similar Articles