What are the naming guidelines for ASP.NET controls?

后端 未结 18 1895
离开以前
离开以前 2020-12-07 21:23

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

相关标签:
18条回答
  • 2020-12-07 21:59

    I don’t think there’s a right or wrong answer here, whatever you decide, I think the most important aspect is just to be consistent when it actually comes to coding.

    0 讨论(0)
  • 2020-12-07 22:00

    I'm not sure of the guidelines regarding ASP.NET, but in the book Framework Design Guidelines from Microsoft, there are several best-practice guidelines about naming of class members. Since ASP.NET Controls in most cases result in a protected field of the appropriate type, I consider these naming guidelines to apply for ASP.NET controls as well. In fact Code Analysis does not differentiate on Control reference fields and other fields.

    These guidelines recommend using a naming scheme that implies the logical use rather than a type-descriptive variant. There are several reasons for this. The prefix is implies a type to the developer that might not be correct due to later changes. It adds an extra step in code maintainence. If you change your Button control into a LinkButton control the name also needs to be changed to correct the prefix.

    For that reason I would call the control FirstNameEdit etc...

    0 讨论(0)
  • 2020-12-07 22:05

    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.

    • msdn.microsoft.com/en-us/library/xzf533w0(vs.71)
    • msdn.microsoft.com/en-us/library/ms229002(VS.80)

    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.)
    0 讨论(0)
  • 2020-12-07 22:07
    Abbreviation    ||   ASP.NET Control
    

    STANDARD CONTROLS:

    btn Button

    cb CheckBox

    cbl CheckBoxList

    ddl DropDownList

    fu FileUpload

    hdn HiddenField

    lnk Hyperlink

    img Image

    ibtn(btn) ImageButton

    lbl Label

    lbtn(btn) LinkButton

    lb ListBox

    lit Literal

    mv MultiView

    pnl Panel

    ph PlaceHolder

    rb RadioButton

    rbl RadioButtonList

    tbl Table

    txt TextBox

    v View

    DATA CONTROLS

    dtl DataList

    dp DataPager

    dtv DetailsView

    ets EntityDataSource

    fv FormView

    gv GridView

    lds LinqDataSource

    lv - ListView

    ods ObjectDataSource

    qe QueryExtender

    rpt Repeater

    smd SiteMapDataSource

    sds SqlDataSource

    xds XmlDataSource

    VALIDATION CONTROLS

    cpv CompareValidator

    ctv CustomValidator

    rv RangeValidator

    rev RegularExpressionValidator

    rfv RequiredFieldValidator

    vs ValidationSummary

    VALIDATION CONTROLS:

    cpv // CompareValidator

    ctv CustomValidator

    rv RangeValidator

    rev RegularExpressionValidator

    rfv RequiredFieldValidator

    0 讨论(0)
  • 2020-12-07 22:07

    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.

    0 讨论(0)
  • 2020-12-07 22:08

    Two reasons why I prefer option 1:

    1. FirstNameTextBox more closely matches my business object.
    2. More usable with IntelliSense.

    Having said that I am considering changing to FirstNameCtrl for the reason csgero pointed out about changing control types. So why bother with any postfix or prefix, to reduce/remove the posibility of conflicts with asp/win form properties.

    0 讨论(0)
提交回复
热议问题