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!
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.
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.
精彩评论