Control.UniqueID Control.UniqueID Control.UniqueID Control.UniqueID Property

定義

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

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

屬性值

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

範例

下列範例會建立ArrayList物件並填入三個文字字串,然後繫結RepeaterWeb 伺服器控制項中的資料,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();
      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>
  <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>

備註

此屬性與不同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. 重複的控制項,也就是RepeaterDataListDetailsViewFormView,和GridViewWeb 伺服器控制項 (或任何自訂伺服器控制項,您會建立包含重複的功能,當資料繫結),做為命名容器及其子控制項。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.

比方說,如果您包含 ASP.NET Label Web 伺服器控制項,在Repeater伺服器控制項,並指派Label控制ID屬性值MyLabel,而Repeater IDMyRepeater.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. 如果您要的資料繫結RepeaterArrayList具有三個項目,所產生的物件UniqueID每個執行個體的屬性Label伺服器控制項都是MyRepeater$ctl00$MyLabelMyRepeater$ctl01$MyLabel,和MyRepeater$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.

適用於

另請參閱