Control.NamingContainer Control.NamingContainer Control.NamingContainer Control.NamingContainer Property

定義

取得伺服器控制項命名容器的參考,其建立唯一命名空間,在具有相同 ID 屬性值的伺服器控制項之間作區別。Gets a reference to the server control's naming container, which creates a unique namespace for differentiating between server controls with the same ID property value.

public:
 virtual property System::Web::UI::Control ^ NamingContainer { System::Web::UI::Control ^ get(); };
[System.ComponentModel.Bindable(false)]
[System.ComponentModel.Browsable(false)]
public virtual System.Web.UI.Control NamingContainer { get; }
member this.NamingContainer : System.Web.UI.Control
Public Overridable ReadOnly Property NamingContainer As Control

屬性值

伺服器控制項的命名容器。The server control's naming container.

範例

下列範例示範如何使用NamingContainer屬性。The following example demonstrates using the NamingContainer property.

<% @ Import Namespace = "System.Data"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <head>
    <title>
            Control NamingContainer Example
         </title>
<script language="C#" runat="server">
      // Build the DataSource.
      ICollection CreateDataSource()
      {
         DataTable myDataTable = new DataTable();
         DataRow myDataRow;
         myDataTable.Columns.Add(new DataColumn("EmployeeName", typeof(string)));
         for (int i = 0; i < 10; i++) 
         {
            myDataRow = myDataTable.NewRow();
            myDataRow[0] = "somename" + i.ToString();
            myDataTable.Rows.Add(myDataRow);
         }
         DataView myDataView = new DataView(myDataTable);
         return myDataView;
      }

      void Page_Load(Object sender, EventArgs e) 
      {
         if (!IsPostBack) 
         {
            // Bind 'DataView' to the DataSource.
            myDataList.DataSource = CreateDataSource();
            myDataList.DataBind();
         }
         // Attach EventHandler for SelectedIndexChanged event.
         myDataList.SelectedIndexChanged += new EventHandler(selectedItemChanged);
      }

      // Handler function for 'SelectedIndexChanged' event.
      void selectedItemChanged(Object sender,EventArgs e)
      {
         DataListItem myCurrentItem = myDataList.SelectedItem;
         Control myNamingContainer = myCurrentItem.Controls[0].NamingContainer;
         // Display the NamingContainer.
         myLabel1.Text = "The NamingContainer is : " + myNamingContainer.UniqueID;
         // Display the UniqueID.
         myLabel2.Text = "The UniqueID is : " + ((Control)(myCurrentItem.Controls[0])).UniqueID;
      }      

    </script>
   </head>
   <body>
      <form runat="server" id="Form1">
         <h3>
            Control NamingContainer Example
         </h3>
         <h4>
            Click an item to view it's Naming Container and UniqueID
         </h4>
         <asp:Label ID="myLabel1" Runat="server"></asp:Label>
         <br />
         <asp:Label ID="myLabel2" Runat="server"></asp:Label>
         <br />
         <asp:DataList id="myDataList" runat="server" BorderColor="black">
            <HeaderStyle BackColor="#aaaadd"></HeaderStyle>
            <SelectedItemStyle BackColor="lightgreen"></SelectedItemStyle>
            <HeaderTemplate>
               EmployeeName
            </HeaderTemplate>
            <ItemTemplate>
               <asp:LinkButton id="button1" Text='<%# DataBinder.Eval(Container.DataItem, "EmployeeName") %>' CommandName="select" runat="server" />
               &nbsp;&nbsp;&nbsp;&nbsp;
            </ItemTemplate>
         </asp:DataList>
      </form>
   </body>
</html>
<%@ Import Namespace = "System.Data"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
   <head>
    <title>
            Control NamingContainer Example
         </title>
<script language="VB" runat="server">
      ' Build the DataSource.
      Function CreateDataSource() As ICollection
         Dim myDataTable As New DataTable()
         Dim myDataRow As DataRow
         myDataTable.Columns.Add(New DataColumn("EmployeeName", GetType(String)))
         Dim i As Integer
         For i = 0 To 9
            myDataRow = myDataTable.NewRow()
            myDataRow(0) = "somename" + i.ToString()
            myDataTable.Rows.Add(myDataRow)
         Next i
         Dim myDataView As New DataView(myDataTable)
         Return myDataView
      End Function

      Sub Page_Load(sender As Object, e As EventArgs)
         If Not IsPostBack Then
            ' Bind 'DataView' to the DataSource.
            myDataList.DataSource = CreateDataSource()
            myDataList.DataBind()
         End If
         ' Attach EventHandler for SelectedIndexChanged event.
         AddHandler myDataList.SelectedIndexChanged, AddressOf selectedItemChanged
      End Sub

      ' Handler function for 'SelectedIndexChanged' event.
      Sub selectedItemChanged(sender As Object, e As EventArgs)
         Dim myCurrentItem As DataListItem = myDataList.SelectedItem
         Dim myNamingContainer As Control = myCurrentItem.Controls(0).NamingContainer
         ' Display the NamingContainer.
         myLabel1.Text = "The NamingContainer is : " + myNamingContainer.UniqueID
         ' Display the UniqueID.
         myLabel2.Text = "The UniqueID is : " + CType(myCurrentItem.Controls(0), Control).UniqueID
      End Sub
    </script>
   </head>
   <body>
      <form runat="server" id="Form1">
         <h3>
            Control NamingContainer Example
         </h3>
         <h4>
            Click an item to view it's Naming Container and UniqueID
         </h4>
         <asp:Label ID="myLabel1" Runat="server"></asp:Label>
         <br />
         <asp:Label ID="myLabel2" Runat="server"></asp:Label>
         <br />
         <asp:DataList id="myDataList" runat="server" BorderColor="black">
            <HeaderStyle BackColor="#aaaadd"></HeaderStyle>
            <SelectedItemStyle BackColor="lightgreen"></SelectedItemStyle>
            <HeaderTemplate>
               EmployeeName
            </HeaderTemplate>
            <ItemTemplate>
               <asp:LinkButton id="button1" Text='<%# DataBinder.Eval(Container.DataItem, "EmployeeName") %>' CommandName="select" runat="server" />
               &nbsp;&nbsp;&nbsp;&nbsp;
            </ItemTemplate>
         </asp:DataList>
      </form>
   </body>
</html>

備註

ASP.NET Web 應用程式中的每個頁面包含控制項階層的架構。Each page in an ASP.NET Web application contains a hierarchy of controls. 此階層不相依於控制項是否會產生對使用者顯示的 UI。This hierarchy is not dependent on whether a control generates UI visible to the user. 指定控制項的命名容器是上面的父控制項實作的階層中INamingContainer介面。The naming container for a given control is the parent control above it in the hierarchy that implements the INamingContainer interface. 伺服器控制項來實作此介面會建立唯一命名空間ID其子伺服器控制項的屬性值。A server control that implements this interface creates a unique namespace for the ID property values of its child server controls. 您可以使用NamingContainer命名容器的子控制項,以取得其父容器的參考屬性。You can use the NamingContainer property of a naming container's child control to get a reference to its parent container.

當您繫結的 Web 伺服器控制項的資料,例如建立伺服器控制項的唯一命名空間是特別重要RepeaterDataList伺服器控制項。Creating a unique namespace for server controls is particularly important when you bind Web server controls to data, such as the Repeater and DataList server controls. 當資料來源中的多個項目建立多個重複的控制項的子系的伺服器控制項執行個體時,可確保命名的容器,這些子控制項的每個執行個體有UniqueID不衝突的屬性值。When multiple entries in the data source create multiple instances of a server control that is a child of the repeating control, the naming container ensures that each instance of these child controls have UniqueID property values that do not conflict. 頁面的預設命名容器是的執行個體Page要求該頁面時,會產生的類別。The default naming container for a page is the instance of the Page class that is generated when that page is requested.

ClientID屬性包含會轉譯為元素的值id的 HTML 標記中的屬性。The ClientID property contains the value that is rendered as the element's id attribute in the HTML markup. 您將指派給的值而定ClientIDMode屬性,產生的值ClientID屬性可能包含的識別碼NamingContainer物件。Depending on the value that you assign to the ClientIDMode property, the value that is generated for the ClientID property might include the ID of the NamingContainer object. 當您設定ClientIDModeStatic,則ClientID值不包含識別碼NamingContainer物件。When you set ClientIDMode to Static, the ClientID value does not include the ID of the NamingContainer object. 當您設定ClientIDMode設為AutoIDPredictable,則ClientID值會包含來自識別碼NamingContainer物件。When you set ClientIDMode to either AutoID or Predictable, the ClientID value will include the ID from the NamingContainer object. 如需詳細資訊,請參閱 ASP.NET 網頁伺服器控制項識別For more information, see ASP.NET Web Server Control Identification.

適用於

另請參閱