[英]How do I add a html style container using C#?
我需要在特定页面的部分中添加HTML样式容器,如下所示:
<style>
#mycontrol
{
color:#ff0000;
}
</style>
尽管有很多方法可以做到这一点,但我仍在考虑从System.Web.UI.HtmlControls命名空间实例化HtmlControl并将其简单呈现在页面上。 但是,我只发现HtmlGenericControl是最接近的候选者-是否可以使用更合适的控件,还是必须使用另一种方法?
尝试类似
HtmlGenericControl style = new HtmlGenericControl();
style.TagName = "style";
style.Attributes.Add("type", "text/css");
style.InnerHtml = "body{background-color:#000000;}";
Page.Header.Controls.Add(style);
高温超导
伊沃·斯托伊科夫(Ivo Stoykov)
您可以尝试以下方法:
var myStyle =
new Style { ForeColor = System.Drawing.Color.FromArgb(255, 0, 0) };
Page.Header.StyleSheet.CreateStyleRule(myStyle, this, ".myStyle");
您可以使用HtmlGenericControl-或根据需要使用Literal。
更好的方法可能是使用http://msdn.microsoft.com/zh-CN/library/system.web.ui.page.header.aspx中的以下代码将其插入到HTML标头中
protected void Page_Load(object sender, System.EventArgs e)
{
// Create a Style object for the body of the page.
Style bodyStyle = new Style();
bodyStyle.ForeColor = System.Drawing.Color.Blue;
bodyStyle.BackColor = System.Drawing.Color.LightGray;
// Add the style rule named bodyStyle to the header
// of the current page. The rule is for the body HTML element.
Page.Header.StyleSheet.CreateStyleRule(bodyStyle, null, "body");
// Add the page title to the header element.
Page.Header.Title = "HtmlHead Example";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.