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?

4

3 回答 3

3

尝试这个

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);
}
于 2013-06-17T05:45:14.730 回答
3

有人已经提出了这个问题,这可能会对您有所帮助:

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

在 Asp.Net 中以编程方式添加样式表

于 2013-06-17T05:23:02.303 回答
1

试试这个代码,

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);
于 2013-06-17T05:23:17.057 回答