Master Pages in ASP.NET
Master Pages in ASP.NET are files that have .master extensions. ASP.NET master pages allow us to create a consistent layout and look & feel for all the pages or a group of pages in web application. A page which uses a master page is known as the content page. We can then create individual content pages that contain the content what we want to display. A master page can contain one or more content pages.
This article takes a look at what master pages and content pages are and how we can work with them in ASP.NET.
According to the MSDN, "ASP.NET master pages allow you to create a consistent layout for the pages in your application. A single master page defines the look and feel and standard behavior that you want for all of the pages (or a group of pages) in your application. You can then create individual content pages that contain the content you want to display. When users request the content pages, they merge with the master page to produce output that combines the layout of the master page with the content from the content page."
The basic differences between a master page and a normal .aspx page are:
Master Pages are used in lieu of User controls when you need to provide a consistent look and feel of the pages in your application. If you use a User control to provide a consistent look and feel, you have to add it across all pages in your application. Moreover when using user controls, we can not visualize what the page looks like when we are creating it. You can only know the look and feel once the page is executed in the web browser. You can use a User control to create the login form of your application – you typically want the login form to appear only once the application is executed.
Here are the steps to create a master page in ASP.NET:
To create a content page, follow these steps:
Right click on the web application in the solution explorer and select Add->New Item
Now, link this content page to the master page you created earlier as shown below:
Here is how the markup code of the generated Content page looks like:
Note the MasterPageFile attribute in the content page. This attribute is used to provide a reference to the master page to which the content page refers. Further, there is no <form> or <html> tags in the content page unlike a typical .aspx page.
When we are using both master and content pages together, the following is the sequence of the events that are fired for both:
You can configure a master page in any of these three ways:
To access a master page from within a content page, we can use the FindControl method as shown below:
A nested master page is one that references another master page as its master. A nested master page is ideal in situations where we want to keep the main layout in main master page and will inherit the same in another master page. Note that both the parent and child master pages will have the .master extensions. The MSDN states, "Master pages can be nested, with one master page referencing another as its master. Nested master pages allow you to create componentized master pages. For example, a large site might contain an overall master page that defines the look of the site. Different site content partners can then define their own child master pages that reference the site master and that in turn define the look for that partner's content."
Here is an example:
This is markup code of the Parent Master Page:
Master pages can be used to provide a consistent look and feel to one or more pages in your application. Master pages can be nested also. In this article we have had a look at what Master pages are and how we can use them in our applications.
Re: Master Pages in ASP.NET
Nomination this Article for Article of the month - May 2009
Re: Master Pages in ASP.NET
In this section, you create a parent master page. This page contains a banner and navigation controls that can be used throughout the site. Later, you will create another master page that will be used inside this parent master page. Child master pages can provide various layouts for pages while retaining the look that is established by the parent master page.
To create the master page
1.In Solution Explorer, right-click the name of the Web site, and then click Add New Item.
The Add New Item dialog box is displayed.
2. Under Visual Studio installed templates, click Master Page.
3. In the Name box, type ParentMaster.
4. Clear the Place code in separate file check box.
5. In the Language list, click the programming language that you prefer to work with.
6. Click Add.
The new master page is opened in Source view.
And this example also we can creating a Master Pages in ASP.NET
|All times are GMT +5.5. The time now is 17:31.|