Traverse the page's controls set

Source: Internet
Author: User
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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.