What are the naming guidelines for ASP.NET controls?

后端 未结 18 1893
离开以前
离开以前 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:43

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

    0 讨论(0)
  • 2020-12-07 21:45

    I've been struggling with this problem too. I used to use the "hungarian style prefix".

    Now I take a different approach, I try to see the controls as private fields from my class. I don't pre- of postfix my private fields with their type, so why should I do that to an TextBox?

    So what used to be:

    var newCustomer = new Customer();
    newCustomer.Name = txtName.Value;
    newCustomer.Address = txtAddress.Value;
    newCustomer.City = txtCity.Value;
    newCustomer.HasEnoughMoney = cbHasMoney.Selected;
    

    Becomes:

    var newCustomer = new Customer();
    newCustomer.Name = name.Value;
    newCustomer.Address = address.Value;
    newCustomer.City = city.Value;
    newCustomer.HasEnoughMoney = hasMoney.Selected;
    

    To be honest, I couldn't care less if the "name" control is a text box or what else, I just want it's value.

    And if it's not clear enough if your talking about your control or about another field/variable, I think you should either reconsider the name of that field/variable or the function of you class (meaning it might be a little bit to big?).

    0 讨论(0)
  • 2020-12-07 21:48

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

    0 讨论(0)
  • 2020-12-07 21:52

    If you look at it from a code maintainance point of view what would be the best notation to look at after you did the code 2 years ago. Although we try to ensure that forms don't have too many fields on them we all know that this sometimes happens. If we used the Hungarian type notation by prepending the control type I think it would be easier to see where that value is coming from instead of having to figure it out in the event the variable name doesn't make it obvious. If you are using any type of Refactoring tool then changing the name of the control would change the code automatically thereby reducing the change control arguement.

    0 讨论(0)
  • 2020-12-07 21:54

    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
    0 讨论(0)
  • 2020-12-07 21:56

    Microsoft does provide some guidance here.

    When you drag a control onto a web form you get something like "TextBox1" automatically. That's the IDE telling you that you should change the "1" part for your specific needs.

    In that case, "TextBoxFirstName" seems like the way to go.

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