共用方式為


Control.NamingContainer 屬性

定義

取得伺服器控制項命名容器的參考,其建立唯一命名空間,在具有相同 ID 屬性值的伺服器控制項之間作區別。

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

屬性值

伺服器控制項的命名容器。

屬性

範例

下列範例示範如何使用 NamingContainer 屬性。

<% @ 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 應用程式中的每個頁面都包含控件階層。 此階層不相依於控件是否向用戶顯示UI。 指定控件的命名容器是實作 介面之階層 INamingContainer 中其上方的父控件。 實作這個介面的伺服器控制項會為其 ID 子伺服器控制件的屬性值建立唯一的命名空間。 您可以使用 NamingContainer 命名容器子控件的 屬性來取得其父容器的參考。

當您將 Web 伺服器控制項系結至數據時,建立伺服器控制件的唯一命名空間特別重要,例如 RepeaterDataList 伺服器控制件。 當數據源中的多個專案建立屬於重複控件子系之伺服器控件的多個實例時,命名容器可確保這些子控件的每個實例都有 UniqueID 不會衝突的屬性值。 頁面的預設命名容器是要求該頁面時所產生的類別實例 Page

屬性 ClientID 包含呈現為 HTML 標記中專案 id 之 屬性的值。 根據您指派給 ClientIDMode 屬性的值,為 ClientID 屬性產生的值可能包含 對象的標識碼 NamingContainer 。 當您設定ClientIDMode為 時,ClientID值不會包含 對象的識別碼NamingContainerStatic。 當您設定 ClientIDModeAutoIDPredictable時, ClientID 值會包含 來自 NamingContainer 物件的識別碼。 如需詳細資訊,請參閱 ASP.NET 網頁伺服器控制項識別

適用於

另請參閱