Search code examples
asp.netvb.netformsasp.net-controls

Advantages of not specifying control IDs?


Reading over TRULY UNDERSTANDING VIEWSTATE the author suggests:

When ASP.NET parses the form, and finds a tag with runat=server, it creates an instance of the specified control. The variable name it assigns the instance to is based on the ID you assigned it (by the way, many don't realize that you don't have to give a control an ID at all, ASP.NET will use an automatically generated ID. Not specifying an ID has advantages, but that is a different subject).

What advantages could one gain by not specifying control IDs? My quick searching didn't turn anything up. Thanks for the help.


Solution

  • If you do not specify control ID, then you cannot access the control and change it in the code-behind file, so it seems like a constant control on the page. I do not know if this is an advantage or not; I see it just as a rarely-used option.

    The only advantage I can think of is if you have many images that you do not wish to change, but you like having asp.net take care of the image path. In that case, it is a nice idea to not specify control id.

    When the markup is parsed, any controls not set to runat="server" are treated as regular text, which is assigned to the Text property of literal controls. Thus their IDs do not appear in the page. In other controls that post data, if you do not include ID then the control is usually rendered with only the name (not the id).

    Also the controls without control id are not registered on the table with the controls, and searching for controls may be faster in a smaller table.