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

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

问题描述

我们正在制定我们希望在我们的开发团队中使用的设计指南,并讨论如何命名ASP.NET控件。我在谈论我们的好朋友Label,TextBox,Button等。



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


  1. 将控制类型作为后缀添加到控件ID:[FirstName _ TextBox]或[FirstName _ tbx]

  2. 将控件类型作为前缀添加到控件ID [tbxFirstName]

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

我们最后决定使用选项2.它不像选项1那么冗长,我喜欢它在控件名称之前指定了什么控件。 p>

我的问题是,Microsoft是否已经为这些前缀发布了任何指南,或者您对我们的决定有任何意见。

解决方案

当您将TextBox1添加到页面时,Visual Studio添加它的原因是因为Microsoft无法知道您打算如何使用它。命名为Control1会太混乱,因为它可以是任何数量的控件。



Microsoft提供了一般的OO命名约定的指导, 。由于UI控件最终是代码中使用的变量,因此它们应遵循与任何其他变量(无匈牙利语符号前缀)相同的约定。





原因是...




  • 控制类型可能会从文本框更改为列表框,则所有关联的代码都必须修复

  • 您的代码应该更关心控件的内容,更少关注控件的类型。当你关心控件的类型,你开始依赖于某些功能,并打破封装 - 你应该能够轻松地交换控件,而不改变太多或任何代码。 (基本的OOP原则)

  • 这是很容易想出标准控件的前缀,但每天都在开发新的控件。您可以制作自己的WebUserControl,或者您可以购买一组第三方控件。 您将如何决定要为自定义控件使用哪个前缀?您的代码应该关注其中包含的信息,而不是关注控件的类型。



示例




  • txtFirstName => firstName或FirstName

  • txtState =>状态或状态

  • cboState =>状态或州(更改控制类型的主要示例是什么是lstState或rdoState - 代码不关心控件的类型,而是用户选择的状态)

  • ctlBilling => billingAddress或BillingAddress(自定义控件 - 使用匈牙利记号法,是,但是具有有意义的名称,我开始理解其中包含的信息,即billingAddress.Street,billingAddress.FullAddress等。)


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.

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

  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].

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.

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

解决方案

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

The main reasons are...

  • 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.

Examples

  • txtFirstName => firstName or FirstName
  • txtState => state or State
  • cboState => state or State (prime example of changing control types what about lstState or rdoState - they should all have the same name because your code is not concerned about the type of control,rather the state the user selected)
  • ctlBilling => billingAddress or BillingAddress (custom control - with hungarian notation it is not very evident what the control even is, but with a meaningful name I begin to understand the information contained in it. i.e. billingAddress.Street, billingAddress.FullAddress etc.)

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

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