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

N

Nathan Sokalski

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
(e-mail address removed)
http://www.nathansokalski.com/
 
N

Nathan Sokalski

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

Stan

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
(e-mail address removed)://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>
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top