ASP.NET 中的 ID 命名约定?
它来自 HTML、XML 和 PHP 的世界,是使用 ASP.NET 制作 Web 应用程序时的一种新思维方式。我想在我的 MasterPage 中使用以下代码:
<div id="leftnav">
<asp:ContentPlaceHolder ID="leftnav" runat="server">
</asp:ContentPlaceHolder>
</div>
但由于本示例中的 leftnav
使用了两次,Visual Studio 提出了一个小而明显的抗议。在这种情况下我应该如何思考,以及在 ASP.NET 中哪种命名 ID 的使用最合适。
我不喜欢默认命名,因为 id="ContentPlaceHolder1"
没有说明任何内容。
感谢您的聆听!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我将 div 称为“nav”,将占位符称为“navPlaceholder”。这是因为“左”这个词意味着位置,它应该由你的CSS单独处理,而不是你的html。如果设计师决定将导航放在右侧怎么办?您可以在 CSS 中执行此操作,但最终会得到一些令人困惑的内容,例如
div #lefnav { float:right; }
这是一个简单的例子,我知道但需要记住一些事情。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.“leftNavPlaceHolder”怎么样?
只要符合您的命名约定就可以了:)
How about "leftNavPlaceHolder"?
Just as along as your consistent with your naming convention :)
不,默认 ID 很糟糕。我无法想象它们是用来使用的,只是 Visual Studio 不够智能,无法提出更好的建议,而且他们并没有建议半智能的东西,因为他们不想尝试脱落因为他们不是。合理 =)
因此,请养成始终更改默认 ID 的习惯。将它们更改为什么完全取决于您; “左导航内容”?当涉及到 ID 时,惯例几乎唯一涵盖的就是大写。
与纯 HTML 相比,唯一真正需要改变的是,您不能将“左导航”之类的 ID(即包含连字符)用于服务器控件。
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.
我建议,除非你确实需要,否则不要给 div 提供 id,因为这会影响性能。
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:
or
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.
我为所有 ASP.Net 控件添加前缀 ux 来表示用户控件。
然后你可以连接你的
String emailAddress = "[email protected]";
到您的uxEmailAddress.Text = emailAddress
I prefix all ASP.Net controls with
ux
to mean user control.Then you can hook up your
String emailAddress = "[email protected]";
to youruxEmailAddress.Text = emailAddress