Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/35.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何修改ASP.NET中div的代码隐藏文件中的CSS样式?_C#_Asp.net_Css - Fatal编程技术网

C# 如何修改ASP.NET中div的代码隐藏文件中的CSS样式?

C# 如何修改ASP.NET中div的代码隐藏文件中的CSS样式?,c#,asp.net,css,C#,Asp.net,Css,我正试图根据从aspx页面代码背后的数据库表中获得的信息修改div的CSS样式属性。下面是我试图做的,但我会出错 Aspx: 我做错了什么?这是一个HtmlGenericControl,因此不确定推荐的方法是什么,因此您也可以这样做: testSpace.Style.Add("display", "none"); testSpace.Attributes.Add("style", "text-align: center;"); 或 或 或 另一种方法是: testSpace.Style.Ad

我正试图根据从aspx页面代码背后的数据库表中获得的信息修改div的CSS样式属性。下面是我试图做的,但我会出错

Aspx:


我做错了什么?

这是一个HtmlGenericControl,因此不确定推荐的方法是什么,因此您也可以这样做:

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

另一种方法是:

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

在vb.net中,您可以这样做:

testSpace.Style.Item("display") = "none"

如果您正在使用
新建
创建元素,可以执行以下操作:

var row = new HtmlTableRow
{
  Cells =
  {
    new HtmlTableCell
    {
        InnerText = text,
        Attributes = { ["style"] = "min-width: 35px;" }
    },
  }
};
或者如果专门使用
cssstylection

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

Add(“样式”,“显示:无;”);也可以。不太确定Robert,我认为这行将用新样式替换现有样式,而不是合并两种样式。有用的是,这将替换现有样式,例如,您可能希望完全更改类属性。我在使用
testSpace.style.Item(“display”)=“none”时遇到问题。我发现错误
“System.Web.UI.cssstylection”不包含“Item”的定义。这是特定于某个.NET版本的吗?对不起。第一种是VB.net方法。我将编辑我的答案
testSpace.Attributes["class"] = "centerIt";
testSpace.Style.Add("display", "none");
testSpace.Style["background-image"] = "url(images/foo.png)";
testSpace.Style.Item("display") = "none"
var row = new HtmlTableRow
{
  Cells =
  {
    new HtmlTableCell
    {
        InnerText = text,
        Attributes = { ["style"] = "min-width: 35px;" }
    },
  }
};
var row = new HtmlTableRow
{
  Cells =
  {
    new HtmlTableCell
    {
        InnerText = text,
        Style = { ["min-width"] = "35px" }
    },
  }
};