If you use mastepage. Sometimes, you need to access the attributes, methods, or control information of masterpage on contentpage during development. This demo illustrates how to implement such a job>
In fact, it is very simple. The key step is to "declare" the "Reference" to masterpage in contentpage ":
<% @ Page Language = "C #" masterpagefile = "~ /Masterpage. Master "autoeventwireup =" true "codefile =" contentpage1.aspx. cs "inherits =" contentpage1 "%>
<% @ Mastertype virtualpath = "~ /Masterpage. Master "%> <% -- declare here -- %>
<Asp: Content ID = "content1" contentplaceholderid = "contentplaceholder1" runat = "server">
</ASP: content>
In contentpage, you can access the attributes and methods of masterpage public and its controls.
Masterpage HTML:
<% @ Master language = "C #" autoeventwireup = "true" codefile = "masterpage. master. cs" inherits = "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> my masterpage title </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Asp: Label id = "lblheader" runat = "server" text = "label"> </ASP: Label>
<Br/>
<Asp: textbox id = "TXT" runat = "server"> </ASP: textbox>
</Div>
<Div>
<Asp: contentplaceholder id = "contentplaceholder1" runat = "server">
</ASP: contentplaceholder>
</Div>
</Form>
</Body>
</Html>
Masterpage Cs:
Public partial class masterpage: system. Web. UI. masterpage
{
Private String title;
Public String title
{
Get {return title ;}
Set {Title = value ;}
}
Protected void page_load (Object sender, eventargs E)
{
This. lblheader. Text = title;
}
Public void gettitle ()
{
TXT. Text = "I will go ";
}
}
A property and a method are defined in masterpage.
Contentpage:
Protected void page_load (Object sender, eventargs E)
{
Page. header. Title = "masterpage title here ";
Master. Title = "Access to masterpage's property ";
Master. gettitle ();
Textbox TXT = Master. findcontrol ("TXT") as textbox;
TXT. backcolor = system. Drawing. color. Red;
}
As you can see, attributes, methods, and controls are accessible.