ross dickinson: web and desktop software developer

Cleaning up ASP.Net WebForms markup

by Ross on November 19, 2010 at 1:08 PM under ASP.Net | Programming

One thing that clutters up ASP.Net websites is the amount of excessive markup created by controls.

One way to reduce the overall page size of the final output is to null/remove ID's on controls created in markup that don't need it. This often pops up because you've created a label, button, or some other control in the markup that you need to access from the code behind. If the control isn't being used on the client-side(through javascript, form validation, or some other purpose), you can safely set the ID property to null to prevent ASP from rendering the attribute. This is espescially helpful in versions previous to .Net 4, where you could potentially get id fields like "ctl00_ContentPresenter1_Parent_Child_GrandChildren_CabDrivers_lblStatus" mucking things up.

Consider this before going ahead: Objects relying on the FindControl method will still require that the ID attribute be set in order to find it. To avoid causing problems, you should null the ID property during the rendering phase. This can be done on the page level or the control level.

Add comment

  • Comment
  • Preview

About the author

rossisdead is a 26 year old web and desktop software developer from New Jersey. He has two cats and likes long walks on the beach.

On Stackoverflow

On Stackoverflow Careers

On Codeplex

On Github