Best practice for naming convention of UI controls for referencing in code-behind?

What is the best practice for naming UI controls (textboxes, drop-downs, etc.) on forms and reports for reference in the code-behind pages?

I develop a lot of reports and forms in my office. I have several web applications providing about 80+ "live" reports being generated from various and multiple data sources (Access, SQL, Oracle). These reports are considered "live" because they accept user set paramaters from a form, then query the database to produce a report based on the current information available.

So, the process starts with obtaining the values set by the user, passing those to the database query, receiving the dataset, and finally assigning the dataset to the report. In some cases, additional fields displayed on the report need to be calculated from the dataset before the report can be generated. This requires referencing the output controls on the report to assign the calculated value.

While I don't really care to use prefixes in my code for variables or member fields, I do use them to identify the UI controls. For example, txtFirstName to reference the report control to assign the data from the FirstName field in the dataset to the display control on the report. Is there a better practice for naming/referencing UI controls on forms and reports?

-------------Problems Reply------------

The main product I work on at work uses the txt_ pnl_ etc prefixes. This does work, though it is a bit of a pain at times when switching something that just hides/shows controls from say, a tr to a panel because you have to rename it.

What I've started doing in new projects, is naming my UI controls with a ui prefix; for example, uiName. Since I am strongly opposed to anti-hungarian notation, and strive for self-documenting code, this convention works well. In fact, if anything, it is real hungarian notation (ui being the prefix meaning user interface control).

For GUI controls, I suffix the variable name with the control name:

  • firstNameTextBox
  • lastNameTextBox
  • submitButton

This makes the relationship obvious between for example _firstName and firstNameTextBox.Text, and no one has to remember what the hungarian notation equivalent is. I would always choose clarity over brevity in naming variables.

I still use Hungarian Notation for Controls but no longer for variables.

btn Button
cbo ComboBox
chk CheckBox
clb CheckedListBox
grp GroupBox
iml ImageList
lbl Label
lnk Hyperlink
mnu Menu
pbr ProgressBar
pic Picture
pnl Panel
rtb RichTextBox
tmr Timer
tvw TreeView
txt TextBox

Your answers here will be very subjective. Different tastes and programming backgrounds will give you different preferences.

Perhaps what will be most important to you in the long run is consistency among all of your projects, so that regardless of who developed the code you will be able to understand it when reading.

We outsource a lot, so make certain to communicate our naming conventions to all of our project managers.

Here are some links to naming conventions:

I do similarly to you. When you have tons of controls it all becomes messy, so I prefix each name with the capitals of the control class.

For example:

TextBox -> tbName

DataGrid -> dgName

Panel -> pName

This makes it clear how to handle new controls (i.e. how to derive the prefix)

I've always felt that the only real reason for the prefixes was so you could have things like txtFirstName and lblFirstName on the same form/page. Since, the vast majority of the time, I'm really only working with the actual field control itself, I skip the prefix for that, and only use the prefixes for associated controls. For instance, lblMonth & Month, skipping the cbo prefix.

It saves typing, and it will generally be obvious what sort of control you're using in such forms. More complex controls will get the full prefix treatment.

Category:naming conventions Views:0 Time:2008-09-21

Related post

  • What is good practice for writing web applications that control daemons (and their config files) 2011-01-01

    Can someone suggest some basic advice on dealing with web applications that interact with configuration files like httpd.conf, bind zone files, etc. I understand that it's bad practice, in fact very dangerous to allow arbitrary execution of code with

  • Using version control to keep multiple code branches up to date 2012-02-01

    I'm hoping that someone will be able to help me better understand version control in this scenario. Currently we're thinking of either implementing VisualSVN or Mercurial, but want to get our strategy figured-out first. I'm part of a two man developm

  • How to bind a nested control from main page code behind 2009-09-10

    I have the following structure in place and need to rebind the lower control (DropDownList) from the code behind of the MainPage. x MainPage1 x---- Panel1 (modal popup) x--------- UpdatePanel (upMailOrStatusAction, on Panel1) x-------------- RadioBut

  • change Visibility of a control in ASP.NET code behind (c#) 2010-06-03

    How do you set the visibility of a (fileupload) control from code (I need to hide a fileupload control in a webuser control from server site, otherwise hasFIle is always false). Also setting the "Visible" property to false does not work (as i

  • how to control the http status code properly? 2011-11-27

    I met a strange thing when test the 400 error handling in CodeIgniter. Message: Cannot modify header information - headers already sent by (output started at ...) And the http status code is always 200, finally I found that there is a new line before

  • Every time I click on 'Control Panel' an error code appears...."Internet Explorer has stopped working." 2012-06-25

    Every time I click on 'Control Panel' an error code appears...."Internet Explorer has stopped working..check on line for solution & restart program"...?????? *original title - Control Panel Error* --------------Solutions------------- Hi Richard,

  • Can't Exit Content Control After Running VBA Code 2014-01-27

    Resolved: Badly placed Cancel statement. I'm using Word 2010 32bit on Windows 7 64bit. I have created a legal document with a preamble that contains multiple rich text content controls for various pieces of information, all nested within a locked for

  • What are the naming conventions for common controls while working within Visual Studio in C#? 2011-04-08

    I'm having trouble finding a site that goes into detail about naming conventions for the common controls of Visual Studio. Either a website or list would be appreciated. I'm not looking for naming conventions for namespaces, classes, methods, or vari

  • What are best practices for validating ASP.Net user controls? 2009-03-27

    I'm wondering if any best practices exist for guidelines as to what should and should not go into a validator control. My thought and practice was that it should be basic sanity checking (i.e. did the user enter all the required inputs? Is there 10 d

  • Naming convention for windows control in C# 2011-04-27

    I want to know what the guidance on name the control in c#. I have seen the following link but it does not talks about controls: Other links tell me to use hungarian notation but some links tell m

  • What is the best practice for implementing AutoSize on custom controls? 2012-04-09

    What is the best practice to implement autosizing on custom Windows Forms controls? For instance, a custom Label control inheriting from the System.Windows.Forms.Label class. The Control.ClientSize article on MSDN has an example on how to set the new

  • Best Practice For Creating a Modular HTML Control Widget? 2013-05-29

    So I have a custom control (for example a clock) that I created with html/js/css but I'd like to make it more modular, so it could be used multiple times in the same page. So currently I have the files: MyClock.html MyClock.js MyClock.css But I'd lik

  • Naming Conventions of XAML controls in Win 8 apps 2014-11-12

    Is there any standard naming conventions available for naming the XAML controls (such as Grid, Stack Panel, Text Box, Label etc..) in Window 8 Applications. --------------Solutions------------- Hi Meena Sivaraman, Thank you for posting your question

  • Best practice when you need two user controls (winforms) to communicate 2009-06-30

    Extended problem I would like to add a new problem in addition to the original problem specified below. One of the user controls in the application contains a list of objects that I need to access from another user control. How can this be done? (I d

  • Best Practice to refer a control in jQuery 2009-09-17

    I see 4 or more ways of referring to an control in jQuery $("input[id$='txt1']"); OR $("#<%= txt1.ClientID %>"); OR $("#txt1"); OR or access using class Can you guide which one to choose in what scenario --------------Solutions---------

  • best practice for rendering a set of controls in WPF? 2010-07-20

    I'm creating a WPF application that is similar in layout to Microsoft Outlook, where you have some navigation buttons on the left and then a grid on the right hand side that displays content. I'd like this content area (which is just a Grid, split in

  • Best practices when Integrating an ASPX User control into a web form 2011-10-27

    I have a dashboard that I've created for an MVC .net web app that is using Telerik tools. The concept I'm attempting to achieve is to have a master dashboard page, that users can add tabs to, each tab holding a set of dashboard controls. The dashboar

  • Best practices for naming conventions 2015-03-16

    I've been a Flash programmer for a long time and have learned my naming conventions over many years -- mostly the hard way. Lately I've started working with Access more and more and I feel like my names are all over the map and are hindering me. I kn

  • A number of hopefully simple questions regarding best practices and coding conventions about this code 2010-10-18

    I have placed comments with my questions inline. The code supports making rest calls. // (1) Is appending Base to the name of base classes useful? public abstract class RestCallBase : IRestCall { // (2) Is there a good way to decide on the ordering/g

Copyright (C), All Rights Reserved.

processed in 0.148 (s). 11 q(s)