In most of the web applications you may need to add a reset button to clear the values of a form which is having dropdown and textboxes, in this situation most of the developers are doing this by adding code like TextBox1.Text =""; and DropDown1.SelectedValue="0", there is a simple solution to do this.
See below code:
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; } } } } }
You can call the function using following syntax:
FormControl.ResetControls(this.Controls, true, true, false);
Above statement resets all controls except label in the current form
|
No responses found. Be the first to respond and make money from revenue sharing program.
|