Find child controls based on its attributes?

I want to create a navigation bar for my site. This bear will have various links to pages, and the link to the page on which the user is currently located should be highlighted.

Currently I have html:

<div id="navbar" runat="server"> <a href="/" runat="server" id="lnkHome">Home</a> | <a href="~/info.aspx" runat="server" id="lnkInfo">Info</a> | <a href="~/contacts.aspx" runat="server" id="lnkContacts">Contacts</a> | <a href="~/settings.aspx" runat="server" id="lnkSettings">Settings</a> </div> 

And the code in my PageLoad event:

 //Show the currently selected page String filename = System.IO.Path.GetFileNameWithoutExtension(Request.Path).ToLower(); if (filename == "default") lnkHome.Attributes.Add("class", "selected"); else if (filename == "info") lnkInfo.Attributes.Add("class", "selected"); else if (filename == "contacts") lnkContacts.Attributes.Add("class", "selected"); else if (filename == "settings") lnkSettings.Attributes.Add("class","selected"); 

It is hard to maintain. If I want to add a link, I must provide it with an identifier and add information for it in the if statement. I want a more flexible system in which I can dynamically add links to the navigation bar and show them when the user is on the right page.

How can I do it? Is it possible to search navbar for child elements based on their href property? It would be better if these elements should not have the runat="server" attribute, therefore they can be considered as normal HTML. Or is there another implementation that I should consider?

+4
source share
1 answer

I faced many situations when I need to find a descendant or ancestor. In response to this, I wrote several extension methods that help me a lot. I would suggest using them with the following code:

Requires the use of operators:

 using System.Collections.Generic; using System.Web.UI; 

Extension Methods:

 /// <summary> /// Finds a single, strongly-typed descendant control by its ID. /// </summary> /// <typeparam name="T">The type of the descendant control to find.</typeparam> /// <param name="control">The root control to start the search in.</param> /// <param name="id">The ID of the control to find.</param> /// <returns>Returns a control which matches the ID and type passed in.</returns> public static T FindDescendantById<T>(this Control control, string id) where T : Control { return FindDescendantByIdRecursive<T>(control, id); } /// <summary> /// Recursive helper method which finds a single, strongly-typed descendant control by its ID. /// </summary> /// <typeparam name="T">The type of the descendant control to find.</typeparam> /// <param name="root">The root control to start the search in.</param> /// <param name="id">The ID of the control to find.</param> /// <returns>Returns a control which matches the ID and type passed in.</returns> private static T FindDescendantByIdRecursive<T>(this Control root, string id) where T : Control { if (root is T && root.ID.ToLower() == id.ToLower()) { return (T)root; } else { foreach (Control child in root.Controls) { T target = FindDescendantByIdRecursive<T>(child, id); if (target != null) { return target; } } return null; } } 

Your C # code:

 var fileName = Path.GetFileNameWithoutExtension(Request.Path); var controlId = "lnk" + fileName; var anchorTag = navbar.FindDescendantById<HtmlAnchor>(controlId); anchorTag.Attributes.Add("class", "selected"); 
+1
source

Source: https://habr.com/ru/post/1380226/


All Articles