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>