Inherit master page?

Discussion in 'ASP .Net' started by =?Utf-8?B?TWF0dA==?=, Jul 3, 2007.

  1. 1. I have a master page. Is it possible to create another master page inherit
    the existed one?

    2. Is it possible to inject css link, js script block to the head part of
    web content page?
    =?Utf-8?B?TWF0dA==?=, Jul 3, 2007
    #1
    1. Advertising

  2. "Matt" <> wrote in message
    news:...
    > 1. I have a master page. Is it possible to create another master page
    > inherit
    > the existed one?


    How much do you love design view? Technically, it is possible, but it does
    not render right now.

    > 2. Is it possible to inject css link, js script block to the head part of
    > web content page?


    If you have a content area in the head, you can attach js, etc. to it. You
    can also inject into the master header.

    --
    Gregory A. Beamer
    MVP; MCP: +I, SE, SD, DBA
    http://gregorybeamer.spaces.live.com
    Co-author: Microsoft Expression Web Bible (upcoming)

    ************************************************
    Think outside the box!
    ************************************************
    Cowboy \(Gregory A. Beamer\), Jul 3, 2007
    #2
    1. Advertising

  3. > 1. I have a master page. Is it possible to create another master page
    > inherit the existed one?


    Using Visual Studio 2005 and ASP.NET 2.0? Not "really". You'll lose the
    designer view, but if you're fine with dealing with the code, there's a way.

    If you create both of your master pages and then, in an inherited "defaultPage",
    you can code it up to pass the master page as a parameter.

    private string runtimeMasterPageFile;

    public string RuntimeMasterPageFile
    {
    get { return runtimeMasterPageFile; }
    set { runtimeMasterPageFile = value; }
    }

    protected override void OnPreInit(EventArgs e)
    {
    if (runtimeMasterPageFile != null)
    {
    MasterPageFile = runtimeMasterPageFile;
    }

    base.OnPreInit(e);
    }

    So, on the page that would inherit both, your Page tag would look like below.
    In this example, I have a Default.master page, but also a Secondar.master
    page.

    <%@ Page Language="C#" CodeFileBaseClass="DefaultPage" MasterPageFile="~/Default.master"
    RuntimeMasterPageFile="~/Secondary.master"
    AutoEventWireup="true" CodeFile="ViewStudent.aspx.cs"
    Inherits="ViewStudent" Title="Untitled Page" Async="true" %>

    The code for this was snatched up from Scott Guthrie's blog at http://weblogs.asp.net/scottgu/archive/2005/11/11/430382.aspx
    and has worked great over the past couple of years.

    Now, if you're looking forward or experimenting with the new Visual Studio
    2008 and .NET 3.0/3.5, MasterPages CAN use other MasterPages. The Page tag
    is rendered as below. When you create a new master page, it has the option,
    just like any other page, to select a master page.

    <%@ Master Language="C#" MasterPageFile="~/ParentMasterPage.master" AutoEventWireup="false"
    CodeFile="ChildMasterPage.master.cs" Inherits="ChildMasterPage" %>

    > 2. Is it possible to inject css link, js script block to the head part
    > of web content page?


    Explore the Page.Header object and it's attributes. For example, for your
    CSS link:

    HtmlLink customStyleSheet = new HtmlLink();
    customStyleSheet.Href = "StyleSheet.css";
    customStyleSheet.Attributes.Add("type", "text/css");
    customStyleSheet.Attributes.Add("rel", "stylesheet");
    Page.Header.Controls.Add(customStyleSheet);


    If nothing else, you can add a <div> with an id and runat=server attribute
    and drop controls, or CSS in, with literal controls.

    <head runat="server">
    <div id="MyCustomContent" runat="server" />
    </head>

    Then, in code behind:

    protected void Page_Load(object sender, EventArgs e)
    {
    MyCustomContent.InnerHtml = @"<link href='StyleSheet.css' rel='stylesheet'
    type='text/css' />";
    }

    You could do the same with a <script> tag, I'm assuming..

    HTH.

    -dl

    --
    David R. Longnecker
    http://blog.tiredstudent.com

    > 1. I have a master page. Is it possible to create another master page
    > inherit the existed one?
    >
    > 2. Is it possible to inject css link, js script block to the head part
    > of web content page?
    >
    David R. Longnecker, Jul 3, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug McCann
    Replies:
    1
    Views:
    993
    William Brogden
    Aug 5, 2004
  2. Replies:
    1
    Views:
    3,190
    PeterKellner
    May 16, 2006
  3. Zeba
    Replies:
    1
    Views:
    480
    Mark Rae
    Feb 22, 2007
  4. Nathan Sokalski
    Replies:
    25
    Views:
    1,410
    Nathan Sokalski
    Mar 4, 2010
  5. SerpentKiss2010
    Replies:
    0
    Views:
    2,470
    SerpentKiss2010
    Apr 6, 2011
Loading...

Share This Page