How to Programmatically add CSS Stylesheet file to Asp.Net page?

27 09 2008

Sometimes its necessary to programmatically add some CSS stylesheets to your ASP.NET page,

Following is the way

protected void Page_Init(object sender, EventArgs e)


HtmlLink css = new HtmlLink();

css.Href = “css/fancyforms.css”;

css.Attributes[“rel”] = “stylesheet”;

css.Attributes[“type”] = “text/css”;

css.Attributes[“media”] = “all”;



And thats it, next time you load this page, reference to your CSS file will be added at runtime to the page HTML code and loaded.




Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: