Free Training
C Language   |   CSS   |   MainFrame   |   VBScript   |   PHP   |   XML   |   C++ Tutorials   |   Ajax   |   JavaScript   |   CSS3   |   UML   |   jQuery   |   Microsoft AJAX

Sponsored Links

ASP.NET 2.0 Tutorials

 
Home Tutorials ASP.NET 2.0
 

ASP.NET 2.0: Referencing Master Page Members

 

ASP.NET 2.0: Referencing Master Page Members

Page 1 of 2

ASP.NET 2.0 Training : Referencing Master Page Members

In this tutorial you will learn about reference Master Page Mebers, add property to Master Page, expose Master Properties.


Referencing Master Page Members

Members of the Master page can be referenced by content pages. These members can be methods, properties or controls. The constraint for property reference is theat the property has to be declared as public members of the master page. They could be public page scope variables, public propertis and public methods. Let us work out a simple example. The developer wants to set the title of a content page or to add a style sheet on a per page basis. Code will have to be added to the Page_Load event. The Header property on the Page class exposes the content of the< head > tag as programmable entities.


To add a property to the master page

1. Open the MasterPage.master page.
2. In Solution Explorer, right-click MasterPage.master, and then click View Code to open the code editor.
3. Type the following code in the Home.aspx:

...................< Script runat=”server” >
.................................Void Page_Load(object sender, EventArgs e)
................................{
.............................................Header.title= “This is another title”;
................................}
...................< /Script >



4. The code sets a different title for the master.



To expose Master Properties


A control can be given an identity in the master by simply setting the runat attribute and giving the control an ID. The control can then be accessed from within the content page through the Master property. The Master property defined on the Page class references the master page object for the content page. It implies that only public properties and method defined on the master page class are accessible.


Let us say that the requirement is that a subtitle has to be exposed as a programmable property. First we shall render the string through a server side control and then we shall create a public wrapper property to make the control accessible from the content page. Add the following code to the masterpage.master. The code defines the public SubTitle and wraps the InnerText property of the _titlebar control.


1. Open MasterPage.master
2. Enter the following code below @Master directive.


Click here to view sample code


Next Page: ASP.NET 2.0: Referencing Master Page Members - Page 2


Read Next: ASP.NET 2.0 :Changing Master Pages Dynamically



 

 

Comments


Ajatsahtru said:

  First of all thnx a ton for a great post. I'm a biggner to asp.net.
I'm not getting this line:

The following code will have to be added to the content page
((MasterPage)Master).SubTitle= “Welcome”;

Where to put this line exactly??
Hope u reply this stupid question.
Regards
May 29, 2009, 2:33 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links