Create a master page in ASP. NET 2.0

Source: Internet
Author: User

Although master pages and content pages are powerful, their creation and application processes are not complex. This and next sections take the example shown in "create 1" as an example to describe how to use Visual Stuido 2005 to create a master page and content page. This section focuses on how to create a master page.

The master page contains the public part of the page, that is, the webpage template. Therefore, before creating an example, you must determine which content is the public part of the page, starting with analyzing the page structure. Figure 1 shows a page. In the following section, the page is named Index. aspx and assumed to be a page in a website. The analysis shows that the structure of the page is 5.



Figure 5 page Structure

Page Index. aspx consists of four parts: page header, page end, content 1, and content 2. In this example, the header and footer of a page are public parts of the page where Index. aspx is located. Many pages of a website contain the same header and footer. Content 1 and content 2 are non-public parts of the page and are unique to the Index. aspx page. Based on the knowledge of the master page and content page, we can see that if you use the master page and content page to create the page Index. aspx, you must create a master page MasterPage. master and a content page Index. aspx. The master page contains content such as the header and end of the page, and the content page contains content 1 and content 2.

Use Visual Studio 2005 to create a common Web site, and then create a master page named MasterPage. master under the root directory of the site. Because this is a process of adding a new file, you can click the "Add new project..." option in the "website" command menu to open the window shown in 6.



Figure 6 Add a master page

In this example, the master page is created. Therefore, you must select the master page icon and set the file name to MasterPage. master. Note that the window also contains a check box item "put the code in a separate file ". By default, this check box is selected. Indicates that Visual Studio 2005 will be MasterPage. the application code hiding model of the master file is created in MasterPage. automatically create a MasterPage related to the master file. master. cs file. If this option is not selected, only one MasterPage. master file will be created. We recommend that you select this option.

After creating the MasterPage. master file, you can edit the file. According to the previous instructions, the master page only contains the public part of the page. Therefore, MasterPage. master mainly contains the code of the page header and the end of the page. The source code is as follows:

Master page MasterPage. master File Source Code

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

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> </title>
<Link href = "css/myfreetemplates.css" rel = "stylesheet" type = "text/css"/>
</Head>

<Body background = "http://www.68design.net/art/images/pixi_lime.gif" leftmargin = "0" topmargin = "0">
<Form id = "form1" runat = "server">
<Div align = "center">
<Table width = "763" height = "100%" border = "0" cellpadding = "0" cellspacing = "0" bgcolor = "# FFFFFF">
<Tr>
<Td width = "763" height = "86" align = "right" valign = "top">
</td>
</Tr>
<Tr>
<Td width = "763" height = "53" align = "right" valign = "bottom" background = "images/nav_bg.gif"> </td>
</Tr>
<Tr>
<Td width = "763" height = "22" align = "right" valign = "top"> </td>
</Tr>
<Tr>
& Lt; td width = "763" valign = "top" & gt;
<Table width = "100%" border = "0" cellspacing = "0" cellpadding = "0">
<Tr>
& Lt; td width = "244" valign = "top" & gt;
<Asp: ContentPlaceHolder ID = "ContentPlaceHolder1" runat = "server"> </asp: ContentPlaceHolder>
</Td>
<Td valign = "top" align = "left">
<Asp: ContentPlaceHolder ID = "ContentPlaceHolder2" runat = "server"> </asp: ContentPlaceHolder>
</Td>
</Tr>
</Table>
</Td>
</Tr>
<Tr>
<Td width = "763" height = "1" background = "http://www.68design.net/art/images/pixi_lime.gif"> </td>
</Tr>
<Tr>
<Td width = "763" height = "35" align = "center" class = "baseline">©Copy right Study. Com 2006 </td>
</Tr>
</Table>
</Div>
</Form>
</Body>

The above is the master page MasterPage. the source code of the master. the source code of aspx is very similar, for example, Web elements such as
Figure 7 shows the design-Time View of the MasterPage. master file.


Figure 7 view the master page during design

You can use Visual Studio 2005 to edit the master page, and it fully supports the "WYSIWYG" function. Whether in code or design mode, you can use Visual Studio 2005 to edit the master page, which is the same as editing a common. aspx file. The two rectangular boxes in the figure represent the ContentPlaceHolder control. Developers can add content directly in the rectangle box. The code of the set content will be included in the ContentPlaceHolder control declaration code. Note that this method is not standard. Therefore, this method is not recommended.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.