Adding to the style of the Master page from a Content page

Discussion in 'ASP .Net' started by Nathan Sokalski, Aug 18, 2008.

  1. I have a content page from which I need to add a style rule to be used by
    the page. Since content pages only have the content tags, I obviously need
    to edit the Master page's style using code. I am assuming this will be done
    in the PreInit eventhandler, but i cannot find the right way to do this. Can
    someone help me? Thanks.

    Nathan Sokalski

    http://www.nathansokalski.com/
    Nathan Sokalski, Aug 18, 2008
    #1
    1. Advertising

  2. I tried that, but I could not figure out how to add the CSS properties I
    wanted to the Style object (such as padding), since it doesn't have
    properties for them, and I couldn't figure out what property to use. Can you
    tell me how to set CSS properties that the Style object does not have
    properties for? Thanks.

    "Mark Rae [MVP]" <> wrote in message
    news:%...
    > "Nathan Sokalski" <> wrote in message
    > news:...
    >
    >> Can someone help me?

    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    > Style objStyle = new Style();
    > objStyle.ForeColor = System.Drawing.Color.Navy;
    > objStyle.BackColor = System.Drawing.Color.LightGray;
    > this.Header.StyleSheet.CreateStyleRule(objStyle, null, "body");
    > }
    >
    >
    > --
    > Mark Rae
    > ASP.NET MVP
    > http://www.markrae.net
    Nathan Sokalski, Aug 18, 2008
    #2
    1. Advertising

  3. Nathan Sokalski

    Stan Guest

    On 18 Aug, 15:27, "Nathan Sokalski" <>
    wrote:
    > I have a content page from which I need to add a style rule to be used by
    > the page. Since content pages only have the content tags, I obviously need
    > to edit the Master page's style using code. I am assuming this will be done
    > in the PreInit eventhandler, but i cannot find the right way to do this. Can
    > someone help me? Thanks.
    >
    > Nathan Sokalski
    > ://www.nathansokalski.com/


    hi Nathan
    It depends on which version of Framework you are working with. With
    version 3.5 MasterPages have a ContentPlaceholder in the Header
    section so would make your task it very simple. With earlier versions
    you have to do it programmatically using the Header property of the
    page. The following example is from the Class library help files (note
    that it works just as well with content pages):

    <%@ Page Language="C#" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://
    www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <script runat="server">

    void Page_Load(object sender, EventArgs e)
    {
    if (Page.Header != null)
    {
    // Create a Style object for the <body> section of the Web page.
    Style bodyStyle = new Style();

    bodyStyle.ForeColor = System.Drawing.Color.Blue;
    bodyStyle.BackColor = System.Drawing.Color.LightGray;

    // Add the style to the header of the current page.
    Page.Header.StyleSheet.CreateStyleRule(bodyStyle, this, "BODY");

    // Add text to the label2 control to see the style rules applied
    to it.
    label1.Text = "This is what the bodyStyle looks like.";
    }
    }

    </script>

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head id="head1" runat="server">
    <title>IStyleSheet Example</title>
    </head>
    <body>
    <form id="form1" runat="server">
    <h1>IStyleSheet Example</h1>
    <asp:Label
    id="label1"
    runat="server">
    </asp:Label>
    </form>
    </body>
    </html>
    Stan, Aug 21, 2008
    #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:
    0
    Views:
    710
  3. Rolf Welskes
    Replies:
    7
    Views:
    946
    Steven Cheng[MSFT]
    Aug 22, 2006
  4. John
    Replies:
    2
    Views:
    899
  5. Nathan Sokalski

    Adding to the style of the Master page from a Content page

    Nathan Sokalski, Aug 18, 2008, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    186
Loading...

Share This Page