Client ID generation in ASP. net4.0

Source: Internet
Author: User
Tags visual studio 2010
Some time last year, I began to explore improvements to ASP. net4.0 web forms. I found some exciting improvements in ASP. net4.0, and I confirm that all of these will make web development easier and provide us with more flexibility. So I picked out these exciting ASP. net4.0 features one by one. Earlier, I wrote Article You can click the following link to view details.
URL routing in ASP. NET 4.0

As described in the title, you know how to control the generation of the Client ID of the ASP. NET Server Control in ASP. NET 4.0. Earlier, this client ID was another mysterious thing for me, but later I found that the. NET engine generated the server-side control IDAlgorithm.

Visual Studio 2010

Why is the Client ID:
Client ID is often a problem, especially for applications in the new era.ProgramWe are more inclined to enrich the client programming of Internet applications. Over the past few years, many technologies and programming methods have been formed for creating rich UIS such as jquery, JSON, and dojo.

In Dom, the client ID of the control to be accessed plays an important role. Therefore, Microsoft also tries to make our programming life easier by providing the function of controlling Client ID generation in RIA development that can ensure simpler and fewer errors.

Client ID of previous versions:
Let me discuss how client IDs are generated in earlier versions.

First, let's start with the standard control textbox or label. The client ID generated here. The prefix is the prefix of all top-down named containers separated by underscores. This is indeed a good idea to generate a unique ID on the client. However, as I have discussed, client ID is an important part of application development in the new era. Let's take a look at a simple textbox Server Control example:

Download Attachment(44.59 KB)

From the above picture, we can see that my label and textbox are in a contentplaceholder, and then let's take a look at the client ID:

Download Attachment(34.14 KB)

The client ID here is ctl00_contentplaceholder1_mytextbox. If we start from ctl00 one by one (the following line is the separator), the next is the ID of contentplaceholder, and the next is the ID of textbox.

It is worth noting that when you move the control to another location, the client ID will automatically change.

Although we know that the page will generate a unique ID, you must prevent any control from exceeding the control range of the page .. Net engine will automatically generate a client ID for you based on her algorithm.

Now, let's talk about some data controls, such as gridview, listview, and dropdownlist. What are these controls used? We only bind a data source to the control. When running, the control line is automatically generated based on the data. What about the client ID here? The client ID is automatically generated and prefixed with a row number, as we have discussed. Take a look at this example.

The gridview in my aspxCode. Here I show the ID, bookname, and price.

Download Attachment(49.9 KB)

In the preceding example, A gridview is used. There are three labels in different template columns in the gridview. The gridview is in contentplaceholder. Check the Client ID:

Download Attachment(58.37 KB)

You can see ids like ctl00_contentplaceholder1_gridviewbooks_ctl02_lblid, which are generated by the. net engine in sequence and followed by the label ID.

This ID is not easy to use.

With the development of Web development in the new era, client ID becomes an important part of web development when you do a lot of work on the client.

Client ID generation of controls in ASP. NET 4.0
ASP. NET 4.0 provides an effective way to control the generation of client IDs. In ASP. NET 4.0, a new property clientidmode is provided to process the generation of the control Client ID. This property allows us to customize how to generate the control Client ID. clientidmode provides four options:
* Autoid
* Static
* Predictable
* Inherit

Let's discuss it one by one.

Autoid: the function of this attribute is the same as that of the Client ID generated in the previous. Net version. If you do not want to change the function generated by the Client ID of the previous version, specify this value.

Static: indicates that you want the control to have a static ID. When you know that the Control ID is unique on the page, you should use this option .. The net engine generates the same client ID without adding any suffix or prefix. This mode is mainly used for standard single control. Let's take a look at the example.


Download Attachment(16.74 KB)

As you can see in, I set the clientidmode of label to autoid, and set the textbox to static. Now let's take a look at the generated Client ID.

Download Attachment(21.84 KB)

You can see that the client ID of the label is the same as that of the previous version because I set the clientidmode value to auto.

But for Textbox, I set it to static, so its client ID is the same as what we set on the ASPX page. This is the beauty of ASP. net4.0. If we set it to static, the. NET engine uses the Control ID as the client ID without generating a new client ID.

Note: If the mode we set is static and the page does not have controls with the same client ID, the page contains controls with the same client ID. This may be disastrous when we access it from the client.

Predictable: This is another client ID generation mode that I like. You can use this option when you are not sure whether the ID is unique on the page. This value allows us to predict the Client ID on the rendering page. When you set this option, you need to set more attributes based on your choice.

I modified the aspx code in the above example as follows:

Download Attachment(48.33 KB)

Because the data control is used, we cannot set the option to static because multiple controls (one row of data) are generated based on the data ).

We use the predictable mode so that we can predict what the Control ID will be. We need to set another clientidrowsuffix attribute. I will set its value to the value of the ID column.

Check the Client ID.

Download Attachment(50.41 KB)

The client ID of the control is maincontent_gridviewbooks_lblname_1. If we look deeper, we find that there is no counter at all. The first is the ID of contentplaceholder, the ID of the gridview, the label ID, and the suffix ID.

Inherit: the Control ID is generated in the same way as the parent control. This is the default option of the control.

Set attributes at all levels

In many cases, you can set the clientidmode attribute. It can be a control level, a page level, or an application level. The results are the same. We can set the page command as follows:
1 minute agoUpload

Download Attachment(45.93 KB)

To set the application level, you need to set it in the configuration file. The setting will be applied to all pages in the application:
1 minute agoUpload

Download Attachment(20.79 KB)

Translation is hard. Please indicate the source for reprinting.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.