févr. 22

In the project I currently work in, I see a few confusion between the use of UniqueID and ClientID we can do server-side or client-side. So it's time to recall the differences between the two.

If you have a Textbox (runat server) with an Id, let's say "MyControl", you will have

  • UniqueId : ctl00$MyControl
  • ClientId : ctl00_MyControl

where

  • the prefix (here "ctl00") will vary depending of the nesting of INamingContainer controls
  • "$" is a constant, defined in Control.IdSeparator and that may be overriden in the page itself ot in the web.config file
  • "_" is a constant, defined in Control.ClientIDSeparator (and that may be overriden in control's derived classes)

What does it will generate client side ?

<input name="ctl00$MyControl" type="text" id="ctl00_MyControl" />

So to be brief :

  • UniqueID = HTML's name attribute
  • ClientID = HTML's id attribute

Where the confusion comes from ? Probably from your respective favourite browsers. Indeed you will probably end by doing some JavaScript and so doing a getElementById. The only thing is that IE will be more permissive and will find your control if you do a getElementById(Element's name), but FireFox wil fail.

In a single word : ClientID = HTML's id attribute and so when doing getElementById, you should always give the ClientId.

Tags: |

Commentaires

Ajouter un commentaire




biuquote
  • Commentaire
  • Aperçu immédiat
Loading