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 = "";
}
}
}
}