How do you modify a CSS style in the code behind file for divs in ASP.NET?

前端 未结 4 1491
醉酒成梦
醉酒成梦 2020-12-01 00:21

I\'m trying to modify a CSS style attribute for a div based on the information I get from a database table in the code behind of my aspx page. The following is essentially w

相关标签:
4条回答
  • 2020-12-01 01:05

    If you're newing up an element with initializer syntax, you can do something like this:

    var row = new HtmlTableRow
    {
      Cells =
      {
        new HtmlTableCell
        {
            InnerText = text,
            Attributes = { ["style"] = "min-width: 35px;" }
        },
      }
    };
    

    Or if using the CssStyleCollection specifically:

    var row = new HtmlTableRow
    {
      Cells =
      {
        new HtmlTableCell
        {
            InnerText = text,
            Style = { ["min-width"] = "35px" }
        },
      }
    };
    
    0 讨论(0)
  • 2020-12-01 01:06

    It's an HtmlGenericControl so not sure what the recommended way to do this is, so you could also do:

    testSpace.Attributes.Add("style", "text-align: center;");
    

    or

    testSpace.Attributes.Add("class", "centerIt");
    

    or

    testSpace.Attributes["style"] = "text-align: center;";
    

    or

    testSpace.Attributes["class"] = "centerIt";
    
    0 讨论(0)
  • 2020-12-01 01:13
    testSpace.Style.Add("display", "none");
    
    0 讨论(0)
  • 2020-12-01 01:21

    Another way to do it:

    testSpace.Style.Add("display", "none");
    

    or

    testSpace.Style["background-image"] = "url(images/foo.png)";
    

    in vb.net you can do it this way:

    testSpace.Style.Item("display") = "none"
    
    0 讨论(0)
提交回复
热议问题