Asp.net: access controls on the nested template page

Source: Internet
Author: User

If the control to be accessed is inside the contentplaceholder control on the master page, you must first obtain the reference to the contentplaceholder control, and then call its findcontrol method to obtain reference to the control.

In the page_load method on the Content Page, set the text attribute of the label control on the nested template page:
Obtain the reference of the contentplaceholder control on the main template page, and then obtain the reference of the control to be modified.
Protected void page_load (Object sender, eventargs E)
{
If (! Ispostback)
{
Contentplaceholder Pcontent = (contentplaceholder) page. Master. Master. findcontrol ("Main ");
If (Pcontent! = NULL)
{
Label plbltitle = (Label) Pcontent. findcontrol ("lbl_title ");
If (plbltitle! = NULL)
{
Plbltitle. Text = "my title ";
}
}
}
}

Appendix:
Main template page:
<% @ Master language = "C #" autoeventwireup = "true" codefile = "masterpage. master. cs" inherits = "master_masterpage" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: contentplaceholder id = "Main" runat = "server">
</ASP: contentplaceholder>
</Div>
</Form>
</Body>
</Html>

Nested template page:
<% @ Master language = "C #" masterpagefile = "~ /Master/Default. Master "autoeventwireup =" true "codefile =" combines. master. cs "inherits =" master_combines "%>

<Asp: Content ID = "content_combine" contentplaceholderid = "Main" runat = "server">
<Asp: Label id = "lbl_title" runat = "server" text = ""> </ASP: Label>
<Asp: contentplaceholder id = "combine" runat = "server">
</ASP: contentplaceholder>
</ASP: content>

Content Page:
<% @ Page Language = "C #" masterpagefile = "~ /Master/combines. Master "autoeventwireup =" true "codefile =" stationcombine. aspx. cs "inherits =" _ default "Title =" untitled page "%>
<% @ Mastertype virtualpath = "~ /Master/combines. Master "%>

<Asp: Content ID = "content1" contentplaceholderid = "combine" runat = "server">
</ASP: content>

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.