Friday, February 18, 2011

Client ID problem when moving from .NET framework 3.5 to 4.0

HI guys,
ASP .NET automatically assigns a unique client ID to each and every server side control to make them distinguishable irrespective of the fact that whether 'id' attribute is applied to that control or not. This would be used by various client side operations like JavaScript functions etc.. Now recently we migrated our project from framework from 3.5 to 4.0. And after that I noticed that all the controls that were used in previous framework with prefix like ctl00.. became invalid. Then I figured that the mechanism through which ASP .NET 4.0 assigns the clientID is different from earlier versions. It uses a much cleaner approach like that of simple HTML controls in assigning clientId i.e instead of ctl00Master_XYZ it will use only XYZ as clientd Id.

What if we have used hardcoded autogenerated clientID in javascript functions.
First thing, as good coding convention we should not use the hardcoding of dynamic entity like clientId. You should use this way: <%= Control.ClientID %>'. Now if in any case you have used this thing and owe this 'Technical Debt', then other way is change the clientID Mode. clientId Mode can be changed at control level, Page level or at application level.Using this syntax:

<asp:Label ID="Label1" runat="server" ClientIDMode="[Mode Type]" />

Here set the Mode Type to 'legacy' or 'AutoId'.
It's done!!!
Other Mode types supported are :
Inherit
Static
Predictable


More info on these at
http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx

No comments:

Post a Comment