the difference between ClientID and ID of web control

本文介绍了ASP.NET中Web控件的ID和ClientID的使用区别。ID用于服务器编程,ClientID用于客户端。ID在页面中可能不唯一,而ClientID是唯一的。在客户端操作控件时,应使用ClientID而非ID。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值