C#asp.net为什么ClientID和UniqueID之间存在差异?

时间:2022-11-25 12:53:56

I know ClientID is used for javascript and UniqueId for server side and that ClientID uses an underscore (_) and UniqueId uses a dollar sign ($) in asp.net 2.0. But what I don't get is why use two different id's. Why isn't possible to just OR use the underscore OR use the dollar sign in both: server and client side. Can someone explain this?

我知道ClientID用于服务器端的javascript和UniqueId,而ClientID使用下划线(_),而UniqueId在asp.net 2.0中使用美元符号($)。但我没有得到的是为什么使用两个不同的id。为什么不能只使用或使用下划线或在服务器端和客户端使用美元符号。有人可以解释一下吗?

2 个解决方案

#1


12  

(In addition to my original answer above)

(除了我上面的原始答案)

Well, as you probably know UniqueID is used with name attribute and ClientId with id attribute of rendered HTML tag. UniqueID uses colon as separator. On the other hand ClientId uses underscore as separator, because colon is not allowed in JavaScript variable names. ClientID is indeed also unique on the Page as UniqueID is, but ClientID is targeted at client-side processing and UniqueID for server-side (pretty obvious),the latter especially to route for postback data and events with composite controls

好吧,您可能知道UniqueID与name属性一起使用,ClientId与呈现HTML标记的id属性一起使用。 UniqueID使用冒号作为分隔符。另一方面,ClientId使用下划线作为分隔符,因为JavaScript变量名称中不允许使用冒号。 ClientID在Page上确实也是唯一的,因为UniqueID是,但ClientID的目标是客户端处理,而UniqueID用于服务器端(非常明显),后者特别用于路由回发数据和事件与复合控件

However I think some reasoning might be that using underscore as separator in normal Control IDs is pretty common behavior and therefore underscore cannot be used in UniqueID as control separator (if we'd theoretically think managing with one property), because you couldn't make distinction between controls. On the other hand for the same reasoning, you can't use colon in Control IDs, Page Framework does not allow it, so that it makes sure colons can't get to the ClientIDs (this was because of JavaScript does not like it).

但是我认为一些推理可能是在正常控件ID中使用下划线作为分隔符是非常常见的行为,因此下划线不能在UniqueID中用作控件分隔符(如果我们理论上认为使用一个属性进行管理),因为你无法制作控制之间的区别。另一方面,出于同样的原因,你不能在Control ID中使用冒号,Page Framework不允许它,这样就可以确保冒号无法访问ClientID(这是因为JavaScript不喜欢它) 。

And for these reasons, colon is pretty good choice to be used in UniqueID, because FindControl method can use it to navigate Control tree and locate controls (it can easily split the UniqueID).

由于这些原因,冒号是在UniqueID中使用的相当不错的选择,因为FindControl方法可以使用它来导航控制树和定位控件(它可以轻松地拆分UniqueID)。

#2


6  

this.UniqueID is the control's name appended with all naming containers, think of it as the fully qualified control name. this.ClientID is the value that will appear in the id attribute of the control, it is a translation of the uniqueid to be a (well almost) html compliant id tag (leading underscores are not really html compliant).

this.UniqueID是附加所有命名容器的控件名称,将其视为完全限定的控件名称。 this.ClientID是将出现在控件的id属性中的值,它是uniqueid的转换,是一个(几乎)符合html的id标记(前导下划线不是真正符合html)。

#1


12  

(In addition to my original answer above)

(除了我上面的原始答案)

Well, as you probably know UniqueID is used with name attribute and ClientId with id attribute of rendered HTML tag. UniqueID uses colon as separator. On the other hand ClientId uses underscore as separator, because colon is not allowed in JavaScript variable names. ClientID is indeed also unique on the Page as UniqueID is, but ClientID is targeted at client-side processing and UniqueID for server-side (pretty obvious),the latter especially to route for postback data and events with composite controls

好吧,您可能知道UniqueID与name属性一起使用,ClientId与呈现HTML标记的id属性一起使用。 UniqueID使用冒号作为分隔符。另一方面,ClientId使用下划线作为分隔符,因为JavaScript变量名称中不允许使用冒号。 ClientID在Page上确实也是唯一的,因为UniqueID是,但ClientID的目标是客户端处理,而UniqueID用于服务器端(非常明显),后者特别用于路由回发数据和事件与复合控件

However I think some reasoning might be that using underscore as separator in normal Control IDs is pretty common behavior and therefore underscore cannot be used in UniqueID as control separator (if we'd theoretically think managing with one property), because you couldn't make distinction between controls. On the other hand for the same reasoning, you can't use colon in Control IDs, Page Framework does not allow it, so that it makes sure colons can't get to the ClientIDs (this was because of JavaScript does not like it).

但是我认为一些推理可能是在正常控件ID中使用下划线作为分隔符是非常常见的行为,因此下划线不能在UniqueID中用作控件分隔符(如果我们理论上认为使用一个属性进行管理),因为你无法制作控制之间的区别。另一方面,出于同样的原因,你不能在Control ID中使用冒号,Page Framework不允许它,这样就可以确保冒号无法访问ClientID(这是因为JavaScript不喜欢它) 。

And for these reasons, colon is pretty good choice to be used in UniqueID, because FindControl method can use it to navigate Control tree and locate controls (it can easily split the UniqueID).

由于这些原因,冒号是在UniqueID中使用的相当不错的选择,因为FindControl方法可以使用它来导航控制树和定位控件(它可以轻松地拆分UniqueID)。

#2


6  

this.UniqueID is the control's name appended with all naming containers, think of it as the fully qualified control name. this.ClientID is the value that will appear in the id attribute of the control, it is a translation of the uniqueid to be a (well almost) html compliant id tag (leading underscores are not really html compliant).

this.UniqueID是附加所有命名容器的控件名称,将其视为完全限定的控件名称。 this.ClientID是将出现在控件的id属性中的值,它是uniqueid的转换,是一个(几乎)符合html的id标记(前导下划线不是真正符合html)。