views:

78

answers:

6

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!

+3  A: 

How about "leftNavPlaceHolder"?

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

TWith2Sugars
+1  A: 

I would suggest, don't give id to div unless you really need, because it will impact on performance.

Muhammad Akhtar
the 11 extra characters being sent to the client? notice that the DIV is not `runat="server"`. OP is talking about a *smallbut noticable protest* in the designer - not a compile error =)
David Hedlund
I am talking to whole html controls id not for single ID
Muhammad Akhtar
I think if you have much html on page, and you unnessceraly id, it will have impact on performance, although it is minor.
Muhammad Akhtar
+6  A: 

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.

Daniel Dyson
a sensible approach. +1 for separating our concerns
David Hedlund
Thanx Daniel. I've changed the names to globalNav, localNav and supplementNav to better describe its content, rather than style or position. Sincerely,
BennySkogberg
+1  A: 

Subjective. The obvious answer to this type of question is: Pick something that you like; standardise it, and always follow it.

Personally, I name my things in the following fashion:

<asp:Literal runat="server" ID="ltlDescriptionOfContent" />

or

<asp:PlaceHolder runat="server" ID="plhSendMessage">
</asp:PlaceHolder>

For an area that contains, yes, information about sending a message. But you don't have to do this. Do whatever you want. Just make it consistent. And ideally, convey a little information about what is within.

Noon Silk
That's a nice way of thinking, just like ordinary C# naming conventions. I just thought there were a simple naming convention around, that everybody uses. Thanx for your answer.
BennySkogberg
This is very sensible advice too.
Daniel Dyson
+2  A: 

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.

David Hedlund
+1 for "make a habit of always changing the default IDs" They are meaningless and make any client side scripting harder to understand.
Daniel Dyson
+1  A: 

I prefix all ASP.Net controls with ux to mean user control.

Then you can hook up your String emailAddress = "[email protected]"; to your uxEmailAddress.Text = emailAddress

ck