views:

22

answers:

2

If in the markup you declare a control like this:

<asp:Label runat="server" AssociatedControlID="txtSomeInput">Some Input:</asp:Label>
<asp:Textbox id="txtSomeInput" runat="server" />

Does leaving out the ID from the label control change anything in regards to Viewstate or the Page/Control life cycle? Does it just get some generated ID? Obviously it cannot be referenced in the code-behind at design time (well at least not by the programmer)...Just curious as to if this has any side effects (or tangible benefits)?

Further, I would presume if this control cannot be referenced at design time it can't really be changed in a way that would add any information to viewstate...yet the runat tag allows the AssociatedControlID to be decided (master page crap ids for example) thus not having to have a html label with a <%# %> to write the clientId??

thanks!

A: 

Some side-effects are if you're using some tools that run script client-side. For example, a button without an ID cannot properly trigger an update panel operation correctly (in most cases) because when the javascript is created rendering what clicks should be captured/re-routed as partial postbacks...the lack of an ID leaves it out of the list (Think, what javascript could you manually run to attach the handler without it?).

The button just needs the right click handler, the ID doesn't matter for what it triggers server-side...but this leaves client-script needing more a lot of times.

As a side note: If you haven't looked at .Net 4, I suggest you take a look, the situation has vastly improved with ClientIDs.

Nick Craver
Good points, I've been waiting for .Net 4 for ages...when they announced some of the benefits last year it really will give WebForms some extra credibility
davidsleeps
+1  A: 

It makes no difference from a functionality perspective. Elements which require an ID to function properly will auto-generate one. It does make referencing the control by name in the code-behind difficult, but still possible, it just requires manually finding it and wiring it.

Rex M
Thanks for clarifying that, I was only really interested in removing the ID from controls that don't need an referencing...where a control needs referencing, that's what an ID is for...
davidsleeps