What are the naming guidelines for ASP.NET control

2019-01-16 11:54发布

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.

18条回答
霸刀☆藐视天下
2楼-- · 2019-01-16 12:10

Almost everyone uses Hungarian-style prefixes (option 2). Semantic naming is less useful because "Firstname" is actually the texbox.Text value, not the textbox itself.

查看更多
姐就是有狂的资本
3楼-- · 2019-01-16 12:11

I find that most of the time I care about what kind of information the control is for rather than what control type is currently being used to capture that data, so I prefer the type of information before the control type, so I can find it in a sorted list in the IDE:

  • AgeRangeDropDownList
  • AgreedToTermsCheckBox
  • FirstNameTextBox
  • LastNameTextBox

VS:

  • chkAgreedToTerms
  • ddlAgeRange
  • txtFirstName
  • txtLastName
查看更多
ゆ 、 Hurt°
4楼-- · 2019-01-16 12:12

I tend to go with the control type as a prefix and the name of the control afterwards but I always CamelCase so in your example for different types of controls you might have..

  • TxbFirstName
  • DdFirstName
  • ChbFirstName

For intellisense reasons I also always fully qualify the name of the control so I wouldn't do any of the following...

  • TxbFName
  • TxbClientNo
  • TxbNoOfCpn

But ultimately down to personal preference

查看更多
做个烂人
5楼-- · 2019-01-16 12:15

Not really sure about any guidelines, i suspect there are, but I always use number 2 as well!

查看更多
放我归山
6楼-- · 2019-01-16 12:16

I think it is better to use option 1 because it is easy to find the field by its meaning and its usage to understand programming coding down the road . Also,it is more usable with IntelliSense to find where we use this field for in our programming code. Therefore I can find the right control by the name of the meaningful field. I will not remmember what kind of control I use for this field but I can find this field by using the meaningful of field name instead of the type of control example I want to find "City" control , I just typ "City" , Intellisence will show me all information for this control but if I do not remember what kind of control I use for , I do not know what to begin....

查看更多
何必那么认真
7楼-- · 2019-01-16 12:18

Not sure about Microsoft official standards, but this is what i've done through out my development career.

I generally abbreviate the control type in front of the the name of what the control does. I keep the abbreviation lower case and the control's name CamelCase.

E.g. A texbox for username becomes tbUserName

Here is a list of standard abbreviations I use:

Abbr     -  Control

btn  -  Button
cb   -  CheckBox
cbl  -  CheckBoxList
dd   -  DropDownList
gv   -  GridView
hl   -  Hyperlink
img  -  Image
ib   -  ImageButton
lbl  -  Label
lbtn -  LinkButton
lb   -  ListBox
lit  -  Literal
pnl  -  Panel
ph   -  PlaceHolder
rb   -  RadioButton
rbl  -  RadioButtonList
txt  -  Textbox
查看更多
登录 后发表回答