Home Page

ASP.NET server controls each have a property called the ID.  The ID property defines a unique name for the server control.  You cannot have two server controls on the same web form with the same ID.  The ID is what you use in the C# source code behind .cs file to reference a control on a web form.  If you are unsure what the ID of a control is, click on a control in the Design view and scroll down the Properties window until you see (ID).

When you drag a control onto the web form, the IDE creates a default ID for your control.  It is a good idea to immediately rename your control’s ID right after you put it on the form.  You should name it something meaninful to your application and use a naming convention prefix.  For example, if you plan to use a TextBox to store a user’s last name, name the TextBox control’s ID to “txtLastname” or something similar.  It doesn’t matter what the particular convention is that you use, but be consisent across your application so that your code is more readable and easier to understand.  It is very difficult to debug and maintain a program where the control names are TextBox1, Button1, and so forth.

I like to use simple prefixes appended with a meaninful name for my server control IDs.  Here is a list of some prefixes that I use for my server controls and their meaning:

txt = TextBox (e.g. txtLastname)
btn = Button (e.g. btnSavedata)
ddl = DropDownList (e.g. ddlAccounts)
chk = CheckBox (e.g. chkAdmin)
rb = RadioButton (e.g. rbSpringsemester)

The prefix helps your source code become more maintainable because you can instantly know what kind of server control you are dealing with when you see and modify the code.

No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment