So I have a user control that displays links and I want to change the color of the link that is currently active. This is the code inside my ListView of my usercontrol ascx file.
<asp:ListView ID="sidebarListView" runat="server" DataKeyNames="Id" DataSourceID="SqlDataSourceSidebar" OnItemCommand="sidebarListView_ItemCommand">
<ItemTemplate>
<div id="sidebarItemID" class="sidebarItem" runat="server">
<div>
<asp:LinkButton ID="NameLabel" runat="server" Text='<%# Eval("Name") %>' CommandName="Select" CommandArgument='<%# Eval("Id") %>' />
</div>
</div>
</ItemTemplate>
....
I need to change the sidebarItemID class when the linkbutton is clicked. My default.aspx code behind looks like this:
private void SideBar1_ItemCommand ( object sender , EventArgs e ) {
Int32 facId = Sidebar1.FacultyId;
SqlDataSource1.SelectCommand = "SELECT [Id], [Name], [Faculty_Id], [User_Id], [Author], [Picture], [Location] FROM [Books] WHERE [Faculty_Id]=" + facId + " ORDER BY [DateAdded] DESC";
HtmlGenericControl htmlDivControl = (HtmlGenericControl) FindControlRecursive( Sidebar1 , "sidebarItemID" );
htmlDivControl.Attributes.Add("class", "sidebarItemActive");
string newClass = htmlDivControl.Attributes["class"];
//Response.Write( String.Format( "<script>alert('{0}');</script>" , newClass ) );
}
This correctly changes the sqlDataSource based on the ID of the link clicked in the user control. However the class doesn't change.
If I uncomment the Response.Write(...) section it correctly gives an alert that says "sidebarItemActive" so it correctly finds the control from my page and assigns its class attribute as "sidebarItemActive" but nothing changes on the page if I view the page source in the browser it says the class is still "sidebarItem". What is going on here that the change does not come into effect?
htmlDivControl.Attributes["class"] = "sidebarItemActive";EDIT: That actually probably won't work. Check that the ID of the div on the page is actually sidebarItemID, because controls can append their control name to the id.