【问题标题】:send <style> info from custom server asp element从自定义服务器 asp 元素发送 <style> 信息
【发布时间】:2012-11-01 19:38:11
【问题描述】:
我想发送以在样式块中输出 CSS 信息
protected override void RenderContents(HtmlTextWriter output)
{
...
output.Write("<style> .... ");
}
但是这个块不能嵌套在div块中。我必须把它放在head中。我该怎么做或者有其他方法吗?
【问题讨论】:
标签:
asp.net
custom-server-controls
【解决方案1】:
this.Page.Header.Stylesheet.CreateStyleRule 方法允许您向<head> 添加样式。但是,您可以指定的 CSS 属性仅限于 Style 类支持的属性。 (如果您需要其他属性,可以从 Style 派生您自己的类。)
C# 示例:
protected override void OnPreRender(EventArgs e)
{
base.OnPreRender(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.
this.Page.Header.StyleSheet.CreateStyleRule(bodyStyle, null, "body");
}
【解决方案2】:
直接从System.Web.UI.Control 派生并覆盖Render 方法,而不是从(我假设是)System.Web.IO.WebControls.WebControl 派生。