Find a UnorderedList control inside a master page from a content page in asp.net

Hai guys,

I want to find a UL control and then find a LI within that UL and assign a css class to that from a content page….

<ul id="mainMenu" runat="server" style="width:350px;">
            <li id="mainHome" runat="server"><a title="Home" href="#" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" class="home">Home</a></li>
            <li id="mainManage" runat="server"><a title="Manage" href="#" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" class="manage">Manage</a></li>
            <li id="mainEnquiry" runat="server"><a title="Enquiry" href="#" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" class="enquiry">Enquiry</a></li>
            <li id="mainReport" runat="server"><a title="Report" href="#" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" class="report">Reports</a></li>

            </ul>

If the user clicks home it is redirected to users.aspx page and i want to highlight Home LI with a color… Plz give me suggestion…

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

If I have understood this correctly…

If your list is on the master 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 this on your content page…
Control list = this.Page.Master.FindControl("list");

Then the li objects will be controls in the list object – e.g. list.Controls. Or you can do…
Control home = this.Page.Master.FindControl("list").FindControl("home");

…to find specific controls of the list control.

When using the runat=”server” on the HTML controls the server side equivalent object will be HtmlGenericControl.

If you want to apply a class to the LI tags what you would have to do is cast the LI object to a HtmlGenericControl and then use the Attributes property. For example…

HtmlGenericControl home = (HtmlGenericControl)this.Page.Master.FindControl("list").FindControl("home");

home.Attributes["class"] = "className";

Hope that helps…

Method 2

Give this a spin and let me know if it works.

In CSS, create two classes called something like:

.normalLink
{
background-color:#fff;
}

.selectedLink
{
background-color:#555;
}

In your links:
<li id="mainHome" runat="server"><a title="Home" href="users.aspx" rel="nofollow noreferrer noopener" class="<%= SetSelectedLink("users.aspx") %>">Home</a>
<li id="mainManage" runat="server"><a title="Manage" href="#" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener" class="<%= SetSelectedLink("manage.aspx") %>">Manage</a></li>

In your code behind page:

If you are using a master page, do this next bit in the master code behind, otherwise you can paste it into every regular aspx code behindthat needs it

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 proper urls!


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x