开发者

ID naming convention in ASP.NET?

开发者 https://www.devze.com 2023-01-07 14:28 出处:网络
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:

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&开发者_C百科gt;
</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!


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.


How about "leftNavPlaceHolder"?

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


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.


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


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.


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

Then you can hook up your String emailAddress = "test@abc.com"; to your uxEmailAddress.Text = emailAddress

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号