Dynamically Add CSS Via C# Code

Akiii
Posted by Akiii under C# category on | Points: 40 | Views : 4097
Add the below code in the page load section of your aspx page :-


HtmlHead head = (HtmlHead)Page.Header;
HtmlLink link = new HtmlLink();
link.Attributes.Add("href", Page.ResolveClientUrl("myStyleSheet.css "));
link.Attributes.Add("type", "text/css");
link.Attributes.Add("rel", "stylesheet");
head.Controls.Add(link);


As soon as you add the above code, the visual studio intellisense will show error as the HtmlHead and the HtmlLink classes does not exist in the current context.
Then add a using statement :

using System.Web.UI.HtmlControls; 


"myStyleSheet.css" is the name of my stylesheet, you can name it anything you want....


I hope this helps....

Thanks and Regards
Akiii

Comments or Responses

Posted by: SheoNarayan on: 2/2/2012 Level:HonoraryPlatinum | Status: [Administrator] | Points: 10
Good, code. Keep it up!

Thanks
Posted by: Akiii on: 2/2/2012 Level:Bronze | Status: [Member] | Points: 10
Thank you very much Sir......

Regards
Akiii

Login to post response