Pages

Subscribe:

Ads 468x60px

Thursday, March 15, 2012

how to reset all form elements in asp.net using c#

In this web programming tutorial we will learn that how we can reset all web form controls in one click ie (by clicking on reset button) in asp.net using c#.
code for .cs file.

public  static void ResetControls(ControlCollection pagecontrols, bool txtbox, bool dropdownlist, bool label)  
  {  
      foreach (Control cntrl in pagecontrols)  
      {  
          foreach (Control mycontrols in cntrl.Controls)  
          {  
              if (txtbox)  
              {  
                  if (mycontrols is TextBox)  
                  {  
                      (mycontrols as TextBox).Text = string.Empty;  
                  }  
              }  
              if (dropdownlist)  
              {  
                  if (mycontrols is DropDownList)  
                  {  
                      (mycontrols as DropDownList).SelectedIndex = 0;  
                  }  
              }  
              if (label)  
              {  
                  if (mycontrols is Label)  
                  {  
                      (mycontrols as Label).Text = string.Empty;  
                  }  
              }  
          }  
      }  
  }  
now we will call above function in order to reset all form elements except lables as below
FormControl.ResetControls(this.Controls, true, true, false);  

No comments:

Post a Comment