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

2

22

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?

Tanager answered 23/10, 2009 at 8:12 Comment(0)
W
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

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

Wingless answered 23/10, 2009 at 9:40 Comment(0)
W
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).

Wingless answered 23/10, 2009 at 8:20 Comment(3)
So, leading underscores are not (really) html compliant. So why don't use dollar signs? Why not use the ClientId on the server side instead of UniqueId? The're both excactly the same, the only difference is the use of the dollar sign and underscore.Tanager
ClientID's purpose is to return the uniquely identifying ID of the control (it is equivalent to UniqueID but uses underscore as control separator) that can be used in client-side scripting.Wingless
I think you don't get my point :) Javascript don't like the dollar signs and therefore it uses underscores. That's clear now. So my next question is: why don't use the underscores on the server side? Thus, why don't use ClientID on the server? Why must there be an apart property that uses dollar signs? Why can't the underscores be used on the server?Tanager

© 2022 - 2024 — McMap. All rights reserved.