C# asp.net 为什么ClientID和UniqueID之间有区别?

发布于 2024-08-08 12:01:33 字数 169 浏览 4 评论 0原文

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

余生再见 2024-08-15 12:01:34

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

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

但是我认为有一些推理可能是在普通控件 ID 中使用下划线作为分隔符是非常常见的行为,因此下划线不能在 UniqueID 中用作控件分隔符(如果我们理论上认为使用一个属性进行管理),因为您无法区分控件。另一方面,出于同样的原因,您不能在控件 ID 中使用冒号,页面框架不允许这样做,因此它确保冒号无法到达 ClientID(这是因为 JavaScript 不喜欢它) 。

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

(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

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).

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).

尝蛊 2024-08-15 12:01:34

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

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文