Foreach (control CTL in page. Controls)
{
Response. Write ("<li>" + CTL. tostring ());
}
Above Code It does not display the list of all controls contained in the page. It only displays direct child controls of the page class, instead of any child controls of these child controls. To display all controls on the page, you can recursively traverse the controls set of each control, such:
# 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.
/// </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 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