ClientID and UniqueID are definitely different. In the simplest sense, ClientID is used for client-side, UniqueID is used for server-side. However, there are more subtle differences between them.
ClientID:
- Used with id attribute of rendered HTML tag
- Uses underscore as separator (e.g. ParentPanel_ChildControl)
- MSDN ClientID reference
- <%=%>, combined with $get or $find, can be used to access the ClientID thru JavaScript. It is executed at the rendering stage of the Page, and is included to the page’s rendering output (HTML, most likely). <%=%> does never run at client-side. It is server-side code. You just use it to “spit” output between the content on the aspx page, so that it will impact on the end result which is rendered at the client. For information on $get and $find, check here.
UniqueID:
- Used with name attribute
- Uses colon as separator (e.g. ParentPanel:ChildControl)
- MSDN UniqueID reference
With regard to the separator explained above, I’ve encountered one exception, whereas the UniqueID separator is “&”. I have a button and a update panel on a page, with the button being the AsyncPostbackTrigger for the update panel. When trying to access the button via its ID in code behind, the ID rendered and written to the HTML is not the button’s ClientID, but its UniqueID, which then has “&”, instead of colon (:), as the separator. The only explanation I can think of for such behavior is due to the postback triggers for the update panel. Trial and error always help. 🙂
In addition, both ClientID and UniqueID can be overridden to be identical all around, of course. Just simply assign them with the ID attribute of the control.
/// Override to force simple IDs all around
/// </summary>
public override string UniqueID
{
get
{
return this.ID;
}
}
/// <summary>
/// Override to force simple IDs all around
/// </summary>
public override string ClientID
{
get
{
return this.ID;
}
}
By Bryan Xu