Removing master page at runtime

Last post 10-16-2007 11:08 PM by Amanda Wang - MSFT. 3 replies.

Sort Posts:

  • Removing master page at runtime

    10-15-2007, 7:59 AM
    Hi,          I have a page with master page developed earlier and it is working fine. Now I want the same page to be used in some other module where I don’t want the master page, only the page has to be displayed. I don’t want the code to be repeated here by creating a new page and copying the old code, instead I want to call the page and the master page has to be removed runtime.Note: I don’t want to disturb the existing page. So no code change in the existing page.

    Has anyone faced this kind of situation? Any help on this will be highly appreciated.

     

  • Re: Removing master page at runtime

    10-15-2007, 9:09 AM
    Answer
    • Loading...
    • ca8msm
    • Joined on 11-29-2005, 3:57 PM
    • http://aspnetlibrary.com
    • Posts 2,140

    It's probably easier to create a second blank Master Page so you can set it to use that rather than trying to remove all references to a Master Page. You can then set it like this:

     

        Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
            Page.MasterPageFile = "othermasterpage.master"
        End Sub
      

    Mark,
    Join me at the new IT Community of the 21st Century: http://lessthandot.com

    Please remember to click "Mark as Answer" on this post if it helped you
  • Re: Removing master page at runtime

    10-15-2007, 9:38 AM

    ca8msm:

    It's probably easier to create a second blank Master Page so you can set it to use that rather than trying to remove all references to a Master Page. You can then set it like this:

     

        Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
            Page.MasterPageFile = "othermasterpage.master"
        End Sub

      

    if my page1.aspx had materpage1. master and i am calling this page1.aspx from page2.aspx, then where should i use your code? to change the blankmaster.master. since i said i dont want to touch the old page that is page1.aspx in this example how to chage the master page of page1 from page2.

    Edited: can you please explain in C#.

  • Re: Removing master page at runtime

    10-16-2007, 11:08 PM
    Answer

    Hi,

    Base on your description, you want the content page to remove the master page at runtime,that is to as say, do not use the master page at runtime, right?

    But if the content page used the master page, the aspx file will like this:

    <%@ Page Language="C#" MasterPageFile="~/TestCases/DEFAULT.master" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="TestCases_Default" Title="Untitled Page" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="Main" Runat="Server">
    </asp:Content>

    So it is very different with the normall page, so the content page only can work fine with the master page, or else it will throw the error message.

    About how the master page and the content page work, you can read this article: http://www.odetocode.com/Articles/450.aspx

    But you can try to implement it by using the flexible method.

    For exampe:

    You can create a empty master page, there is nothing other than the contentplaceholder control and the html tags. So if the content page use this master page, the page only display itself.  At the runtime. change the content page's masterpagefile in the PreInit event programmatically.

    If you do like this, will not disturb the other existed pages.

    for example:

    1. the empty master page:

    <%@ Master Language="C#" AutoEventWireup="true" CodeFile="MasterPage.master.cs" Inherits="TestCases_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>

     2. Set the content page's masterpagefile  programatically

    void BasePage_PreInit(object sender, EventArgs e)
        {
            MasterPageFile =
    "~/MasterPage.master";
        }

    Hope it helps.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Yours sincerely,
    Amanda Wang
    Microsoft Online Community Support
Page 1 of 1 (4 items)
Microsoft Communities
Page view counter