C# 我需要从代码隐藏更改列表视图中的asp:按钮
我在列表视图中有一些按钮,我需要通过代码更改它们的CSS属性,这样颜色可以通过函数动态更改 我得到以下错误:C# 我需要从代码隐藏更改列表视图中的asp:按钮,c#,asp.net,css,listview,code-behind,C#,Asp.net,Css,Listview,Code Behind,我在列表视图中有一些按钮,我需要通过代码更改它们的CSS属性,这样颜色可以通过函数动态更改 我得到以下错误: **Error 6 The name 'SubDomainButton' does not exist in the current context.** 我很高兴能够编辑当前分配给按钮的CSS类,或者引用ListView中的按钮 我想做一些像 SubDomainButton.Style.Add("border-color", (string) (Session["BorderF
**Error 6 The name 'SubDomainButton' does not exist in the current context.**
我很高兴能够编辑当前分配给按钮的CSS类,或者引用ListView中的按钮
我想做一些像
SubDomainButton.Style.Add("border-color", (string) (Session["BorderFontColor"]));
SubDomainButton.Style.Add("color", (string)(Session["BorderFontColor"]));
SubDomainButton.Style.Add("background-color", (string)(Session["BackgroundColor"]));
HTML段:
<asp:ListView ID="SubDomainListView" runat="server" DataKeyNames="ID" DataSourceID="SubDomainSqlDataSource"
EnableModelValidation="True" OnItemCommand="SubDomainListView_ItemCommand">
<itemtemplate>
<asp:Button ID="SubDomainButton" runat="server" Text='<%# Eval("SubDomain") %>' CommandText="click" CommandName="Select"
CommandArgument='<%# Eval("ID")%>' UseSubmitBehavior="False" CssClass="button" />
</itemtemplate>
<selecteditemtemplate>
<asp:Button ID="Button2" runat="server" Text='<%# Eval("SubDomain") %>' CommandText="click" CommandName="Select"
CommandArgument='<%# Eval("ID")%>' UseSubmitBehavior="False" CssClass="buttoninact" Enabled="False" />
</selecteditemtemplate>
<layouttemplate>
<div id="itemPlaceholder" runat="server">
</div>
</layouttemplate>
</asp:ListView>
您可以通过添加内联CSS样式来实现这一点:
<itemtemplate>
<asp:Button ID="SubDomainButton" runat="server" Text='<%# Eval("SubDomain") %>' CommandText="click" CommandName="Select" CommandArgument='<%# Eval("ID")%>' UseSubmitBehavior="False" CssClass="button"
style='<%# String.Format("border-color: {0}; color: {1}; background-color: {2};",(string)Session["BorderFontColor"],(string)Session["BorderFontColor"],(string)Session["BorderFontColor"]) %>' />
</itemtemplate>
看看这篇文章。我得到以下信息:错误6当前上下文中不存在名称“SubDomainButton”。我想这是因为它在listview中?这是因为id在页面呈现时得到前缀。将属性ClientIDMode设置为“static”,它应该可以工作。很抱歉,出现了错误:错误6“object”不包含“Attributes”的定义,并且找不到接受类型为“object”的第一个参数的扩展方法“Attributes”(是否缺少using指令或程序集引用?),我忘了键入b变量。尝试添加(按钮),就像我在编辑中所做的那样。希望它能起作用。这似乎没有改变什么。
<itemtemplate>
<asp:Button ID="SubDomainButton" runat="server" Text='<%# Eval("SubDomain") %>' CommandText="click" CommandName="Select" CommandArgument='<%# Eval("ID")%>' UseSubmitBehavior="False" CssClass="button"
style='<%# String.Format("border-color: {0}; color: {1}; background-color: {2};",(string)Session["BorderFontColor"],(string)Session["BorderFontColor"],(string)Session["BorderFontColor"]) %>' />
</itemtemplate>