Traverse all controls on the page

Source: Internet
Author: User

This problem is often encountered during the development of our system. There are many page controls, and we hope to control these controls through traversal. To give a simple example, you can determine whether all textbox buttons on the page are empty or clear all textbox buttons. In this way, we need to find a way to obtain these controls. The following is an example of the Code.

This is the code used to verify whether all radiobuttonlist on the page has been selected:

/** // <Summary>
/// Enter the verification information
/// </Summary>
Private void validatefield ()
{
For (INT I = 0; I <page. Controls. Count; I ++)
{
Foreach (system. Web. UI. Control in page. controls [I]. Controls)
{
If (control is radiobuttonlist)
{
If (string. isnullorempty (control as radiobuttonlist). selectedvalue ))
{
BMC. clutility. showmessage (this. Page, "some images are not filled in ");
Return;
}
}
}

}
}

If your page also contains widgets such as panel, datalist, and gridview, you can call them recursively [although the performance is not high ]:

Code
/** // <Summary>
/// Enter the verification information
/// </Summary>
Private void validatefield ()
{
For (INT I = 0; I <page. Controls. Count; I ++)
{
Validatefield (page. controls [I]. Controls );
}
}
/** // <Summary>
/// Verify the control set
/// </Summary>
/// <Param name = "cc"> </param>
Private void validatefield (controlcollection CC)
{
Foreach (system. Web. UI. Control in CC)
{
If (control is radiobuttonlist)
{
If (string. isnullorempty (control as radiobuttonlist). selectedvalue ))
{
BMC. clutility. showmessage (this. Page, "some images are not filled in ");
Return;
}
}
If (control is checkboxlist)
{
If (string. isnullorempty (control as checkboxlist). selectedvalue ))
{
BMC. clutility. showmessage (this. Page, "some images are not filled in ");
Return;
}
}
If (control. hascontrols ())
{
Validatefield (control. Controls );
}
}
}

 

Similarly, we can clear all textbox

Clear all edit box Codes
/** // <Summary>
/// Clear all edit boxes
/// </Summary>
Private void clearalltextbox ()
{
For (INT I = 0; I <page. Controls. Count; I ++)
{
Foreach (system. Web. UI. Control in page. controls [I]. Controls)
{
If (control is textbox)
{
(Control as textbox). Text = "";
}
}
}
}

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.