Skip to content

Programatically adding a CSS reference to the page

Yesterday, I ran into a situation where I wanted to add a css reference to the page during run time(programatically) through the code behind. Did you know that .Net provides a very simple and straight way to get it done? Atleast I did not till this point of time.


public static void AddHeaderStyleSheet(Page _Page, string _Path)
{

HtmlLink LobjCss = new HtmlLink(); LobjCss.Href = _Path; LobjCss.Attributes[“rel”] = “stylesheet”; LobjCss.Attributes[“type”] = “text/css”; _Page.Header.Controls.Add(LobjCss);


}

The same approach can also be used to attach the script files etc.

Published in.NETASP.NetTips

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *