Click here to Skip to main content
15,173,640 members
Articles / Web Development / CSS
Posted 8 Jan 2013


13 bookmarked

Adding Styles Dynamically to ASP.NET Page Header

Rate me:
Please Sign up or sign in to vote.
4.75/5 (3 votes)
15 Jan 2013CPOL1 min read
This tip describes how to add the dynamic style to ASP.NET page header


Recently, when I was working on the ASP.NET web site, I wanted to modify the page styles dynamically. I Googled and found that there are various techniques out there for this. In this tip, I wish to share those techniques with you.

Using the Code

Following are some of the techniques out there to accomplish this.

1. By using Page.header.Controls.Add()

Page.Header property gets the document header of the page if the head element is defined with “runat=server” tag in the page declaration.The Header property gets a reference to an HtmlHead object that you can use to set document header information for the page. The HtmlHead allows you to add information such as style sheets, style rules, a title, and metadata to the head element.

You can read more about Page.Header property here.

By using a literal control, you can include the CSS file or even a style set.

Literal cssFile = new Literal()
    Text = @"<link href=""" + Page.ResolveUrl(styleFilePath) + 
	@""" type=""text/css"" rel=""stylesheet""/>"
new LiteralControl(
@"<style type='text/css'>
background: black;
border: 1px solid;

Or HtmlGenericControl can use to add the styles file:

//// Insert css file to header
HtmlGenericControl oCSS = oCSS = newHtmlGenericControl();
oCSS.TagName = "link";
oCSS.Attributes.Add("href", "css/mystyle.css");
oCSS.Attributes.Add("rel", "stylesheet");
oCSS.Attributes.Add("type", "text/css");

2. Page.Header.Stylesheet

A more elegant technique is the use of the Page.Header.Stylesheet. Instead of using the string to defend styles (like above with Literal), you can use the style properties to create this.

//// Create dynamic style rule which applies to the CSS class selector (".MyCssClass")
Style dynamicClassStyle = new Style();
dynamicClassStyle.BorderStyle = BorderStyle.Solid;
dynamicClassStyle.BorderColor = System.Drawing.Color.Black;
dynamicClassStyle.BorderWidth = new Unit(1);
dynamicClassStyle.BackColor = System.Drawing.Color.White;
Page.Header.StyleSheet.CreateStyleRule(dynamicClassStyle, null, ".MyCssClass"); 
 //// Create dynamic style rule which applies to type selector ("DIV")
Style dynamicTypeStyle = new Style();
dynamicTypeStyle.BorderStyle = BorderStyle.Solid;
dynamicTypeStyle.BorderColor = System.Drawing.Color.Black;
dynamicTypeStyle.BorderWidth = new Unit(1);
dynamicTypeStyle.BackColor = System.Drawing.Color.White;
Page.Header.StyleSheet.CreateStyleRule(dynamicTypeStyle, null, "DIV"); 

For more information, please refer to this link.

With whatever technique you follow, please remember the following hints as well,

Adding styles programmatically using the methods of the IStyleSheet interface during asynchronous postbacks is not supported. When you add AJAX capabilities to a Web page, asynchronous postbacks update regions of the page without updating the whole page.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tharaka MTR
Technical Lead Eyepax IT Consulting (Pvt) Ltd.
Sri Lanka Sri Lanka
Having more than 9 year hands-on industry experience in software development
Responsible for designing, implementing and managing complex software systems with stringent up-time requirement.

Visit my blog

Comments and Discussions

QuestionMore simple way Pin
Nasenbaaer2-Jun-15 5:25
MemberNasenbaaer2-Jun-15 5:25 
QuestionIf (Page.Header is null), how to include javascript code into? Pin
Andre Mesquita31-Oct-14 5:46
MemberAndre Mesquita31-Oct-14 5:46 
AnswerRe: If (Page.Header is null), how to include javascript code into? Pin
Austin Mullins18-Mar-15 12:59
MemberAustin Mullins18-Mar-15 12:59 
From the docs:

Gets the document header for the page if the head element is defined with a runat=server in the page declaration.

Most likely you just need to add runat=server to your <head> tag.
GeneralRe: If (Page.Header is null), how to include javascript code into? Pin
Tharaka MTR30-Mar-15 5:07
professionalTharaka MTR30-Mar-15 5:07 
GeneralMy vote of 5 Pin
dgDavidGreene17-Jan-13 8:27
MemberdgDavidGreene17-Jan-13 8:27 
GeneralRe: My vote of 5 Pin
Tharaka MTR17-Jan-13 8:41
professionalTharaka MTR17-Jan-13 8:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.