Master page helps you to create a consistent look and feel to your site while minimizing the amount of work you have to do. With the introduction ASP.net 2.0 Master Pages, you no longer have to use server side includes to keep the site consistent among different pages. To be able to use and apply master page to your site you first need to create a master page:
1. Right click on the Project
2. Click Add New Item
3. Change the filename as appropriate then click OK to accept
4. Design the layout of the page as you wish and save it
Note that the design in the master page will be applied to all pages that use it except for the content part.
Now that we have a master page, we can start using it. Let's create a web page that would use the master page.
1. Right click on the Project
2. Click Add New Item
3. Select Web Form
4. Check the box called Select Master Page
5. Select the master page you just created and hit OK.
At this point, the code window will open up for you to edit. The new web page doesn't look like a typical web form but instead it will look something like this:
<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %><asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
your code goes here
your code goes here
</asp:Content>
When you run the application and/or browsing to the web form, IIS will merge both the master page and the web form to produce the finale code and send it to the browser.
Now that you know how to use master page, you might want to take a look at this blog to learn how to dynamically change master page at run time
Showing posts with label Master Page. Show all posts
Showing posts with label Master Page. Show all posts
Wednesday, May 16, 2007
Dynamically changing master page on your website
If you notice, you will see that a lot of blog sites out there allow you to create and use different site template. With a click of a button, you can completely change the look and feel of the entire site; including the theme, layout, navigation...
While this can certainly be done using classic ASP, ASP.net master pages have made it extremely easy to set this up. All you have to do is define a couple of master pages before and change MasterPageFile property at run time.
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Page.MasterPageFile = "NewMasterPage.master"
End Sub
In this code snippet, I just assign a new master page to the MasterPageFile property of the Page object during the preinit stage and the site gets a new look.
If you are new to master pages, please have a look at this blog: Using Master Pages in ASP.net Applications.
While this can certainly be done using classic ASP, ASP.net master pages have made it extremely easy to set this up. All you have to do is define a couple of master pages before and change MasterPageFile property at run time.
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
Page.MasterPageFile = "NewMasterPage.master"
End Sub
In this code snippet, I just assign a new master page to the MasterPageFile property of the Page object during the preinit stage and the site gets a new look.
If you are new to master pages, please have a look at this blog: Using Master Pages in ASP.net Applications.
Subscribe to:
Posts (Atom)