.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Sandeep Singh
Melody Anderson
Eminent IT

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

How to load .css file dynamically in asp.net

Posted By: priya     Posted Date: October 25, 2009    Points:2   Category :ASP.Net

How to load .css (stylesheet) file dynamically in asp.net through code in C#? i need to change for some asp.net web pages.


Author: prasath             
Posted Date: October 25, 2009     Points: 5   

Sometimes its necessary to programmatically add some CSS stylesheets to your ASP.NET page, for example if you want to allow your users to change look and feel of your website by choosing themes etc.

Here is how to do it, just add this code to the Page_Init event handler on the Page where you want to insert CSS file:

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.

Ref: http://www.dreamincode.net/forums/showtopic37326.htm

Author: elizas             
Posted Date: February 10, 2010     Points: 5   

If your application targets different user-groups, you may feel the need to customize the layout of the page to display group-specific visual interface. This can be achieved by using customized CSS files that are added dynamically.
You can add CSS files in to ASP.NET webpages dynamically. Let me show you how.
Add following HTML tag in the ASP.NET (.aspx) page in the header section.
<link id="CSSFile" type="text/css" rel="Stylesheet" runat="server" />
In the code behind page you can then add the CSS file as follows by accessing the link ID
CSSFile.Attributes.Add("href", "Dynamic.css")

(Dynamic.css is the CSS file that has been added to the page.)


Post Reply

You must Sign In To post reply
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend