我需要从后面的代码更改ListView中的asp:按钮

时间:2013-05-06 17:10:04

标签: c# asp.net css listview code-behind

我在列表视图中有一些按钮,我需要通过后面的代码更改其CSS属性,以便可以通过函数动态更改颜色。

我收到以下错误:

**Error 6 The name 'SubDomainButton' does not exist in the current context.**

我很高兴能够编辑当前分配给按钮的CSS类或引用ListView内的Button。

我想做点像......

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:     / *按钮样式* /

.button {
-webkit-box-shadow: rgba(0,0,0,0.0.1) 0 1px 0 0;
-moz-box-shadow: rgba(0,0,0,0.0.1) 0 1px 0 0;
box-shadow: rgba(0,0,0,0.0.1) 0 1px 0 0;
border: 1px solid;
font-family: Lucida Grande,Tahoma,Verdana,Arial,sans-serif;
font-size: 12px;
font-weight: 700;
padding: 2px 6px;
height: 28px;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
text-decoration: underline;
}

/*Hover Styles*/
.button:hover {
-webkit-box-shadow: rgba(0,0,0,0.0.1) 0 1px 0 0;
-moz-box-shadow: rgba(0,0,0,0.0.1) 0 1px 0 0;
box-shadow: rgba(0,0,0,0.0.1) 0 1px 0 0;
border: 1px solid;
font-family: Lucida Grande,Tahoma,Verdana,Arial,sans-serif;
font-size: 12px;
font-weight: 700;
padding: 2px 6px;
height: 28px;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
text-decoration: none;
}

.buttoninact {
-webkit-box-shadow: rgba(0,0,0,0.0.1) 0 1px 0 0;
-moz-box-shadow: rgba(0,0,0,0.0.1) 0 1px 0 0;
box-shadow: rgba(0,0,0,0.0.1) 0 1px 0 0;
border: 1px solid;
font-family: Lucida Grande,Tahoma,Verdana,Arial,sans-serif;
font-size: 12px;
font-weight: 700;
padding: 2px 6px;
height: 28px;
border-radius: 5px;
-moz-border-radius: 5px;
-webkit-border-radius: 5px;
text-decoration: none;
}

1 个答案:

答案 0 :(得分:1)

您可以通过添加内联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>