Friday, July 24, 2009

Client Id of a control in ASP.NET 4.0.

Client Id is used to store the id of a server control in client side. ASP.NET Engine creates a very long client id to unique identify each element on the same page. If you are having so many inner controls hierarchy then asp.net will generate very long client id like “ctl00_Ctl001_ctl02_BM_Login_btnSave” here i i have used control name like ctrl00,ctr001,bm etc. If you are having long control name then it will more longer then this. This will increase html kilobytes for the pages and if you are having so many server controls in the page then it will take time to load in browser and will decrease performance of application . To overcome this problem we have to override existing asp.net controls like textbox,label etc and then you have override client id from server side with server side id. Now with asp.net 4.0 you can control client id generated by .net framework. In asp.net 4.0 there is one another property called ClientIdMode which will control the behavior of client id generated by .net framework. Like following.

<asp:Button ID="btnSave" runat="server" ClientIDMode="[Mode]" />

There are four types of different mode for ClientIdMode property.

  1. Legacy- This is the same as in old version. It will generate client like like “ctl00_Ctl001_ctl02_BM_Login_btnSave” in old fashion.

  2. Inherit-This is the default mode for every control . It will refer parent control to have its clientidmode. If you want to change it then you have to specify your client id mode for that control.

  3. Static –This mode will put client id as static if you set server id like “btnSave” then it will generate the same ld like “btnSave”. So if you want to use this mode then you have to make sure that you are having all unique name in one pages. Otherwise it will be a mess for ids.

  4. Predictable- This mode will used when the framework needs to ensure uniqueness of each control in particular way. The framework traverse from the control to parent controls and put hierarchy of parent control id with control id like ‘”ctrl0_btnsave” and for another btnsave its like “ctrl0_ctrl1_btnsave” etc.

  5. Technorati Tags: ,
Share:

2 comments:

  1. You are correct on this one, but changing ContentPlaceHolder Id name shorter will do the trick.

    Moreover Gridview is expensive, It is always better to generate controls dynamically at runtime, that will give you more flexibility, light weight and full control over html.

    ReplyDelete
  2. There is possibility to turn off ClientID for almost any control and it works even in ASP.NET 2.0 projects.

    Project: http://clientidless.codeplex.com/
    Demo: http://show-demos.net/NoClientId/

    ReplyDelete

Your feedback is very important to me. Please provide your feedback via putting comments.

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews