Control.UniqueID Property
Assembly: System.Web (in system.web.dll)
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.
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.
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); } }
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.set_DataSource(myArray);
Repeater1.DataBind();
} //Page_Load
private void Button1_Click(Object sender, EventArgs e)
{
for(int i=0;i < Repeater1.get_Controls().get_Count();i++) {
get_Response().Write("<br>");
get_Response().Write(
Repeater1.get_Controls().get_Item(i).get_UniqueID());
}
} //Button1_Click
Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.