开发者

How to assign a master page to a existing .aspx page?

How to assign a master page to a existi开发者_如何学Gong .aspx page ?


Add this to your existing aspx Page declaration:

MasterPageFile="~/SomeMasterPage.Master"

Should also mention that to add page specific content to your Page you also need to add any Content tags (defined in your Master Page) to your aspx page also:

 <asp:Content ID="myContent" ContentPlaceHolderID="someContent" runat="server">

      // Page Content goes here

 </asp:Content>

If you don't put these in then the default content from your Master Page is used instead.


When assigning an existing .aspx page to a master page all the basic elements of a web-page now resides with the master page like head, body, title etc. Hence if your page is using these elements for purpose like giving title to the webpage, defining javascripts in the tag... all these operations now need to be done on the master page. But if you need to assign these elements the respective values at child - page level then you need to define a content place holder in the head tag of the master page. Another content place holder should be placed within the body of the master-page.

At the child-page you will be having two content place holders, one for head and other for body. After which you will need to put the needful code in the respective content place holders.


Using the page level directive in an aspx page:

<%@ page language="C#" masterpagefile="~/Default.Master" codebehind="..." inherits="..." title="..." %>


  1. Add a new master page to your project.
  2. Open your existing .aspx page.
  3. Remove the HTML markup from to the end of .
  4. Remove the HTML markup from to the end of .
  5. Add MasterPageFile="MasterPage.master" to the "@ Page" directive.
  6. Add directly below the "@ Page" directive, where "ContentPlaceHolderID1" is the ID that you defined in your master page: .
  7. Add to the bottom of your script file.

MasterPage.master:

    <%@ 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 runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
        </asp:contentplaceholder>
    </div>
    </form>
</body>
</html>

Default.aspx:

    <%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    Content goes here
</asp:Content>

Hope this will help Thank you...


All of these answers are excellent especially HotTester's!

I want to emphasize details essencial when converting an already existing aspx page to a content page: remove html and head tags and their contents except body tag - remove the body tags BUT KEEP its contents - remove the form tag BUT KEEP its contents. As explained here Convert ASPX pages to content pages?. Then place that content in the content place holder of the body on the child page.

If you have content in the head tag that must remain in the content page and not be placed in the master page, remove the head tag but KEEP its contents and place the contents in the content place holder of the master page head tag.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜