Pages

Thursday, May 24, 2012

ASP.NET 4.0 Web Control ClientID


n ASP.NET, each web control is identified uniquely using the ‘ID’ property of the control. We can access the server controls in the code behind using this ‘ID’ property. However, when the web form with server side controls renders in the form of HTML, the server side ID gets converted into client side ID. But for client-side developers, it becomes difficult when they want to access HTML elements using JavaScript. The reason is that the ‘ID’ property gets generated at runtime and is not known at design time. Although you can use expressions to access them, the code is not very readable.
In ASP.NET 4.0, Microsoft has given developers the control to render client side ID of server controls, by introducing a new property called ‘ClientIDMode’. It offers the following options to render the server side ‘ID’ at client end –
  1. AutoID – this is the same as previous web server control ‘ID’ rendering at client side.
  2. Static – this option keeps the server ID name intact even at the client side. For example if the Textbox ID at server side is ‘txtCustomerID’ then at client side, it will remain as it is.
  3. Predictable – this option is used for repeating templates like Gridview or Listview.
  4. Inherit – ClientID will be generated the same way as Parent ID. For example, if the ParentID is ‘MainContent’ then the Textbox ‘txtCustomerID’ will be ‘MainContent_txtCustomerID’.
Now let’s see some examples of the same. Create an ASP.NET 4.0 website with the name ‘ClientIDExample’ as shown below –
asp.net-clientid-project

Now let’s add the following pages to the application –
  • InheritIDPage.aspx.
  • AutoIDPage.aspx.
  • StaticIDPage.aspx.
  • PredictableID.aspx.
Make sure that you choose the ‘Site.master’ page when you add the above pages. Now let’s add code for the ‘AutoIDPage.aspx’ –
Note: I have used document.writeln just for illustration purposes. It won’t work in a document that’s served as XHTML.
asp.net-autoid-clientid
In the above code, we are using JavaScript to print the client ID which is rendered from server side to client side.
When you run this page, you will see an output similar to the one shown below –
Now write the following code in our ‘StaticIDPage.aspx’ –
asp.net-staticid
Run this page and you should see the following output –
ASP.NET ClientID Static
Now let’s add the following code in ‘InheritIDPage.aspx’ –
ASP.NET ClientID Inherit
ASP.NET ClientID Predictable
In this demo, we are using the Northwind database and Employee table. In the ‘GridView’ control, we are setting two properties – ‘ClientIDMode’ and ‘ClientIDRowSuffix’. The ClientIDRowSuffix property is used during client ID generation. Now run this page. Right click the page in browser and view the source code. You will see the following output –
clip_image002
You can control the web control client ID generation at Page level as well as web site level. For eg: include ClientIDMode property at Page directive as shown below –
ClientID Mode Page level
If you want to set the ClientIDMode at web site level, you can use the web.config settings as shown below –
ClientID Mode Webconfig level
In this post, we have seen how easy it is to take control of Client ID generation as per our requirement. The entire source code of this article can be downloaded over here

No comments:

Post a Comment