Use ASP. NET master pages programmatically

Source: Internet
Author: User

You can use the master page to program many public tasks, including:

  • Access members defined on the master page (including public properties/methods or controls ).

  • Dynamically attaches the master page to the content page.

Access members on the master page

To provide access to the master page members, the page class discloses the master attribute. To access a specific
You can use the @ mastertype command to create a strong type reference for the master page. Yes
Use this command to point to a specific master page. When you create your ownMasterAttribute
Type is set to reference the master page.

For example, there may be a master page named masterpage. Master, which is a class nameMasterpage_master. Create @ page and@ MastertypeCommand:

<% @ Page masterpagefile = "~ /Masterpage. Master "%>

<% @ Mastertype virtualpath = "~ /Masterpage. Master "%>

When using@ MastertypeCommands (such as commands in this example) can reference members on the master page, as shown in the following example:

CompanyName. Text = Master. companyName;

TheMasterThe property type has been setMasterpage_master.

Obtain the value of the control on the master page.

At runtime, the master page and content page are merged, so the code of the content page can access the control on the master page. (If the contentplaceholder control on the master page contains some controls, these controls will be inaccessible after being overwritten by the content control on the Content Page .) These controls are protected and therefore cannot be directly accessed as master page members. However, you can use the findcontrol method to locate specific controls on the master page. If the control to be accessed is located inContentplaceholderInside the control, you must first obtainContentplaceholderControl, and then call its findcontrol method to obtain the reference to the control.

The following example shows how to obtain a reference to a control on the master page. One of the referenced controls is located inContentplaceholderControl, the other is not.

 
// Gets a reference to a TextBox control inside a ContentPlaceHolder 
ContentPlaceHolder mpContentPlaceHolder; 
TextBox mpTextBox; 
mpContentPlaceHolder = (ContentPlaceHolder)Master.FindControl(
"ContentPlaceHolder1"); 
if(mpContentPlaceHolder != null) 
{ 
   mpTextBox = (TextBox) mpContentPlaceHolder.FindControl("TextBox1");
   if(mpTextBox != null) { mpTextBox.Text = "TextBox found!"; } 
} 
// Gets a reference to a Label control that is not in a  
// ContentPlaceHolder control 
Label mpLabel = (Label) Master.FindControl("masterPageLabel"); 
if(mpLabel != null) 
{ 
   Label1.Text = "Master page label = " + mpLabel.Text; 
} 

As shown above, you can useFindcontrolMethod to access the master pageContentplaceholderControl content. IfContentplaceholderThe widget hasContentControl content merging,ContentplaceholderThe widget does not contain its own default content. Instead, it includes the text and controls defined on the Content Page.

Dynamically append a master page

In addition@ PageCommand or configuration file), you can dynamically attach the master page to the content page. Because the master page and content page are merged in the initialization phase of page processing, the master page must be allocated before this. Generally, the master page is dynamically allocated in the preinit phase, as shown in the following example:

Void page_preinit (Object sender, eventargs E)
{This. masterpagefile = "~ /Newmaster. Master ";
} Strong types of dynamic master pages

If the content page uses@ MastertypeThe command assigns a strong type to the master page, which must be applicable to all dynamically assigned master pages. If you want to dynamically select a master page, we recommend that you create a base class and derive the master page from this base class. This base master page class can then define the common attributes and methods of the master page. When you use@ MastertypeWhen a strong type is assigned to a master page, the type can be assigned to the Base class instead of a single master page.

The following example shows how to create a base master page type that can be used by multiple master pages. This example includes a base type derived from the masterpage control, two master pages inherited from this base type, and a query string (? Color = green) Dynamically select the content page of the master page. This base master Type defines the nameMytitle. One of the master pages overrides thisMytitleProperty. The other master page is not overwritten. The content page willMytitleProperty is displayed as the page title. Therefore, the page title varies with the selected master page.

This is the base master page type.

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;
Public class basemaster: system. Web. UI. masterpage
{
Public Virtual string mytitle
{
Get {return "basemaster title ";}
}
}

This is the first master page with a blue background. Note,@ MasterIn the commandInheritsAttribute reference base type.

 
<%@ Master Language="C#" Inherits="BaseMaster" ClassName="MasterBlue" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"  "http://www.w3.org/TR/
xhtml11/DTD/xhtml11.dtd">
 <script runat="server"> 
// No property here that overrrides the MyTitle property of the base 
master.
</script> 
<title>No title</title> 

<body> 
<form id="form1" runat="server"> 
<div style="background-color:LightBlue"> 
   <asp:contentplaceholder id="ContentPlaceHolder1" runat="server"> 
      Content from MasterBlue. 
   </asp:contentplaceholder> 
</div>
</form> 
</body> 

This is the second master page. Besides displaying the green background and overwritingMytitleIt is the same as the first master page.

<% @ Master language = "C #" inherits = "basemaster"
Classname = "mastergreen" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.1 // en"
Http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd>

<SCRIPT runat = "server">
Public override string mytitle
{
Get {return "mastergreen title ";}
}
</SCRIPT>

<HTML>
<Head id = "head1" runat = "server">
<Title> No title </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div style = "background-color: lightgreen">
<Asp: contentplaceholder id = "contentplaceholder1"
Runat = "server">
Content from mastergreen.
</ASP: contentplaceholder>
</Div>
</Form>
</Body>
</Html>

This is the content page, which allows you to select the master page based on the query string provided by the request. Use
@ MastertypeCommand to grant the strong type to the master pageMasterThis command references the base type.

<% @ Page Language = "C #" Title = "content page" %>
<% @ Mastertype typename = "basemaster" %>
<SCRIPT runat = "server">
Protected void page_preinit (Object sender, eventargs E)
{
This. masterpagefile = "masterblue. Master ";
If (request. querystring ["color"] = "green ")
{
This. masterpagefile = "mastergreen. Master ";
}
This. Title = Master. mytitle;
}
</SCRIPT>

<Asp: Content ID = "content1" contentplaceholderid = "contentplaceholder1"
Runat = "server">
Content from content page.
</ASP: content>

Address: http://blog.coders.com.cn/pignut_wang/

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.