MasterPage <head> content

Last post 01-28-2009 4:30 PM by ggamble. 5 replies.

Sort Posts:

  • MasterPage <head> content

    07-24-2006, 12:05 PM
    • Member
      650 point Member
    • Dman100
    • Member since 07-11-2006, 7:06 PM
    • Posts 408

    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?

  • Re: MasterPage <head> content

    07-26-2006, 11:27 PM
    • Contributor
      4,635 point Contributor
    • Gordon-Freeman
    • Member since 07-17-2006, 8:19 AM
    • SH, PRC
    • Posts 909

    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";

    你好! Just FYI o_O
  • Re: MasterPage <head> content

    08-10-2006, 9:09 AM
    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);
    }
  • Re: MasterPage <head> content

    08-10-2006, 9:33 AM
    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 :)
  • Re: MasterPage <head> content

    08-10-2006, 10:47 AM
    • Contributor
      6,537 point Contributor
    • bitmask
    • Member since 07-29-2003, 3:18 PM
    • Citizen of the Earth
    • Posts 1,228
    • TrustedFriends-MVPs

    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

    Scott
    http://www.OdeToCode.com/blogs/scott/
  • Re: MasterPage <head> content

    01-28-2009, 4:30 PM
    • Member
      46 point Member
    • ggamble
    • Member since 12-30-2008, 5:34 PM
    • Olympia
    • Posts 31

     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);
    }
      
Page 1 of 1 (6 items)