How does one traverse the control on a specified page under ASP. NET ?)

Source: Internet
Author: User

 

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

 

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.