Home > ASP.NET, C# > Reset values of all controls in form/ASP.Net page using C#

Reset values of all controls in form/ASP.Net page using C#

Today i am going to blog a post which is not a rocket science , well there is no rocket science ever posted from my side 🙂

I was lying in my blanket , enjoying cozy winter using the laptop , when friend of mine asked ‘what if we need to clear the values of all controls on form to defaults’.

That was not a tough question , more then tough it’s interesting as there are many scenarios in which we need to give default values to the controls on web or form in single click.

foreach is the key loop in that.

So lets start exploring this query.

Initially a form with following controls is designed.


  • Check Box : name is chkBox
  • Radio Button : name is rbtn
  • Text Box : name is txtBox
  • Drop Down : name is cmbBox
  • Button : name is btnReset

Now requirement is to set the default values to all the controls on the form or web. Right now i am going with form.

Few controls are being used to make it more easy to understand.
Here comes the code for that on btnReset_Click

private void btnReset_Click(object sender, EventArgs e)


           foreach (Control c in this.Controls)


               switch (c.GetType().ToString())


                   case "System.Windows.Forms.TextBox":

                       ((TextBox)c).Text = string.Empty;


                   case "System.Windows.Forms.ComboBox":

                       ((ComboBox)c).SelectedIndex = 1;


                   case "System.Windows.Forms.CheckBox":

                       ((CheckBox)c).Checked = false;


                   case "System.Windows.Forms.RadioButton":

                       ((RadioButton)c).Checked = false;





I have assigned the default values in Switch statement.


In case of web page , class representing text box “System.Windows.Forms.TextBox” would have been “System.Web.UI.WebControls.TextBox”


Happy coding :).

  1. Manisha
    August 17, 2013 at 2:24 pm

    Good Explanation and code.

  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: