2

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?

3 Answers 3

3

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);
}
Sign up to request clarification or add additional context in comments.

Comments

3

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

Comments

1

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.