Part 147 Master pages in asp net04:33

  • 0
Published on June 10, 2017

Link for code samples used in the demo

Link for all dot net and sql server video tutorial playlists

This is continuation to Part 146, please watch Part 146 before proceeding.

Master pages in asp.net allow you to create a consistent look and behavior for all the pages in an asp.net web application.

To create a master page
1. Right click on the “Project Name” in “Solution Explorer”, and select “Add – New Item”
2. From the “Add New Item” dialog box, select “Master Page”
3. Give your master page a name and click OK.

Master pages have .master extension. Notice that there are 2 ContentPlaceHolder controls on the master page.
1. One in the “head” section and
2. Second one in the “body” section

ContentPlaceHolder control defines a region on the master page, where the content pages can plugin page specific content.

Points to remember
1. html, head and body sections are not present on the content page, as they are already present on the master page, that this content page is subscribed to.
2. Content pages are associated with master pages using “MasterPageFile” attribute of the “Page” directive
3. “Content” control on a content page is linked to “ContentPlaceHolder” on the master page using “ContentPlaceHolderID” attribute of the “Content” control. Controls like textbox, button, gridview etc.. and any text that you want in the content page must be placed inside the content control. If you try to place anything outside of the content control, you would get an error – Only Content controls are allowed directly in a content page that contains Content controls.
4. When you flip the content page to design mode, notice that, the region outside of the “ContentPlaceHolder” is greyed out and disabled. This is because on a content page, you should only be able to edit “Content” that is specific to that page.

So, an asp.net master page allows us to define a common layout for all the pages in the application. Later, if we have to change something in the common layout, we only have to do it at one place, that is in the master page.

Enjoyed this video?
"No Thanks. Please Close This Box!"