locked
MasterPage <head> content RRS feed

  • Question

  • User-956037180 posted

    How can I control and change the contents within the <head> section of the master page?  I'm using nested master pages, and I need to specify a different stylesheet for the nested master page.  How can I accomplish this?

    Master Page:

    <%@ Master Language="C#" %>
    <%@ Register Src="../UserControls/TopNavigationBar.ascx" TagName="TopNavigationBar"
        TagPrefix="uc2" %>
    <%@ Register Src="../UserControls/searchform.ascx" TagName="searchform" TagPrefix="uc1" %>
    <%@ Register Src="../UserControls/MainNavigationBar.ascx" TagName="MainNavigationBar" TagPrefix="uc3" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="Head1" runat="server">
        <title>Educational Software Solutions - Student, Curriculum Assessment & Management :: CompassLearning</title>
        <link href="../styles.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
    <form id="Form1" runat="server">
        <div id="container">

     etc...


    Nested Master Page:

    <%@ Master Language="C#" MasterPageFile="~/Templates/SiteMaster.master" AutoEventWireup="true" CodeFile="TwoColumnMaster.master.cs" Inherits="TwoColumnMaster" %>
    <%@ Register Src="../UserControls/LeftNavigationBar.ascx" TagName="LeftNavigationBar" TagPrefix="uc1" %>
    <asp:content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
    <div id="leftcolumn">
    <asp:ContentPlaceHolder id="LeftColumn" Runat="Server" />
    <uc1:LeftNavigationBar ID="LeftNavigationBar1" runat="server" /> 
    </div>
    <div id="rightcolumn">
    <asp:ContentPlaceHolder id="RightColumn" Runat="Server" />
    </div>
    </asp:content>

     

    I originally tried changing the stylesheet in the content page:


    public partial class index : System.Web.UI.Page

    {

    protected void Page_Load(object sender, EventArgs e)

    {

    Master.FindControl("MainNavigationBar1").Visible = false;

    HtmlLink cssLink = new HtmlLink();

    cssLink.Href = "~/styles2.css";

    cssLink.Attributes.Add("rel", "stylesheet");

    cssLink.Attributes.Add("type", "text/css");

    Header.Controls.Add(cssLink);

    }

    }

    That just added another stylesheet reference and didn't replace the master page stylesheet reference.

    I've read several articles to no avail.

    Can anyone help on how to achieve this?

    Monday, July 24, 2006 12:05 PM

All replies

  • User-1936818034 posted

    If you are just to change the style sheet, why not use Theme?

    Or you can try Literal control, for example:

    <link href='<asp:Literal id="css1" Text="Styles1.css" />' rel="stylesheet" type="text/css" />

    When you wanna change the style sheet, set css1.Text ="Styles2.css";

    Wednesday, July 26, 2006 11:27 PM
  • User-739157696 posted
    Here is another solution, which enables you to add as many additional stylesheets for a certain page as you desire:
     
    Make sure you give an ID to the <head> element in the MasterPage, like:
     
    <head id="masterHead" runat="server">
     
    Then in the codebehind (PageLoad) you can add the following code to add one or more stylesheets:
     
    // Add additional StyleSheet(s)
    HtmlHead headerControl = (HtmlHead)Page.Master.FindControl("masterHead");
     
    if (headerControl != null)
    {
      HtmlGenericControl newStylesheet = new HtmlGenericControl();
      newStylesheet.InnerHtml = @"<link href=""StyleSheet.css"" type=""text/css"" rel=""stylesheet"" />";
      headerControl.Controls.Add(newStylesheet);
    }
    Thursday, August 10, 2006 9:09 AM
  • User-739157696 posted
    To change the MasterPage's stylesheet you can do something similar:
     
    Make sure you give an ID to the <link> element that contains the StyleSheet, like:
     
    <link id="masterStyle" href="MasterStyleSheet.css" type="text/css" rel="stylesheet" runat="server" />
     
    Then in the codebehind (PageLoad) you can add the following code to replace the MasterPage stylesheet:
     
    // Add additional StyleSheet(s)
    HtmlLink stylesheetControl = (HtmlLink)Page.Master.FindControl("masterStyle");
     
    if (stylesheetControl != null)
    {
      stylesheetControl.Href= @"<link href=""NewStyleSheet.css"" type=""text/css"" rel=""stylesheet"" />";
    }
     
     
    PS: Just to make sure, use:
     
    using System.Web.UI.HtmlControls
     
    in both my examples :)
    Thursday, August 10, 2006 9:33 AM
  • User390817712 posted

    Another alternative is to just place a ContentPlaceHolder inside of the <head> tag, and then add a default stylesheet inside the place holder.

    If something wants to change the default stylesheet, they just need to provide an <asp:Content> control to override the default.

    See: http://odetocode.com/Blogs/scott/archive/2006/04/10/3258.aspx

    Thursday, August 10, 2006 10:47 AM
  • User457443266 posted

     Here's another way that may be a little easier.

     

            protected void Page_Init(object sender, EventArgs e)
    {
    HtmlLink link = new HtmlLink();
    link.Href = "style/stylesheet.css";
    link.Attributes.Add("rel", "stylesheet");
    link.Attributes.Add("type", "text/css");
    Page.Header.Controls.Add(link);
    }
      
    Wednesday, January 28, 2009 4:30 PM