UniqueID vs. ClientID Why are they Differenct?

K

Ken Baltrinic

Can someone explain this?

I am writing an owner rendered User Control that basically wraps some
functionality around a text box. This control uses client side javascript
and implments IPostBackDataHandler. In order for my JaveScript to find the
control using GetElementByID I must emit Control.ClientID as the value of
the ID attribute of my textbox and in order for the postback handling
mechanism to link up that same textbox with my control's
PostbackDataHandler, I must emit Control.UniqueID as the value of the name
attribute.

I understand why I have to put a value in both the Name and ID attributes
but what I don't get is why are they different values? My rendered textbox
comes out looking like this:

<input id="_ctl0_datReportDate" name="_ctl0:datReportDate" ... >

Can anyone explain why these values need to be different and why we have two
properties on the Control class for them?

--Ken
 
P

pj

I understand why I have to put a value in both the Name and ID attributes
but what I don't get is why are they different values? My rendered textbox
comes out looking like this:

<input id="_ctl0_datReportDate" name="_ctl0:datReportDate" ... >

Can anyone explain why these values need to be different and why we have two
properties on the Control class for them?

I don't know the whole answer... part of it is that the id tag can't
contain colons (nor can colons be used in javascript identifiers) - so
the ClientID is simply the UniqueID with the colons replaced with
underscores. I'm not sure why MS didn't just use underscores
throughout. I'm sure someone here does know though....

pj
 
K

Ken Baltrinic

pj said:
I don't know the whole answer... part of it is that the id tag can't
contain colons (nor can colons be used in javascript identifiers) - so
the ClientID is simply the UniqueID with the colons replaced with
underscores. I'm not sure why MS didn't just use underscores
throughout. I'm sure someone here does know though....

pj

Then I suppose the next logical jump here would be that Microsoft didn't
want to use underscores as delimiters in the UniqueID because the underscore
is a valid character for naming a control and thus using it might result in
non-unique UDs if you are using a really lame UniqueID generation algorithm.
And there I think we have the ultmiate answer to the question.

I did a quick test. I created a page with a lable on it id="foo_bar", and I
created a custome control id="foo" containing a child label id="bar" on the
same page and yep, both labels render as spans with ID="foo_bar". Leave it
to microsoft to not really get it right.

--Ken
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top