How to traverse all controls on a specified page under ASP. NET
Preface: the purpose of writing it is to feel that this code will be helpful to some friends!
# Region clears all controls on the specified page, public static void ClearAllContent ()
/// <Summary>
/// Clear all the controls on the specified page, including TextBox, CheckBox, CheckBoxList, RadioButton, and RadioButtonList. But not clear
/// In addition to ListBox and DropDownList, such control values can be used for the current page. Generally, these controls contain stored Dictionary data.
/// Author: Kevin
/// Date: 2004-12-02
/// </Summary>
/// <Param name = "page"> specified page </param>
Public static void ClearAllContent (System. Web. UI. Control page)
{
Int nPageControls = page. Controls. Count;
For (int I = 0; I <nPageControls; I ++)
{
Foreach (System. Web. UI. Control control in page. Controls [I]. Controls)
{
If (control. HasControls ())
{
ClearAllText (control );
}
Else
{
If (control is TextBox)
(Control as TextBox). Text = "";
If (control is CheckBox)
(Control as CheckBox). Checked = false;
If (control is RadioButtonList)
(Control as RadioButtonList). SelectedIndex =-1;
If (control is RadioButton)
(Control as RadioButton). Checked = false;
If (control is CheckBoxList)
{
Foreach (ListItem item in (control as CheckBoxList). Items)
{
Item. Selected = false;
}
}
} // If... else
} // Foreach
} //
}
# Endregion