fred1234
fred1234

Reputation: 429

I need to change asp:buttons that are in a ListView from code behind

I have some buttons in list view and I need to change their CSS attributes through code behind so the color can be changed dynamically through functions.

I get the following error:

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

I would be happy being able to edit the CSS Class currently assigned to the buttons or reference the Button that is inside the ListView.

I want to do something like...

SubDomainButton.Style.Add("border-color", (string)    (Session["BorderFontColor"]));
SubDomainButton.Style.Add("color", (string)(Session["BorderFontColor"]));
SubDomainButton.Style.Add("background-color", (string)(Session["BackgroundColor"]));

HTML Segment:

<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 Styles */

.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;
}

Upvotes: 0

Views: 995

Answers (1)

filipko
filipko

Reputation: 965

You can do it by adding inline CSS styles:

<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>

Upvotes: 1

Related Questions