Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello,

I have an asp.net webform application that has a link button inside a listview.
What I want to do is to highlight/change color of the selected button on Itemcommand.
I have tried a lot of solutions with no luck. It changes the color but it doesn't change back when i click another button.

Any help will be so much appreciated.
Thanks
Samira

What I have tried:

I tried to change color of the selected/clicked button on ItemCommand , ItemDataBound and button click.
Link button inside listview:
<asp:ListView runat="server" ID="subs_view" DataSourceID="SubsFilterSqlDataSource" OnItemCommand="subs_view_ItemCommand" OnItemDataBound= "subs_view_ItemDataBound">
 <ItemTemplate>                                                       
    <asp:LinkButton  cssClass="col-md-9 link-btn" runat="server" ID="btnSubs" CommandName="subs" CommandArgument='<%#Eval("code") %>' Text='<%#Eval("SubCat_CountItems") %>' OnClick="btnSubs_Click"> </asp:LinkButton>
 </ItemTemplate>
</asp:ListView>

I tried to change colors in different ways:
protected void subs_view_ItemCommand(object sender, ListViewCommandEventArgs e)
        {   
            if (e.CommandName == "subs")
            {
                
                LinkButton linkbtn = (LinkButton)e.Item.FindControl("btnSubs");                
                linkbtn.ForeColor = System.Drawing.Color.Red;
//the color stays red when clicking another button
            }
        }


protected void btnSubs_Click(object sender, EventArgs e)
        {
            //subs_view.DataBind();
            //LinkButton mybtn = (LinkButton)sender;
            ////mybtn.ForeColor = System.Drawing.Color.Black;
            //mybtn.ForeColor = System.Drawing.Color.Red;
        }

--
protected void subs_view_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            //LinkButton btnSubs = (LinkButton)e.Item.FindControl("btnSubs");
            //btnSubs.ForeColor = System.Drawing.Color.Black;
        }
Posted
Updated 29-May-17 7:34am

1 solution

You can try to set the
ViewStateMode="Disabled"
on the ListView control. That case on button click, post back happen, the original color will be restored using the default color instead of the color saved in ViewState.
 
Share this answer
 
Comments
Samira Radwan 29-May-17 15:23pm    
That works. Thanks a lot!!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900