How to assign a master page to a existing .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.


  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...


Using the page level directive in an aspx page:

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

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.

Tags:

.Net

Asp.Net