ASP.NET 4.0 Web Control ClientID

Posted by: Suprotim Agarwal , on 12/24/2011, in Category ASP.NET
Views: 151533
Abstract: 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’. In this article, we will explore this property.

In 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

Give a +1 to this article if you think it was well written. Thanks!
Recommended Articles
Suprotim Agarwal, ASP.NET Architecture MVP, MCSD, MCAD, MCDBA, MCSE, is the CEO of A2Z Knowledge Visuals Pvt. He primarily works as an Architect Consultant and provides consultancy on how to design and develop .NET centric database solutions.

Suprotim is the founder and primary contributor to DotNetCurry, DNC .NET Magazine, SQLServerCurry and DevCurry. He has also written an EBook 51 Recipes using jQuery with ASP.NET Controls. and is authoring another one at The Absolutely Awesome jQuery CookBook.

Follow him on twitter @suprotimagarwal


Page copy protected against web site content infringement by Copyscape


User Feedback
Comment posted by clubmild on Sunday, March 31, 2013 7:36 AM
boleh kasi tutorial software yg mempunyai cracker winzzar IP www.M88asia.com.Thanks
Comment posted by Maik on Thursday, May 23, 2013 1:59 PM
Thank you!!!
Comment posted by Ankit Garg on Wednesday, June 26, 2013 9:59 PM
Nice article

Post your comment
Name:  
E-mail: (Will not be displayed)
Comment:
Insert Cancel