Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

Control.UniqueID Property

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

[Visual Basic]
Public Overridable ReadOnly Property UniqueID As String
[C#]
public virtual string UniqueID {get;}
[C++]
public: __property virtual String* get_UniqueID();
[JScript]
public function get UniqueID() : String;

Property Value

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, and DataGrid 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:ctl0:MyLabel, MyRepeater:Ctl1:MyLabel, and MyRepeater:Ctl2:MyLabel.

Example

[Visual Basic, C#] The following example creates an ArrayList object and populates it with three text strings, then binds the data in the ArrayList to a Repeater Web server control when the page is loaded. When a user clicks the button associated with the Button1_Click method, the UniqueID property is obtained for each child control of the Repeater that was added when the DataBind method was called.

[Visual Basic] 

      Private Sub Page_Load(sender As Object, e As EventArgs)
         ' Intialise ArrayList.
         Dim myArray As New ArrayList()
         myArray.Add("Hello")
         myArray.Add("World")
         myArray.Add("!")

         ' Bind ArrayList to Repeater control.
         Repeater1.DataSource = myArray
         Repeater1.DataBind()
      End Sub

      Private Sub Button1_Click(sender As Object, e As EventArgs)
         Dim i As Integer
         For i = 0 To Repeater1.Controls.Count - 1
            Response.Write("<br>")
            Response.Write(Repeater1.Controls(i).UniqueID)
         Next i
      End Sub


[C#] 
private void Page_Load(object sender, EventArgs e)
{
   // Intialise ArrayList.
   ArrayList myArray = new ArrayList();
   myArray.Add("Hello");
   myArray.Add("World");
   myArray.Add("!");

   // Bind ArrayList to Repeater control.
   Repeater1.DataSource = myArray;
   Repeater1.DataBind();
}

private void Button1_Click(object sender, EventArgs e)
{
   for (int i=0; i<Repeater1.Controls.Count; i++)
   {
      Response.Write("<br>");
      Response.Write(Repeater1.Controls[i].UniqueID);
   }
}

[C++, JScript] No example is available for C++ or JScript. To view a Visual Basic or C# example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Control Class | Control Members | System.Web.UI Namespace | ID | NamingContainer | Postback Event Sample

Show:
© 2014 Microsoft. All rights reserved.