Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ID naming convention in ASP.NET?

Coming from the world of HTML, XML and PHP its a new way of thinking when making web applications using ASP.NET. I'd like to use the following code in my MasterPage:

<div id="leftnav">
    <asp:ContentPlaceHolder ID="leftnav" runat="server">
    </asp:ContentPlaceHolder>
</div>

But since leftnav in this example is used twice, Visual Studio make a small but noticable protest. How should I think in this situation, and which is the most appropriate use of naming ID's in ASP.NET.

I don't like the default naming since id="ContentPlaceHolder1" says nothing of the content.

Thank you for listening!

like image 769
Benny Skogberg Avatar asked Jul 13 '10 08:07

Benny Skogberg


3 Answers

I would call the div "nav" and the placeholder "navPlaceholder". This is because the word "left" implies position, which should be handled soley by your css, not your html. What if the designers decided they wanted to put the navigation on the right? You could do this in your css, but you would end up with something confusing like div #lefnav { float:right; } a trivial example, I know but something to keep in mind.

like image 143
Daniel Dyson Avatar answered Sep 21 '22 00:09

Daniel Dyson


How about "leftNavPlaceHolder"?

Just as along as your consistent with your naming convention :)

like image 33
TWith2Sugars Avatar answered Sep 20 '22 00:09

TWith2Sugars


No, the default IDs are terrible. I can't imagine they're meant to be used, it's just that Visual Studio isn't intelligent enough to suggest anything better, and they're not suggesting something semi-intelligent, because they don't want to try to come off as something they're not. Reasonable =)

So make a habit of always changing the default IDs. What you change them to is completely up to you; "leftNavContent"? Pretty much the only thing that's coverned by convention is capitalization, when it comes to IDs.

The only thing that should really change from pure HTML is that you can't use IDs like "left-navigation", i.e. containing hyphens, for server controls.

like image 22
David Hedlund Avatar answered Sep 19 '22 00:09

David Hedlund