Thursday, March 19, 2009

Control.ClientID in ASP.NET 4.0

One of the repeated question in ASP.NET forums is “How to access the server-control from client-side?” I’ve wrote an article regarding it. You can find it here.

With the release of ASP.NET 4.0, MSFT development team is introducing an alternate approach for accessing the server-side controlID from client-side. With the release of 4.0, a new property called ClientIDMode has been introduced to control the behavior of the ID displayed in client-side.

The ClientIDMode property has 4  modes and are as follows:

a) Legacy 
In this mode, the ControlID behaves as in framework 2.0, 3.0 and 3.5). That means a textbox in a Master-Page may looks like ctl00_MasterPageBody_ctl01_Textbox1.

b) Inherit 
This is the default value for every control. This mode looks at the controls parent to get its value for ClientID.

c) Static
As the name suggests, it makes the ClientID static. But keep in mind, if you declare 2 controls-id as same; you’ll be popped-out.

d) Predictable.
This models commonly used along with databound controls. The framework traverse the control hierarchy pre-fixing the supplied ID along with its Parent’s ControlID until it reaches a control in the hierarchy whose ClientIDMode is static. For a control placed in a databound control, a suffix with a value that identifies that instance will be added to the supplied ID . The clientIDRow suffix property is used to control the value that will be used as a suffix, e.g: Gridview1_Label1_0.

I know you’ll be eager to see some examples. A more detailed explanations along with the examples can be found at ASP.NET  Weblogs.

No comments:

 
Best viewed in Internet Explorer 8.