C# asp.net Why is there a difference between ClientID and UniqueID?

时间秒杀一切 提交于 2019-11-27 02:34:34

问题


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?


回答1:


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




回答2:


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



来源:https://stackoverflow.com/questions/1612016/c-sharp-asp-net-why-is-there-a-difference-between-clientid-and-uniqueid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!