After Mid Lec 2
After Mid Lec 2
Of
Of
Master Pages
In
ASP.Net
In today’s session:
Understanding the Master Page
Creating the Master Page
Using the Table Tags in Master Page
Applying the Master Page to other Pages
In Web site development using ASP.NET, the master page is a feature that enables you to
define common structure and interface markup elements for your Web site,
including headers, footers, style definitions, or navigation bars. Master pages allow you to
create a consistent look and behavior for all the pages (or group of pages) in your web
application. The master page can be shared by any of the pages in your Web site, called
the Content Page, and removes need to duplicate code for shared elements within your Web
site. The master page defines placeholders for the content, which can be overridden by
content pages. When users request the content page, ASP.NET merges the pages to produce
output that combines the layout of the master page with the content of the content page.
Marquee NEWS
Left Links Body Text (contents Page will be opened here) [Colspan = 2]
3. Here, From the Open Add New Item Window the Master Page and press Add
button
4. The Master Page will be added to your web site and you will see the Master Page.
Here, first Change the Title of the Master Page in the HTML <HEAD> tag as shown:
<title> :: Students Portal :: </title>
5. In the Master Page, see the selected <DIV> Tag. There is a tag called
<asp:ContentPlaceHolder>. This tag defines the area in which all the other linked web
forms are displayed. Here before the <DIV> tag, design the <TABLE> as we give the
Table design on previous page of this document.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>:: Students Portal :: </title>
<asp:ContentPlaceHolder id="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<table width="100%" border="2">
<tr>
<td width="15%">Logo-1</td>
<td>Students <br />Portal</td>
<td width="15%">Logo-2</td>
</tr>
<tr>
<td colspan="3">
<marquee>Today! We are learing Using Master Pages in ASP.Net</marquee>
</td>
</tr>
<tr>
<td></td>
<td colspan="2"></td>
</tr>
<tr>
<td colspan="3" align="center"> ©</td>
</tr>
</table>
<div>
<asp:ContentPlaceHolder id="ContentPlaceHolder1"
runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
7. Next Step is to Cut the Content Place Holder tag inside the <DIV> tag as we have
shown in the gray color highlighted in the above figure. Now, Paste it in the third
Row second <TD> in which we have used Colspan=”2”. This is shown in the next
figure.
Finally, this is the complete Master Page Code. Now, Add a Web Form and Apply
Master Page to this Web Form As Master Page is useless unless we not apply it to a page
that will be opened inside the Master Page.
Here , we have learnt how to add and design a Master Page in ASP.Net. In the following
Section, we will see how to apply Master Page to Web Form or Content Page.
1.3 Applying Master Page to the Content Page
1. Add a New Form by Right Click on the Web Site Name and Choose Web Form and at
the bottom of the Add New Item Window, Check the Apply Master Page Option as
shown
2. Then Press the ADD button, you will be shown all the Master Pages in your web
site, here Select the required Master Page, as we can have multiple Master Pages in a
single Web. For the time being we have only one Master Page for our web site. Press
the OK Button,
3. You can see the following Code in the Home.aspx file. In this file, Developer can
add the code for the Home.aspx page between the
Finally, Save the file, and run the web site. You will see the following output
That is all, we want to do today….
To add a folder and rename the folder to Images, follow the steps shown below:
Right Click the Web Site and
Choose Add New Folder option and rename the Folder to Images
Copy some Images to this folder by simple Copy & Paste Options.
After adding Images to the folder, open the Master Page in Source Code View. Look for
the <TD> in the first <TR> of the above designed Master Page and add image using <IMG>
tag. This code is shown here
You can set its Height and Width property according to your site requirements:
Here, we set the
Height = 180 and Width = 200
Similarly, add the second image in the third <TD> of the same <TR> . Thus the complete
code of first <TR> is shown here
Thus, the site is furnished with images. In this way, you can add images as much as and
anywhere in your web site. The final output is
These ASP.Net Tutorials are prepared only for the purpose of learning ASP.Net with C#.
Anyone can copy, print or reuse it in any format
Saif Ur Rehman Saifi
Assistant Professor, UIIT
PMAS Arid Agriculture University, Rawalpindi
If you found this tutorial helpful or you want to give us any valuable suggestions contact at
www.ASPUIIT.blogspot.com