什么是ASP.NET控件的命名规则? [英] What are the naming guidelines for ASP.NET controls?

查看:152
本文介绍了什么是ASP.NET控件的命名规则?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在纳丁出来的设计原则,我们希望在我们的开发团队使用的过程中,今天坐上讨论围绕ASP.NET控件的命名方式。我说的是我们的好朋友标签,文本框,按钮等。

We are in the process of nutting out the design guidelines we would like to use in our development team and got into a discussion today around how ASP.NET controls should be named. I am talking about our good friends Label, TextBox, Button etc.

我们想出了我们投了以下三种可能性:(例子是进入一个TextBox /显示名字)

We came up with the following three possibilities that we voted on: (Example is a TextBox to enter/display a FirstName)


  1. 作为一个后缀添加控件类型到你的控件ID:[姓 _ 文本框]或[姓 _ TBX]

  2. 添加控件类型为preFIX到你的控件ID [tbxFirstName]

  3. 设置如选项2 [lblTextBox]控件名字和名称相关领域(如为文本框或验证标签)的ID。

  1. Add the control type as a postfix to your controls ID: [FirstName_TextBox] or [FirstName_tbx]
  2. Add the control type as a prefix to your controls ID [tbxFirstName]
  3. Set the ID of the control to FirstName and name related fields (like a label for the textbox or a validator) as in option 2 [lblTextBox].

我们最终决定采用选项2,它并不像选择1 verbose和我喜欢它指定了控制它是控件的名称之前。

We ended up deciding to use option 2. It's not as verbose as option 1 and I like that it specifies what control it is before the name of the control.

我的问题是微软是否已经发布的指引,这些prefixes和,或者如果您对我们的决定有任何意见。

My question is whether Microsoft has released any guidelines for these prefixes and or if you have any comments about our decision.

推荐答案

原因Visual Studio的补充,当你将它添加到页面TextBox1的,是因为微软没有办法知道你打算如何使用它的方式。其命名为控制1会太混乱,因为它可能是任何数量的控制。

The reason Visual Studio adds "TextBox1" when you add it to the page is because Microsoft has no way of knowing how you intend to use it. Naming it "Control1" would be too confusing because it could be any number of controls.

Microsoft提供指导,一般的面向对象的命名约定,但并没有具体命名UI控件。由于UI控件都少不了在code使用的变量,它们应遵循任何其他变量相同的约定 - 没有匈牙利命名法preFIX

Microsoft provides guidance in general for OO naming conventions, but not specifically for naming UI controls. Since UI controls are ultimately variables used in code, they should follow the same convention as any other variable - no hungarian notation prefix.

的主要原因是...


  • 控件类型可以从文本框列表框改变,那么所有相关的code必须是固定的(如前所述)

  • 您$​​ C $ C应该更关注控制的内容,少用它是什么类型的控制。当你关心的是控件的类型,你开始依赖于某些功能和你打破封装 - 你应该能够轻松交换控制在不改变太多或code。 (OOP的基本原则)

  • 这是相当容易拿出prefixes为标准控制,但新的控制每天都在发展。你可以使自己的WebUserControl,或者你可以购买一套第三方控件。 您将如何决定哪些preFIX用于吗而不是集中于控制的类型,你的code应与它所包含的信息有关自定义控件。

  • Type of control may change from textbox to listbox, then all associated code will have to be fixed (noted earlier)
  • Your code should be more concerned with the content of the control and less with what type of control it is. When you are concerned with the type of the control, you start to depend on certain functionalities and you break encapsulation - you should be able to easily swap controls without changing much or any code. (Basic OOP principle)
  • It is fairly easy to come up with prefixes for the standard controls, but new controls are being developed every day. You may make your own WebUserControl, or you may purchase a set of third party controls. How will you decide which prefix to use for the custom controls? Instead of focusing on the type of control, your code should be concerned with what information is contained in it.

例子


  • txtFirstName =>的firstName或姓

  • txtState =>国家或国家

  • cboState =>国家或国家(改变控制类型的怎么样lstState或rdoState的典型例子 - 他们都应该有相同的名称,因为你的code是不关心控件的类​​型,而国家的用户选择)

  • ctlBilling => billingAddress或BillingAddress(自定义控制 - 与匈牙利命名法不是很明显什么控制,即使是,但有一个有意义的名字我开始理解其中包含的信息,即billingAddress.Street,billingAddress.FullAddress等等。)

这篇关于什么是ASP.NET控件的命名规则?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆