Control.UniqueID Property


取得伺服器控制項唯一的、符合階層架構的識別項。Gets the unique, hierarchically qualified identifier for the server control.

 virtual property System::String ^ UniqueID { System::String ^ get(); };
public virtual string UniqueID { get; }
member this.UniqueID : string
Public Overridable ReadOnly Property UniqueID As String

Property Value


伺服器控制項的完整識別項。The fully qualified identifier for the server control.



下列範例會建立 ArrayList 物件,並在其中填入三個文字字串,然後將 Repeater 的 Web 服務器控制項系結至載入頁面時 ArrayList 中的資料。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. 此程式碼會取得資料系結期間所產生之每個子控制項的 UniqueID 屬性。The code gets the UniqueID property for each child control generated during data binding. 程式碼會產生三個版本的 Label 控制項,並將其 UniqueID 屬性值寫入頁面。The code generates three versions of the Label control and writes their UniqueID property values to the page.

<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();

      MyDataList.DataSource = a;

      for (int i = 0; i < MyDataList.Controls.Count; i++)
          Label l = 
          sb.Append("Container: " + 
              ((RepeaterItem)MyDataList.Controls[i]).NamingContainer.ToString() + 
          sb.Append("<b>" + l.UniqueID + "</b><p>");
      ResultsLabel.Text = sb.ToString();
  <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()

          MyDataList.DataSource = a
          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() & _
              sb.Append("<b>" & l.UniqueID.ToString() & "</b><p>")
          ResultsLabel.Text = sb.ToString()
      End Sub


這個屬性與 ID 屬性不同,因為 UniqueID 屬性包含伺服器控制項命名容器的識別碼。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. 重複控制項,這是 RepeaterDataListDetailsViewFormViewGridView Web 服務器控制項(或您所建立的任何自訂伺服器控制項,其中包括資料系結時的重複功能),做為其子控制項的命名容器。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. 這表示它會為其子控制項建立唯一的命名空間,使其 ID 屬性值不會發生衝突。This means that it creates a unique namespace for its child controls so that their ID property values do not conflict.

例如,如果您在 Repeater 伺服器控制項中包含 ASP.NET Label Web 服務器控制項,並將 Label 控制項指派 MyLabelID 屬性值,以及 Repeater ID MyRepeaterFor 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. 如果您將資料系結 Repeater 至具有三個專案的 ArrayList 物件,則 Label 伺服器控制項的每個實例所產生的 UniqueID 屬性會是 MyRepeater$ctl00$MyLabelMyRepeater$ctl01$MyLabelMyRepeater$ctl02$MyLabelIf 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.

Applies to

See also