There will be situations where we will develop an input form for bulk input processing. Obviously, these forms will have large number of input controls like textbox, dropdownlist, checkbox and radiobutton, etc. In such forms, we need to reset these control once the input is processed for the subsequent request. You can use the below code to reset the input controls at a stretch instead of clearing it one by one.
[Updated code as per the user comments - Thanks to gramotei and Sean]
private void btnClear_Click(object sender, System.EventArgs e)
{
Control myForm = Page.FindControl("Form1");
foreach (Control ctrl in myForm.Controls)
{
//Clears TextBox
if (ctrl is System.Web.UI.WebControls.TextBox)
{
(ctrl as TextBox).Text = "";
}
//Clears DropDown Selection
if (ctrl is System.Web.UI.WebControls.DropDownList)
{
(ctrl as DropDownList).ClearSelection();
}
//Clears ListBox Selection
if (ctrl is System.Web.UI.WebControls.ListBox)
{
(ctrl as ListBox).ClearSelection();
}
//Clears CheckBox Selection
if (ctrl is System.Web.UI.WebControls.CheckBox)
{
(ctrl as CheckBox).Checked = false;
}
//Clears RadioButton Selection
if (ctrl is System.Web.UI.WebControls.RadioButtonList)
{
(ctrl as RadioButtonList).ClearSelection();
}
//Clears CheckBox Selection
if (ctrl is System.Web.UI.WebControls.CheckBoxList)
{
(ctrl as CheckBoxList).ClearSelection();
}
}
}
0 comments:
Post a Comment