Accessing Master Page at Run Time


The ASP.NET Framework offers the possibility to access Master Pages programmatically during run time. You can use this as a basis for individual enhancements and changes. The Content Page offers the Master property, which is marked as protected. This way, you get an instance on the individual Master Page, from which you get access to the public properties and methods.

The assignment of an individual page title is a typical example for the application. The files in Listing 4-3 show this using a Literal control placed on the Master Page. The text is assigned through a custom and public property, HtmlTitle, which passes the text to the label.

Listing 4-3: Access Any Public Members Through the Master Properties

start example
 // MasterPage3.master <%@ master language="C#" %> <script runat="server">     public string HtmlTitle     {         get { return this.LT_HtmlTitle.Text; }         set { this.LT_HtmlTitle.Text = value; }     } </script> <html> <head runat="server">     <title><ASP:Literal  runat="server" /></title> </head> <body>     <form runat="server">         <asp:contentplaceholder  runat="server">         </asp:contentplaceholder>     </form> </body> </html> // ContentPage3.aspx <%@ page language="C#" master="~/MasterPage3.master" %> <script runat="server" language="c#">     void Page_Load(object sender, System.EventArgs e)     {         this.Master.HtmlTitle = "Hello World!";     } </script> 
end example

If you intend to define meta tags dynamically, I recommend you use the page's Header object, which implements IPageHeader. The object gives access to meta tags, external CSS files, and any style sheets defined globally on that page. The approach shown in Listing 4-4 works well in Master Pages, Content Pages, and normal pages.

Listing 4-4: Assigning Custom Meta Tags

start example
 <%@ page language="C#" master="~/MasterPage4.master" %> <script runat="server" language="c#"> void Page_Load(object sender, System.EventArgs e) {    this.Master.HtmlTitle = "Hello World!";    this.Header.Metadata.Add("Author", "Patrick A. Lorenz"); } </script> 
end example




ASP. NET 2.0 Revealed
ASP.NET 2.0 Revealed
ISBN: 1590593375
EAN: 2147483647
Year: 2005
Pages: 133

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net