views:

81

answers:

5

If you don't provide an ID for your declared controls in ASPX files, I know that VS will automatically generate one for you. Should I always give the controls a descriptive ID even when I won't be accessing them from the code-behind?

+1  A: 

On the plus side, you may be able to cut down on the name generated by ASP.NET e.g., (SomeGeneratedNameForATextBox23) and save some bytes sent down to the client. On the negative side, you end up polluting your intellisense with a member that you would actually never touch. So, the answer is: it depends :)

aquinas
+3  A: 

I would say yes. It seems to me that it's just good practice and it defiantly makes it easier to debug when you look at the generated source in the browser.

Muad'Dib
+4  A: 

It is more of a preference thing, really. I tend give names to most of my elements, except for things like labels that I'll never change. I take special care to name all of my div's, for example, for my own sanity when the time comes to modify a layout later.

I suppose that sums up the reason I do it, then. Giving names for most of your controls is more declarative and easier to read. If you're concerned about "polluting" your Intellisense, just use a common prefix between similar controls (like TxtFirstName for a textbox).

Dusda
A: 

If you are not using them from code behind, ids wont make any difference, but its always a good practice. Certain things are done just for the sake of good design. These ids wont be of any use on the client side because these ids will be changed when sent to the client. So, you will have to use class attribute to use them using say JQuery.

pokrate
+4  A: 

If you won't be accessing the control from the code-behind, you should first question if you need a server control at all. If it's a basic control like a label with no databinding going on, the answer is probably "no". You can use plain old HTML markup instead. If it's something more complicated, than you should give it a name that addresses that complexity.

Joel Coehoorn
I'm thinking of things like ScriptManager, UpdatePanel, Content, and LinkButtons within a control that will bubble up a command. I need these to be server controls, but don't need to refer to them in the code-behind.
Chris Dwyer
Validators also are server controls, but I don't need to access them from the code-behind.
Chris Dwyer