[英]Add css class from code behind using literal control
I am adding a css class to my aspx page from c# code behind. 我正在从后面的C#代码向我的aspx页面添加一个CSS类。 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? 有没有办法在不使用文字控制的情况下做到这一点?
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);
}
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 在Asp.Net中以编程方式添加StyleSheets
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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.