Skip to main content
.NET Framework Class Library
ControlUniqueID Property

Gets the unique, hierarchically qualified identifier for the server control.

Namespace:   System.Web.UI
Assembly:  System.Web (in System.Web.dll)
Syntax
<[%$TOPIC/931h7hde_en-us_VS_110_1_0_0_0_0%](False)> _
Public Overridable ReadOnly Property UniqueID As [%$TOPIC/931h7hde_en-us_VS_110_1_0_0_0_1%]
[[%$TOPIC/931h7hde_en-us_VS_110_1_0_1_0_0%](false)]
public virtual [%$TOPIC/931h7hde_en-us_VS_110_1_0_1_0_1%] UniqueID { get; }
[[%$TOPIC/931h7hde_en-us_VS_110_1_0_2_0_0%](false)]
public:
virtual property [%$TOPIC/931h7hde_en-us_VS_110_1_0_2_0_1%]^ UniqueID {
	[%$TOPIC/931h7hde_en-us_VS_110_1_0_2_0_2%]^ get ();
}
[<[%$TOPIC/931h7hde_en-us_VS_110_1_0_3_0_0%](false)>]
abstract UniqueID : [%$TOPIC/931h7hde_en-us_VS_110_1_0_3_0_1%] with get
[<[%$TOPIC/931h7hde_en-us_VS_110_1_0_3_0_2%](false)>]
override UniqueID : [%$TOPIC/931h7hde_en-us_VS_110_1_0_3_0_3%] with get

Property Value

Type: SystemString
The fully qualified identifier for the server control.
Remarks

This property differs from the ID property, in that the UniqueID property includes the identifier for the server control's naming container. This identifier is generated automatically when a page request is processed.

This property is particularly important in differentiating server controls contained within a data-binding server control that repeats. The repeating control, which are Repeater, DataList, DetailsView, FormView, and GridView Web server controls (or any custom server controls that you create that include repeating functionality when data bound), serves as the naming container for its child controls. This means that it creates a unique namespace for its child controls so that their ID property values do not conflict.

For example, if you include an ASP.NET Label Web server control in a Repeater server control, and assign the Label control an ID property value of MyLabel, and the Repeater an ID of MyRepeater. If you bind data to the Repeater to an ArrayList object with three entries, the resulting UniqueID properties for each instance of the Label server controls are MyRepeater$ctl00$MyLabel, MyRepeater$ctl01$MyLabel, and MyRepeater$ctl02$MyLabel.

Examples

The following example creates an ArrayList object and populates it with three text strings, then binds a Repeater Web server control to the data in the ArrayList when the page is loaded. The code gets the UniqueID property for each child control generated during data binding. The code generates three versions of the Label control and writes their UniqueID property values to the page.

  <script language="vb" runat="server">

      Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
          Dim sb As New StringBuilder()
          sb.Append("Container: " + _
          MyDataList.NamingContainer.ToString() + "<p>")

          Dim a As New ArrayList()
          a.Add("A")
          a.Add("B")
          a.Add("C")

          MyDataList.DataSource = a
          MyDataList.DataBind()

          Dim i As Integer
          Dim l As Label
          For i = 0 To MyDataList.Controls.Count - 1
              l = CType(CType(MyDataList.Controls(i), RepeaterItem).FindControl("MyLabel"), Label)
              sb.Append("Container: " & _
                 CType(MyDataList.Controls(i), RepeaterItem).NamingContainer.ToString() & _
                 "<p>")
              sb.Append("<b>" & l.UniqueID.ToString() & "</b><p>")
          Next
          ResultsLabel.Text = sb.ToString()
      End Sub
</script>
<script language="c#" runat="server">

  void Page_Load(Object sender, EventArgs e) 
  {
      StringBuilder sb = new StringBuilder();
      sb.Append("Container: " + 
          MyDataList.NamingContainer.ToString() + "<p>");

      ArrayList a = new ArrayList();
      a.Add("A");
      a.Add("B");
      a.Add("C");

      MyDataList.DataSource = a;
      MyDataList.DataBind();

      for (int i = 0; i < MyDataList.Controls.Count; i++)
      {
          Label l = 
              (Label)((RepeaterItem)MyDataList.Controls[i]).FindControl("MyLabel");
          sb.Append("Container: " + 
              ((RepeaterItem)MyDataList.Controls[i]).NamingContainer.ToString() + 
              "<p>");
          sb.Append("<b>" + l.UniqueID + "</b><p>");
      }
      ResultsLabel.Text = sb.ToString();
}
</script>
Version Information

.NET Framework

Supported in: 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0
Platforms

Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.