ASP. NET adopts the masterpage Control Structure

Source: Internet
Author: User

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)

}

 

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.