.net 将动态dotnet webcontrol添加到静态html的属性值中

.net 将动态dotnet webcontrol添加到静态html的属性值中,.net,.net,很容易解释。我有没有办法做到这一点: <div id="header" style='<asp:Literal runat="server" ID="litBackgroundImage"></asp:Literal>' > 它看起来有效,但Visual Studio不会将litBackgroundImage识别为代码隐藏中的有效控件。将div设置为runat=“server”也不起作用,因为style属性是只读的 感激地收到的任何建议将runat=“s

很容易解释。我有没有办法做到这一点:

<div id="header" style='<asp:Literal runat="server" ID="litBackgroundImage"></asp:Literal>' >

它看起来有效,但Visual Studio不会将litBackgroundImage识别为代码隐藏中的有效控件。将div设置为runat=“server”也不起作用,因为style属性是只读的

感激地收到的任何建议

runat=“server”
添加到您的div中,然后您可以从服务器端访问它,这样您就可以动态设置
样式
属性。

可能会起作用:

HtmlControl headerDiv = (HtmlControl)this.FindControl("header");
headerDiv.Attributes.Add("class", **NAMEOFCSSCLASSTOUSE**);
我还没有测试过,但应该可以用。您必须确保您的样式是css文件中的css类

编辑

测试后,上述方法不起作用,但以下方法对我很有效:

<div id="header3" runat="server">This is header3 ...</div>

您是否将文字嵌套在
标记中?这是一个显而易见的答案,但值得一提。

试试这个

<div id="div1" runat="server">...</div>

正如我上面所说,您不能动态设置样式-它是只读的。@Matt,我想BlueCode说的是使用Attributes属性而不是style属性。
<div id="div1" runat="server">...</div>
div1.Style["width"] = "100px";
div1.Style["color"] = "#FF0000";