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?
source share