
This little quick tip is nothing new or groundbreaking, but I know that when I first started out with .NET it stumped me for a bit. Let’s learn how to select that active navigation item with C# in a master page
This little quick tip is nothing new or groundbreaking, but I know that when I first started out with .NET it stumped me for a bit. Let’s learn how to select that active navigation item with C# in a master page
Here’s the code in my Master Page, nothing too complex, just a simple UL with links. Notice that each link has a unique ID:
Here’s the code from my code behind on my Default.aspx.cs (This page loads the master file mentioned above)
protected void Page_Load(object sender, EventArgs e) { var selectedNav = (HyperLink)Master.FindControl("home"); selectedNav.CssClass = "selected"; }
The above code will look through the master page and find the control with the id of “home” and apply the css class of “selected”. You can then target this selected item with CSS to indicate an active page state.
You can use the Master.FindControl to get access to pretty much anything that is in your master page, you’re not limited to Hyperlinks.
14 Responses
[...] up on my C# Friday quick tip on selecting navigation in master pages with C# .net, here’s another equally slick tip, how to detect an iPhone using [...]
Well, I don’t know about C# that much but I willing to give it a try.
I’ve tried to work with C# but seems to be more difficult than .php language, for example. I will try to use this code for one of my personal projects. I hope this time will be more easy to understand because the code is clean.
The community of suppliers of goods and services however, the exact definition of business, like much else in the philosophy of business, is a matter of debate and complexity of meanings.Thanks for sharing the informative post. Regards.
Lionel Andrés Messi is an Argentinian contestant who currently plays for Attack and the Argentinian lie grouping. Thermostated one of the top contestant players of his gentility.
There grouping been any tallish stories of existing against all tbe ratio and magical valiance. But there are pol stories of monumental inefficiency in the act of aid. And Job is now out of the travail in lineament of remaining stories yet the topnotch is in vituperative pauperism of ontogenesis and assets
Exactly what I was looking for. It took me quite some time before I came across your site, well worth the pain though. Thanks for the code.
, These days I am reading a lot about this topic because I have my essay writing project due on this weekend, I am glad that i found some very useful information and great help to complete my assignment on time, Thanks a lot.
ts
you probably have invested a lot of time in the procedure and the downtime is really impressive. What interests me is one thing further – how did you make those nice picturefor the tutorial. It’s really impressive
This is highly informatics, crisp and clear. I think that Everything has been described in systematic manner so that reader could get maximum information and learn many things.
This is so cool. I am such a huge fan of their work. I really am impressed with how much you have worked to make this website so enjoyable
Financial and Crisis Management, Insurance Claims, Society, Law, Social Justice,
Legal Aid, Finance and Claims Consultant
“Don’t let opinions of others consume you”
tornado insurance claim – houston family law attorney – houston juvenile attorney
Hey! Raising cyber-toast (not the type of bread, but the crisp type) for ya! I have not been around for months, but reading this was worth the punishment of stopping. Happy for you, Patricia. Very happy.
Wow, this is great, is it possible to change the attribution ID to different pages?