Basically, The ID of web control should be used on server
programming, and the ClientID should be used on Client, pretty basic.
if you want to use the ID of web control on Client, you will find it
doesn't work, because ASP.NET will not set it down to Client.
and the ID maybe is not unique in a page, a simplest example is:
1. create a web form and a web user control.
2. create a button on the web user control, it's ID should be Button1.
3. create a button on the web form, it's ID should be Button1 too.
4. put the web user control on the web form, now, in this web form, we have two buttons which name are both "Button1".
when we are programming on the server, I can use the Button1 to identify the button in web form and web user control both because they are in different scenario.
But when in client, there's only one page, right? so ASP.NET will generate ClientID automatically so that they are different. If you view the source of the final page in web brower, you can find the Button in web user control's id property changed(I think it's exactly ClientID, not the ID what we discuss here) to WebUserControl1_Button1(in my project, maybe different in your project, but the key is that it has been changed.)
let me summarize where we are, the ID and ClientID of web control are different, ClientID unique in a web page, but ID not, because the "web page" is a Client term, right? when we are programming on server, we concentrate over web form, web control, commonly not web page. so if we want to operate control on client, for example: register a script to page using Register..... function to operate control on client, we should inject control's ClientID into script, not the ID.
and the ID maybe is not unique in a page, a simplest example is:
1. create a web form and a web user control.
2. create a button on the web user control, it's ID should be Button1.
3. create a button on the web form, it's ID should be Button1 too.
4. put the web user control on the web form, now, in this web form, we have two buttons which name are both "Button1".
when we are programming on the server, I can use the Button1 to identify the button in web form and web user control both because they are in different scenario.
But when in client, there's only one page, right? so ASP.NET will generate ClientID automatically so that they are different. If you view the source of the final page in web brower, you can find the Button in web user control's id property changed(I think it's exactly ClientID, not the ID what we discuss here) to WebUserControl1_Button1(in my project, maybe different in your project, but the key is that it has been changed.)
let me summarize where we are, the ID and ClientID of web control are different, ClientID unique in a web page, but ID not, because the "web page" is a Client term, right? when we are programming on server, we concentrate over web form, web control, commonly not web page. so if we want to operate control on client, for example: register a script to page using Register..... function to operate control on client, we should inject control's ClientID into script, not the ID.
本文介绍了ASP.NET中Web控件的ID和ClientID的使用区别。ID用于服务器编程,ClientID用于客户端。ID在页面中可能不唯一,而ClientID是唯一的。在客户端操作控件时,应使用ClientID而非ID。

被折叠的 条评论
为什么被折叠?



