Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Client Ids Generation with ASP.NET 4.0

0.00/5 (No votes)
11 Sep 2010 1  
This article discusses about one of the new features Client ID generation of ASP.NET 4.0

Table of Contents

Introduction

From the past few days, I started exploring ASP.NET 4.0 webforms enhancement. I really found some exciting enhancements in ASP.NET 4.0 and am sure, this is all going to be make web development simple and will provide more flexibility to us. So I started picking the most exciting features of ASP.NET 4.0 one by one. Earlier, I wrote on URL Routing, you can go through to the link given below:

As form the Title, you know that here I am going to discuss how we can control the generation of Client Ids of ASP.NET Server controls in ASP.NET 4.0. This Client Ids was another thing that was earlier a mystery for me, but later I identified the algorithm that is used to generate the ClientIds for the server controls by the .NET engine, but they have never been user friendly.

Prerequisite

  • Visual Studio 2010

Why Client Ids

Client Ids have always been a problem for us. But now a days, in new age applications, we are moving more towards the client side programming in new Rich Internet applications. Many new technologies and the way of programming have evolved in the last few years to make very rich UI like JQuery, JSon, Dojo.

In DOM, to access a control, client id plays a pivotal role. So Microsoft is also trying to make our life easier by providing the capability to have control over the client id generation which will ensure easy, simple and less error prone RIA development.

Client Ids Earlier

So let's discuss how the ClientIds were generated earlier. First, I will start with normal control, say textbox control or a label. So here the client Ids that are generated were starting with prefix of all the naming containers from top to bottom separated as underscore. And actually this was a good idea to generate the unique id at client side. But as I discussed, ClientIds are the key part of new age development. Let's look at the example for a simple textbox server control. My aspx looks like:

Normal Page

Fig: Normal Page

So from the above picture, we can see that my label and textbox are inside a contentplaceholder. Now let's look at the ClientId:

View Source - Normal Page

Fig: View Source - Normal Page

Now here client Id is ctl00_ContentPlaceHolder1_myTextBox. If we go one by one, the ctl00 is the counter, the next one is ID of contentplaceholder and next id of textbox.

So one thing, as you move the control to some other part, the Client Id will get changed as well.

So although we know the ID is going to be unique on the page, but still you don't have any control over itFrown. .NET engine will generate the ClientIds according to its algorithm for you ).

So this is all about of the simple controls. Now lets talk about some data controls, like gridview, listview, dropdown, etc. Here in these controls, what we do is we just bind the datasource to the control. And at runtime based on the data, the number of rows(controls) are generated. So what about the client Ids here. Here also, the Client Ids are being generated in the same way as I already discussed with prefix of rownumber. So let's have a look at the example.

This is my aspx code for GridView. Here I am showing ID, Book Name and Price:

Page with data control

Fig: Page with data control

So in the above example, I have taken a gridview. And in this, I have three labels in different ItemTemplates. The gridview is in contentplaceholder.

Now look to the ClientIds:

View Source - Page with data control

Fig: View Source - Page with data control

You can see the id is like ctl00_ContentPlaceHolder1_gridviewBooks_ctl02_lblID. So here if we go one by one, first the counter the contentplaceholder id again rowcounter generated in sequence for every row by .NET engine to make it unique and last label ID.

So it becomes very uneasy to use.

But as in new era web development, when we doing lots of work at client side, the Client ids become a key part in web development.

Control Client Ids Generation with ASP.NET 4.0

ASP.NET 4.0 provides the power to control Client Ids generation. For that, it provides the new property ClientIDMode property to handle this. This property enables us to specify how the Client Ids will be generated for the controls. This provides four options:

  • AutoID
  • Static
  • Predictable
  • Inherit

We'll discuss them one by one.

AutoID: This property is the same as the earlier version of .NET. Specify this value if you don't want any changes in the Client Id generation from the earlier version as I discussed in ClientIDs in earlier versions.

Static: This means the you want the static id of your control. You should use this property when you know the ID is going to be unique on the page. Here .NET engine will generate the Client Ids as it is, without adding any suffixes or prefixes in it. This mode is mainly used for normal single control. Let's look at the example.

Normal page with ASP.NET 4.0

Fig: Normal page with ASP.NET 4.0

Here as you seen in the picture, I set the ClientIDMode for Label AutoID and for TextBox I set it Static. Now let's see the generated Client Ids:

Brij_634099931624398203_normalpage4.jpg

Fig: View Source: Normal page with ASP.NET 4.0

Now if you see the client ID of Label, it is the same as the earlier one because here I set it to Auto.

But for the TextBox, I set it static. So here the Client Id is the same as the ID that we set it on aspx page. This is the beauty of ASP.NET 4.0. So if we set it to static, the .NET engine won't generate a new client id for the control; it will use the ID of the control as Client ID.

Note: One thing needs to be made sure here, if we set the mode to static then there should be no control on the page with the same id because it will have the same client id on the page and it may be disastrous when we access it from Clientside.

Predictable: This is another mode that I liked for the ClientId generation. When you exactly don't know whether the Id is unique on the page or not, then you can use this property. This value enables us to the predict the client ids on the rendered page. When you set mode to this, you need to set some more properties according to your own choice.

Now I will take the example as above. Now here the aspx code would be like:

Data control page with ASP.NET 4.0

Fig: Data control page with ASP.NET 4.0

Here one thing is we are using datacontrol, then we cannot set it as static because there are going to be multiple controls generated based on the data.

So here we will be using mode as Predictable so that we can predict what will be the id of the control. We need to set one more property ClientIDRowSuffix here. I set it ID means the ID column.

Now let's look at the generated Client Ids:

View Source: Data control page with ASP.NET 4.0

Fig: View Source: Data control page with ASP.NET 4.0

Now here if we look at the ClientID, here it is MainContent_gridviewBooks_lblName_1. So if we look at it deeply, then we find that there is no counter like thing. Here we have first id of contentplaceholder, the id of gridview, the id of label, the suffix id that we set. So it's really predictable Smile and similarly for other rows.

Inherit: This is also value to set to this property. As the name suggests, it means the Id generation of the control will be the same as the parent control. This is the default behavior of the control.

Setting the Property at Various Levels

There are various places where we can set the ClientIDMode property.This can be set at control level, page level as well as application. The behavior will be the same. We can set it at page directive as below:

ClientIDMode at Page level

Fig: ClientIDMode at Page level

To set it at Application level, we need to set it in config file as:

and that will be applied across all the pages in the application.

ClientIDMode at Application level

Fig: ClientIDMode at Application level

Feedback and Suggestions

Feedback is key for improvement. I would request you all to share your feedback and give me some suggestions, which would encourage and help in more and quality writing.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here