Sie sind auf Seite 1von 10

MASTER PAGES: What is Master Page?

A Master Page is simply a single page that holds the structure of your Web site. The files are designated with a .master file extension and are imported into content pages through the MasterPageFile property of the @Page directive of the content pages. They are meant to provide the template that all of your pages will use throughout the site. They are not really meant to hold the content of an individual page or, even, the stylistic definitions of the page. They are meant to provide a blueprint of what your site should look like and then connect that template to style rules set in detached CSS files (as appropriate) and The most interesting part of ASP.NET Web programming is the use ofMaster Page. The master page is the template for how your pages will look like. It enables you to store the same content among multiple content pages in a website. Master pages actually consist of two pieces, the master page itself and one or more content pages.

A master page is an ASP.NET file with the extension .master (for example, MySite.master) with a predefined layout that can include static text, HTML elements, and server controls. The master page is identified by a special@ Master directive that replaces the @ Page directive that is used for ordinary .aspx pages. The directive looks like the following.

<%@ Master Language="C#" % The @ Master directive can contain most of the same directives that a @ Control directive can contain. For example, the following master-page directive includes the name of a code-behind file, and assigns a class name to the master page.

<%@ Master Language="C#" CodeFile="MasterPage.master.cs" Inherits="MasterPage" %>

In addition to the @ Master directive, the master page also contains all of the top-level HTML elements for a page, such as html, head, and form. For example, on a master page you might use an HTML table for the layout, an img element for your company logo, static text for the copyright notice, and server controls to create standard navigation for your site. You can use any HTML and any ASP.NET elements as part of your master page. Replaceable Content Placeholders In addition to static text and controls that will appear on all pages, the master page also includes one or more ContentPlaceHolder controls. These placeholder controls define regions where replaceable content will appear. In turn, the replaceable content is defined in content pages Now we will see how to Create a Sample MasterPage. Go to visualStudio and create a website and Name it as SampleMaster application. Then go to Your Root Folder and Right Click Add New Item Select MasterPageTemplate. Like Below and clik Add. Then a Master page will be added.

The source of the Master page will look like belowIt Contains Content Place holders. Shown Below.

The below is the Design I have done in the Master page:


<table id="mytable1" width="100%" style="" border="2" runat="server"> <tr bgcolor="Green" style="height: 100px"> <td align="center" colspan="3"> <font color="white" size="35px">INFIGO IT SOLUTIONS</font><br /> </td> </tr> <tr bgcolor="white" style="height: 20px"> <td colspan="3"> <table> <tr> <td> <asp:Menu runat="server" ID="Menu3"> <Items> <asp:MenuItem Text="Home" NavigateUrl="~/InfigoMain.aspx" Value="File"></asp:MenuItem> </Items> </asp:Menu>

</td> <td style="width: 30px"> </td> <td> <asp:Menu runat="server" ID="Menu1"> <Items> <asp:MenuItem Text="Cources" NavigateUrl="~/COURCES.aspx" Value="File"></asp:MenuItem> </Items> </asp:Menu> </td> <td style="width: 30px"> </td> <td> <asp:Menu runat="server" ID="Menu2"> <Items> <asp:MenuItem Text="Projects" NavigateUrl="~/Projects.aspx" Value="File"></asp:MenuItem> </Items> </asp:Menu> </td> <td style="width: 50px"> </td> <td> <asp:Menu runat="server" ID="customeMenu"> <Items> <asp:MenuItem Text="Contact" NavigateUrl="~/Contact.aspx" Value="File"></asp:MenuItem> </Items> </asp:Menu> </td> <td style="width: 400px"> </td> <td> <font color="red">Call Us : 8121899813 / 9492185293 </font> </td> </tr> </table> </td> </tr> <tr> <td bgcolor="green" style="width: 10%; height: 500px"> <asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server"> <table> <tr> <td> <h1>This is my left side panel</h1> </td> </tr> </table> </asp:ContentPlaceHolder> </td> <td> <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server"> </asp:ContentPlaceHolder> </td> <td bgcolor="green" style="width: 10%; height: 500px">

<asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server"> <table> <tr> <td> <h1>This is my right side panel</h1> </td> </tr> </table> </asp:ContentPlaceHolder> </td> </tr> </table>

And the Design will look like Below:

Note:

Master Pages are not meant to be rendered on their own; they are only appropriate to be included as a reference to a valid .NET page. So if, for example, you try to view this Master Page in a browser, you would see an error. Now we will add a Sample ASPX Page which will hold the design of the Master Page. Now go to your solution and add a new .aspx page while adding the page Select the master page.. The process give Below..

In the above Screen Adding a New Webpage and Select the Master page while adding and click on Add Button.. Then it will ask to select the Master page. We need to locate the Master page and select.

And click on Ok Button. Then the particular page will be added with the selected Master page. The child page content will be like below .

It will show the content place holders.With in the content place holders we can design our own Custom Designing. The child page will look like this..

Between the white surface is the content place holders we user can Design his custom designing.

You can place any controls in the Content place holders and you can do yor transaction save,update,fetch,delete e.ct.

Advantages of Master Pages :

Master pages provide functionality that developers have traditionally created by copying existing code, text, and control elements repeatedly; using framesets; using include files for common elements; using ASP.NET user controls; and so on. Advantages of master pages include the following: They allow you to centralize the common functionality of your pages so that you can make updates in just one place.

They make it easy to create one set of controls and code and apply the results to a set of pages. For example, you can use controls on the master page to create a menu that applies to all pages. They give you fine-grained control over the layout of the final page by allowing you to control how the placeholder controls are rendered. They provide an object model that allows you to customize the master page from individual content pages.

Das könnte Ihnen auch gefallen