ASP.NET NamingContainer naming convention
The Background
Hello! I'm working on a project in which the client has required a lot of things to happen on a single page, and this has resulted in a rather large blob of HTML being rendered out to the client browser.
The main issue is with input tags (where runat="server" attribute is set), these tend to cause a drastic increase in markup size due to validation, updatepanel triggers, viewstate, and the control markup itself. I've done what I can to reduce the amount of triggers I'm using, I'm compressing the viewstate (to something like 8% of the original viewstate size), I've gotten rid of a lot of ASP.NET Validators and rolled my own, and and I've been using ClientIdMode to reduce the length of the ID attributes of many asp.net elements. All of these combined significantly reduces the amount of HTML being sent to the client, (for example going from 2 megabytes for a request down to 500-600 kb - these are HUGE pages, mind you).
The Issue
One area which I've been having trouble reducing is simply the auto-generated 'name' attribute of input elements.
<input name="ctl00$ctl00$ctl00$_main$_main$_bodyMatterPhase$_phaseTree$ctl00$_taskTree$ctl00$_taskDetails$_detailList$ctrl0$_row$_descriptionText" type="text" value="Investigation Week 1" maxlength="100" id="_taskTree_0__taskDetails_0__detailList_0__row_0__descriptionText_0" style="width:170px;">
As you can see above开发者_Python百科, the name attribute is 139 out of 297 characters, that's almost 50% of the tag markup taken up by that HUGE name.
Does anyone have any ideas on how to stick a hook in somewhere in ASP.NET where I can somehow translate these or generate them differently; say instead of ctl00$ctl00$ctl00$_main$_main$_bodyMatterPhase$_phaseTree$ctl00$_taskTree$ctl00$_taskDetails$_detailList$ctrl0$_row$_descriptionText, it could be a GUID like 0x0AEED4B6445A11E08F873606E0D72085, which is 105 characters shorter.
Any help would be greatly appreciated!
It is not possible in asp.net versions earlier than 4.0.
In 4.0 you have ClientIDMOde property nad you can set it to 'static' value to get shorter names.
Look there for more info: http://msdn.microsoft.com/en-us/library/system.web.ui.control.clientid.aspx
Well to get you two answers to your question. The reason that your items are named in the manner that they are is that INamingContainer is implemented where each container prefixes the name of child controls with a unique identifier. Thus, the structure and length of your id is controlled by the structure of your HTML.
Now, in .NET 4.0, you have control to be able to control this a bit more on your own. I recommend having a look at this article, for a bit more info.
精彩评论