0
Posted on 4:06 AM by prajeesh and filed under ,
In some applications we may need to reset all controls in using a single "Reset" button click, here is the c# code to achieve this.
  1. public  static void ResetControls(ControlCollection pagecontrols, bool txtbox, bool dropdownlist, bool label)  
  2.   {  
  3.       foreach (Control cntrl in pagecontrols)  
  4.       {  
  5.           foreach (Control mycontrols in cntrl.Controls)  
  6.           {  
  7.               if (txtbox)  
  8.               {  
  9.                   if (mycontrols is TextBox)  
  10.                   {  
  11.                       (mycontrols as TextBox).Text = string.Empty;  
  12.                   }  
  13.               }  
  14.               if (dropdownlist)  
  15.               {  
  16.                   if (mycontrols is DropDownList)  
  17.                   {  
  18.                       (mycontrols as DropDownList).SelectedIndex = 0;  
  19.                   }  
  20.               }  
  21.               if (label)  
  22.               {  
  23.                   if (mycontrols is Label)  
  24.                   {  
  25.                       (mycontrols as Label).Text = string.Empty;  
  26.                   }  
  27.               }  
  28.           }  
  29.       }  
  30.   }  

We can call this function using following format if you want to clear all controls except label
  1. FormControl.ResetControls(this.Controls, truetruefalse);  
Shout it kick it on DotNetKicks.com
0
Responses to ... Resetting all controls in an asp.net form