Reputation: 1740
I am adding a css class to my aspx page from c# code behind. here's how I am going around
public void OnPreRenderComplete(EventArgs e)
{
Page.Header.Controls.Add(new LiteralControl("<link rel=\"stylesheet\" type=\"text/css\" href=\"/Content/Css/Edit.css"+"\" />"));
}
The code is working as expected. Now the thing is that I DO NOT want to use literal control to add the class if possible. Is there a way around to do the same without using literal control?
Upvotes: 2
Views: 6937
Reputation: 8736
try this
protected void Page_Init(object sender, System.EventArgs e)
{
HtmlGenericControl css;
css = new HtmlGenericControl();
css.TagName = "style";
css.Attributes.Add("type", "text/css");
css.InnerHtml = "@import \"/foobar.css\";";
Page.Header.Controls.Add(css);
}
Upvotes: 3
Reputation: 24322
Try this code,
HtmlLink link = new HtmlLink();
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("type", "text/css");
link.Href = "/Content/Css/Edit.css";
this.Page.Header.Controls.Add(link);
Upvotes: 1
Reputation: 590
Someone already raised this question and this may help you:
HtmlLink link = new HtmlLink();
//Add appropriate attributes
link.Attributes.Add("rel", "stylesheet");
link.Attributes.Add("type", "text/css");
link.Href = "/Resources/CSS/NewStyles.css";
link.Attributes.Add("media", "screen, projection");
//add it to page head section
this.Page.Header.Controls.Add(link);
Adding StyleSheets Programmatically in Asp.Net
Upvotes: 3