locked
Removing master page at runtime RRS feed

  • Question

  • User-781951798 posted
    Hi,<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>          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.<o:p></o:p>Note: I don’t want to disturb the existing page. So no code change in the existing page. <o:p></o:p>

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

     

    Monday, October 15, 2007 7:59 AM

Answers

  • User-1426124666 posted

    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
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 15, 2007 9:09 AM
  • User1702256038 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 16, 2007 11:08 PM

All replies

  • User-1426124666 posted

    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
      
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 15, 2007 9:09 AM
  • User-781951798 posted

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

    Monday, October 15, 2007 9:38 AM
  • User1702256038 posted

    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.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, October 16, 2007 11:08 PM
  • User1484657998 posted

    Thanks a lot for your reply guys! 

    Thursday, October 29, 2009 12:03 PM
  • User-1957249028 posted
    I wanted to render the page as an excel spread sheet.   If you send html to Excel, it will automatically convert the html to Excel.

    The problem is that I did not want the content of the Master page to appear.

    Below are code parts that I used to stop the rendering of the Master page.  

    If you have done classic asp, you recognize the technique.   It works for asp.net

    Inside the code behind page I created a variable named renderExcel.    When set to true, I do not want the Master Page and some of the content page controls to appear. 

     






    <%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile="DeclarativeParams.aspx.cs" Inherits="BasicReporting_DeclarativeParams" Title="Untitled Page" %> <asp:Content ID="Content1" ContentPlaceHolderID="MainContent" Runat="Server"> <% if (renderExcel == true) // The variable is created in the Code Behind part of the page. { ViewState.Clear(); this.EnableViewState = false; Response.Clear(); // this removes all of the header content of the master page Response.ContentType = "application/ms-excel"; // We want Excel to render the page. Response.AddHeader("Content-Disposition", "attachment; filename=nerd.xls"); CountryName.Visible = false; // These are controls on the page that we do not want Excel to render. DropDownList1.Visible = false; btnRenderExcel.Visible = false; lblSelectACountry.Visible = false; } %> <!-- most of the details have been removed to show technique--> <h2>View Suppliers by Country</h2> <table align="center" style="width: 700px; background-color: #C0C0C0"> <tr> <td style="width: 140px"><asp:Label id="lblSelectACountry" runat="server" Text="Select a Country"></asp:Label></td> <td><asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="CountriesObjectDataSource" DataTextField="Country" DataValueField="Country" AutoPostBack="True" > </asp:DropDownList> <!-- When the button below is pressed, the page will render in Excel. --> &nbsp; <asp:Button ID="btnRenderExcel" runat="server" Text="Download Excel" OnClick="btnRenderExcel_Click" /> </td> </tr> <tr> <td style="width: 140px">&nbsp;</td> <td> <br /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="SupplierID" DataSourceID="ObjectDataSource1" EnableModelValidation="True"> <!--- Gridview Details removed to make this brief --> </td> </tr> </table> <%-- The code below immediately stops page rendering. Notice that this occurred after the </table tag. --%> <% if (renderExcel == true) { Response.End(); // Stops page rendering. Whatever is in the buffer is sent to the browser. } %> <%-- The asp:Content is ignored when response.end is executed. </asp:Content> The code behind part public partial class BasicReporting_DeclarativeParams : System.Web.UI.Page { public Boolean renderExcel = false; // Variable must be public for this to work. protected void Page_Load(object sender, EventArgs e) { } protected void btnRenderExcel_Click(object sender, EventArgs e) { renderExcel = true; } }


     

     

     

     

    Wednesday, January 29, 2014 3:06 PM