Saturday, June 16, 2007

Using a Master Page in ASP.net

The easiest & quickest way to get on with Master Pages

Creating a Master Page :

  1. Create an aspx page, as you want the basic layout to be.
  2. Add a new Master Page to your web app.
  3. Copy all code from aspx page created in step 1, exclude @Page tag
  4. Paste everything by replacing default code in MasterPage, keep @Page tag as it is
  5. Wherever you want content to appear from content pages, place a <asp:contentplaceholder> control
  6. Master page is ready now
The above method helps in previewing changes easily, though a master page can be created directly without using an aspx page first, and can be previewed using a content page.

Using a Master Page :

  1. Add a WebContentForm to web app.
  2. if its a web project, you will have to select WebForm and check 'select master page' check box while adding this page.
  3. Select the master page for this page. That's it!
If a page already exist and you want to convert it to a content page, add "MasterPageFile" attribute in @Page directive of the wannabe content page and remove all html code from this page and pace <asp:content> control and the code you want to appear in content area.

No comments: