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对象,并使用三个文本字符串填充该对象, 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();
      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. 重复Repeater控件,为DetailsViewDataList GridView 、、和 Web 服务器控件(或者创建的任何自定义服务器控件,这些控件在绑定数据时包含重复功能)作为FormView命名其子控件的容器。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在服务器Label Label控件中包括 ASP.NET Web 服务器控件, ID并将的属性值MyLabel Repeater ID 分配给控件,则将MyRepeater.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. Repeater如果将数据ArrayList绑定到具有三个条目的对象,则Label服务器控件UniqueID MyRepeater$ctl00$MyLabel的每个实例的结果属性为、 MyRepeater$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.

适用于

另请参阅