如何使用 C# 添加 html 样式容器?

发布于 2024-10-22 23:45:55 字数 283 浏览 6 评论 0原文

我需要在特定页面的部分中添加 HTML 样式容器,如下所示:

<style>
#mycontrol
{
    color:#ff0000;
}
</style>

虽然有很多方法可以做到这一点,但我正在考虑从 System.Web.UI.HtmlControls 命名空间实例化 HtmlControl 并简单地呈现它在页面上。但是,我只发现 HtmlGenericControl 是最接近的候选者 - 是否有更合适的控件可以使用,或者我是否必须使用其他方法?

I need to add a HTML style container in the section of a particular page, like so:

<style>
#mycontrol
{
    color:#ff0000;
}
</style>

Although there are quite a few ways of doing this I was thinking about instantiating a HtmlControl from the System.Web.UI.HtmlControls namespace and simply render it on the page. However I only found the HtmlGenericControl to be the closest candidate - is there a more suitable control I can use or do I have to use another approach?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

秋心╮凉 2024-10-29 23:45:55

尝试类似

HtmlGenericControl style = new HtmlGenericControl();
style.TagName = "style";
style.Attributes.Add("type", "text/css");
style.InnerHtml = "body{background-color:#000000;}";
Page.Header.Controls.Add(style); 

HTH

Ivo Stoykov 的东西

Try something like

HtmlGenericControl style = new HtmlGenericControl();
style.TagName = "style";
style.Attributes.Add("type", "text/css");
style.InnerHtml = "body{background-color:#000000;}";
Page.Header.Controls.Add(style); 

HTH

Ivo Stoykov

做个ˇ局外人 2024-10-29 23:45:55

你可以试试这个:

    var myStyle =
        new Style { ForeColor = System.Drawing.Color.FromArgb(255, 0, 0) };

    Page.Header.StyleSheet.CreateStyleRule(myStyle, this, ".myStyle");

you can try this:

    var myStyle =
        new Style { ForeColor = System.Drawing.Color.FromArgb(255, 0, 0) };

    Page.Header.StyleSheet.CreateStyleRule(myStyle, this, ".myStyle");
旧梦荧光笔 2024-10-29 23:45:55

您可以使用 HtmlGenericControl - 或者如果您愿意,也可以使用 Literal。

更好的方法可能是使用

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

  }

You can use an HtmlGenericControl - or you can use a Literal if you want to.

A better way might be to insert this into your HTML Header using something like this code from http://msdn.microsoft.com/en-us/library/system.web.ui.page.header.aspx

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

  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文