ASP.NET 中的 ID 命名约定?

发布于 2024-09-09 09:03:19 字数 449 浏览 20 评论 0原文

它来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

疧_╮線 2024-09-16 09:03:19

我将 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.

弄潮 2024-09-16 09:03:19

“leftNavPlaceHolder”怎么样?

只要符合您的命名约定就可以了:)

How about "leftNavPlaceHolder"?

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

南街九尾狐 2024-09-16 09:03:19

不,默认 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.

漫雪独思 2024-09-16 09:03:19

我建议,除非你确实需要,否则不要给 div 提供 id,因为这会影响性能。

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

埖埖迣鎅 2024-09-16 09:03:19

主观。这类问题的明显答案是:选择你喜欢的东西;将其标准化,并始终遵循它。

就我个人而言,我用以下方式命名我的东西:

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

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

对于包含,是的,有关发送消息的信息的区域。但你不必这样做。做任何你想做的事。只要使其一致即可。理想情况下,传达一些有关内部内容的信息。

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.

就此别过 2024-09-16 09:03:19

我为所有 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 your uxEmailAddress.Text = emailAddress

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文