Find the UnorderedList <UL> element inside the main page from the content page in asp.net
Hi guys
I want to find the UL control and then find the LI inside this UL and assign the css class on the content page ....
<ul id="mainMenu" runat="server" style="width:350px;"> <li id="mainHome" runat="server"><a title="Home" href="#" class="home">Home</a></li> <li id="mainManage" runat="server"><a title="Manage" href="#" class="manage">Manage</a></li> <li id="mainEnquiry" runat="server"><a title="Enquiry" href="#" class="enquiry">Enquiry</a></li> <li id="mainReport" runat="server"><a title="Report" href="#" class="report">Reports</a></li> </ul> If the user clicks on him, he is redirected to the users.aspx page, and I want to highlight Home LI with color ... Plz give me an offer ...
If I understood this correctly ...
If your list is on the main page ...
<ul runat="server" id="list"> <li runat="server" id="home">Home</li> <li runat="server" id="news">News</li> </ul> ... then you can do it on your content page ...
Control list = this.Page.Master.FindControl("list"); Then the li objects will be the controls in the list object - for example. list.Controls . Or you can do ...
Control home = this.Page.Master.FindControl("list").FindControl("home"); ... to search for specific list controls.
When using runat = "server" in HTML controls, the equivalent server-side object will be HtmlGenericControl .
If you want to apply the class to LI tags, you will need to make the LI object HtmlGenericControl and then use the Attributes property. For example...
HtmlGenericControl home = (HtmlGenericControl)this.Page.Master.FindControl("list").FindControl("home"); home.Attributes["class"] = "className"; Hope this helps ...
Give it a spin and let me know if it works.
In CSS, create two classes that are called:
.normalLink { background-color:#fff; } .selectedLink { background-color:#555; } In your links:
<li id="mainHome" runat="server"><a title="Home" href="users.aspx" class="<%= SetSelectedLink("users.aspx") %>">Home</a> <li id="mainManage" runat="server"><a title="Manage" href="#" class="<%= SetSelectedLink("manage.aspx") %>">Manage</a></li> In your code, per page:
If you are using the main page, execute the next bit in the main code behind, otherwise you can paste it into every regular aspx code that it needs.
public string SetSelectedLink(string linkURL) { if(Request.Url.ToLower().Contains(linkURL.ToLower()))) { return "selectedLink"; } else { return "normalLink"; } } Edit: This only works if you replace your href # with the correct URLs!