JoshBerke
JoshBerke

Reputation: 67078

Is there a benefit to always having an ID property?

In asp.net is there any benefit to including or not including an id property? Assume for this question that there is no programtic reason why we need an ID. (We are not manipulating the control in code behind, nor on the client).

My personal preference is only to include an Id property when I have a reason to manipulate the control and need to be able to refer to it. So I find myself always removing Image1 Label1 etc etc...

Answers so far:

  1. Keeping them can help assisted technologies.
  2. Keeping them can help with test suites
  3. Removing them can have an impact on bandwidth
  4. Removing them keeps your intellisense cleaner (just added this one)

Upvotes: 2

Views: 213

Answers (6)

Perpetualcoder
Perpetualcoder

Reputation: 13571

These ID's on server controls also help when you are trying to see the generated code on the browser (which you need to do at times, at least I do). For example some text box I call tbLastName would be converted to something like ctl001_tbLastName

Upvotes: 0

CodingWithSpike
CodingWithSpike

Reputation: 43728

When writing HTML by hand, I don't include an ID on anything unless I need it. For a high-load site, there is less bandwidth used if it is not there. Considering people go through major work to remove whitespace and comments from CSS and JS files (usually at build time), I'd consider removing unused ID tags to go into the same boat. Also, I'd assume the browser has less DOM building work to do if there isn't an ID. It all adds up to a bit faster page transfer and render time, which adds up over the long run!

Upvotes: 0

azamsharp
azamsharp

Reputation: 20076

Basically whenever you like to refer to the control from the server side you will require the id. Most of the binding tasks that you perform are on the server side. This includes binding to the GridView control. Assigning messages to the Label control etc.

If you are not using the control in code behind then you can leave out the id. But I would always suggest that give meaningful ID's to all the controls.

If you don't assign the id and add a control with ID say "Button1" then there is a change that this will collide with the already added button with no id.

Upvotes: 0

Kevin Tighe
Kevin Tighe

Reputation: 21171

Having ID properties are useful for testing your page with a tool like Quick Test Pro, etc. They're also useful for blind users who browse with a screen reading tool.

There's a good Hanselminutes podcast where this is discussed: http://www.hanselminutes.com/default.aspx?showID=143

Upvotes: 2

Jeff.Crossett
Jeff.Crossett

Reputation: 315

True, the only reason the ID exists is to get a handle on an individual object, but there is definitely no benefit to deleting the auto-assigned ID that VisualStudio creates for you.

You never know when you may an ID, so I would at least recommend leaving the default if nothing else.

Upvotes: 0

looor
looor

Reputation: 21

Taken from the MSDN (http://msdn.microsoft.com/en-us/library/system.web.ui.control.id.aspx)

"Setting this property (ID) on a server control provides you with programmatic access to the server control's properties, events, and methods. This property can be set by Web developers by declaring an ID attribute in the opening tag of an ASP.NET server control. If this property is not specified for a server control, either declaratively or programmatically, you can obtain a reference to the control through its parent control's Controls property."

So it looks like the ID property is provided for accessing the control through code. However you still are able to access a control without a ID property through code.

Upvotes: 2

Related Questions