How To Name Controls in ASP.NET Web Forms
When working with ASP.NET, particularly Web Forms up until version 4 of the ASP.NET framework, one might want to think a bit at the naming of the controls used.
I often stumble upon control names looking like this: ChoicesPlaceHolder, MessageLabel and similar. That's clear and decent naming, but it can be improved.
Me and fellow developers have found a simple yet useful and clear naming convention, based on a prefix and a descriptive control name (using Hungarian notation).
This is how it can look instead: phChoices, lblMsg
Naming is particularly important when dealing with nested controls and ContentPlaceHolders. Using better naming, you can improve and reduce this rendered control id:
ctl00_ContentPlaceHolderAllColumns_ContentPlaceHolderAllContentColumns_
ContentPlaceHolderSubNavigation_SubNavigation_RepeaterNavigation_ctl06_
HyperLinkNavigationPageLink
To this id:
ctl00_cphAllColumns_cphAllContentColumns_cphSubNav_SubNav_rptNav_
ctl06_hlNavLnk
Pretty much better, huh? Plus it saves bandwidth.
Also, think twice about where you put runat="server". Is it needed on all controls?
Here's a table with common controls, their prefix and an example naming.
Table: Common ASP.NET Web Forms control and naming
| ASP.NET Control | Prefix | Example |
| Button |
btn |
btnSend |
| CheckBoxList |
cbl |
cblColourCombinations |
| ContentPlaceHolder |
cph |
cphMain |
| DropDownList |
ddl |
ddlNavChoices |
| Label |
lbl |
lblMsg |
| Literal |
lit |
litMsg |
| Panel |
pnl |
pnlLogin |
| PlaceHolder |
ph |
phArticles |
| RadioButtonList |
rbl |
rblPaymentChoices |
| TextBox |
tb |
tbUserName |