UniqueID
The UniqueID property is the page-wide unique identifier of an ASP.NET server control. Its uniqueness is guaranteed by prefixing the ID of a server control with the ID of its NamingContainer. If the NamingContainer is the Page the UniqueID will remain the same as the ID.
For example if a Label with ID="Label1" is defined in a user control with ID = "UserControl1" the UniqueID of the Label will be "UserControl1$Label1". Adding another instance of the same user control (with ID = "UserControl2") will make the UniqueID of its child label to be "UserControl2$Label1".
The UniqueID property is also used to provide value for the HTML "name" attribute of input fields (checkboxes, dropdown lists, and hidden fields). UniqueID also plays major role in postbacks. The UniqueID property of a server control, which supports postbacks, provides data for the __EVENTTARGET hidden field. The ASP.NET Runtime then uses the __EVENTTARGET field to find the control which triggered the postback and then calls its RaisePostBackEvent method. Here is some code which illustrates the idea:
IPostBackEventHandler postBackInitiator =
Page.FindControl(Request.Form["__EVENTTARGET") As IPostBackEventHandler;
if (postBackInitiator != null)
postBackInitiator.RaisePostBackEvent(Request.Form["__EVENTARGUMENT"]);
You can use the UniqueID property to find any control no matter how deep it is nested in the control hierarchy. Just pass its value to the FindControl method of the Page.
<asp:Label ID="Label1" Text="Label" />
will render as this:
<span id="Label1">Label</span>
That’s why you often use the following JavaScript statement to access the DOM element corresponding to some ASP.NET server control:
var label = document.getElementById("<%= Label1.ClientID%>");
var label = document.getElementById("Label1");
It is worth mentioning that the values of the ID, UniqueID and ClientID will be the same if the control is defined in the master page (or the page). This however can often lead to unexpected errors. If the ID of the control is hardcoded inside the JavaScript statement (e.g. "Label1") this code will only work provided the control is defined in the Page or master page. Moving the control and the JavaScript code into a userc control with ID "UserControl1" will fail at runtime because the control will now render as:
<span id="UserControl1_Label1">Label</span>
That’s why you should prefer using the "<%= Label1.ClientID%>" syntax to get the client-side id of server controls.
Additionaly the ClientID is used in ASP.NET Ajax as the unique identifier of client-side controls. Thus the following JavaScript statement is commonly used:
var control = $find("<%= MyControl1.ClientID %>");
I have attached a sample web site which
demonstrates the difference between those three properties of controls defined in a page (without a master), master page, content placeholder and user control. Here are two screenshots from the output:
I hope this helps.
Iana Tsolova is Product Manager at Telerik’s DevTools division. She joined the company back in the beginning of 2008 as a Support Officer and has since occupied various positions at Telerik, including Senior Support Officer, Team Lead at one of the ASP.NET AJAX teams and Technical Support Director. Iana’s main interests are web development, reading articles related to geography, wild nature and latest renewable energy technologies.