You can enable trace on a single page or enable global trace in Web. config to view the control tree.
The Page Structure after masterpage is used is as follows:
// Page -- ASP. about_aspx
// Master page -- ASP. site_master
// (Master Page Markup and controls)
// Contentplaceholder -- headcontent
// Content Page Markup and server controls
// (Master Page Markup and controls)
// Contentplaceholder -- maincontent
// Content Page Markup and server controls
// (Master Page Markup and controls)
Whether we use findcontrol to find a control or add a control, it must be added to the contenplaceholder!
BelowCodeAdded controls dynamically and two methods to search for controls.
// BmK findcontrolrecursive
/// <Summary>
/// Iterate through the control to find the child Control
/// </Summary>
/// <Param name = "rootcontrol"> Container containing controls </Param>
/// <Param name = "controtofftobeseek"> ID of the control to be searched </Param>
/// <Returns> </returns>
Private Control findcontrolrecursive (control rootcontrol, String Controtofftobeseek)
{
If (Rootcontrol. ID = Controtofftobeseek)
Return Rootcontrol;
Foreach (Control In Rootcontrol. Controls)
{
Control controltoreturn = Findcontrolrecursive (control, controw.tobeseek );
If (Controltoreturn ! = Null )
Return Controltoreturn;
}
Return Null ;
}
// When masterpage is created, the control must be added to contentplaceholder.
// To search for a control, you must also use findcontrol in contentplaceholder. No matter what the level is, you can always get valid results.
Protected Void Button#click ( Object Sender, eventargs E)
{
For ( Int I = 0 ; I < 5 ; I ++ )
{
Textbox txtdynamicadded = New Textbox ();
Txtdynamicadded. ID = " Txt " + I. tostring ();
Txtdynamicadded. Text = " Hello, world! " ;
// Visible status. Otherwise, the dynamically added controls will not be displayed after the PostBack operation.
Viewstate [txtdynamicadded. ID] = True ;
This . Master. findcontrol ( " Maincontent " ). Controls. Add (txtdynamicadded );
}
Control ctrlfindbutton = Findcontrolrecursive ( This , " Button1 " );
If (Ctrlfindbutton ! = Null )
{
Button btnfind = (Button) ctrlfindbutton;
Btnfind. Text = " Find " + Datetime. Now. tostring ( " T " );
}
//How can I access maincontent?
Control CC= This. Master. findcontrol ("Maincontent"). Findcontrol ("Button1");
// Page -- ASP. about_aspx
// Master page -- ASP. site_master
// (MASTER Page Markup and controls)
// Contentplaceholder -- headcontent
// Content Page Markup and server controls
// (MASTER Page Markup and controls)
// Contentplaceholder -- maincontent
// Content Page Markup and server controls
// (MASTER Page Markup and controls)
}