I’m having some trouble with my main menu on my ASP.NET website. What I want is, when a user clicks on a link, it should change it’s color to black and remain black until the user clicks on the other links.
In other words: I want to highlight the selected link.
So far I’ve got it working using some workaround JavaScript (should consider using jQuery instead I think), but the problem is, that the style dosn’t remain on page postback (or so it seems..)
My current javascript:
<script type="text/javascript">
var last = "none";
function LinkSelector(link) {
if (last != "none") {
document.getElementById(last).className = "NormalLink";
}
document.getElementById(link).className = "ActiveLink";
last = link;
}
</script>
As I wrote, it sure changes the class name when clicked, but dosn’t remain that way when the new page is loaded.
Anyone got a workaround on this? 🙂
Thanks in advance.
All the best,
Bo
Typically, I’ll use the ASP:Menu control. It provides a decent amount of control over the menu items in it.
For example, here’s a generic method that I dump in a masterpage and run during the initial page hit. This method loops over the menu items in the menu control, and selects the menu item that corresponds to the current URL.
Here are a few of the menu items I have in my asp menu control. Note that I use the Value attribute to help me indicate which menu item pertains to the requested URL in the method above.