ASP.NET 4.0 Web Control ClientID

Posted by: Suprotim Agarwal , on 12/24/2011, in Category ASP.NET
Views: 183821
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 –


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.

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’ –

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 –


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

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for ten consecutive times. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by clubmild on Sunday, March 31, 2013 7:36 AM
boleh kasi tutorial software yg mempunyai cracker winzzar IP
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
Comment posted by 23 on Friday, February 6, 2015 12:02 AM
Comment posted by 23 on Friday, February 6, 2015 12:02 AM