.NET Framework 類別庫
Control.NamingContainer 屬性

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

命名空間: System.Web.UI
組件: System.Web (在 system.web.dll 中)

語法

Visual Basic (宣告)
<BindableAttribute(False)> _
Public Overridable ReadOnly Property NamingContainer As Control
Visual Basic (使用方式)
Dim instance As Control
Dim value As Control

value = instance.NamingContainer
C#
[BindableAttribute(false)] 
public virtual Control NamingContainer { get; }
C++
[BindableAttribute(false)] 
public:
virtual property Control^ NamingContainer {
    Control^ get ();
}
J#
/** @property */
public Control get_NamingContainer ()
JScript
public function get NamingContainer () : Control

屬性值

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

ASP.NET Web 應用程式中的各個網頁都包含控制項的階層架構。這個階層架構不受控制項是否產生使用者可見的 UI 的影響。指定控制項的命名容器為階層架構中在它之上而實作 INamingContainer 介面的父控制項。實作這個介面的伺服器控制項將替其子伺服器控制項的 ID 屬性值建立唯一命名空間。

建立伺服器控制項的唯一命名空間,在針對清單 Web 伺服器控制項 (例如 RepeaterDataList 伺服器控制項) 繫結資料時尤其重要。當資料來源中的多個項目建立伺服器控制項的多個執行個體 (也就是重複控制項的子控制項) 時,命名容器確保這些子控制項的各個執行個體具有不相衝突的 UniqueID 屬性值。網頁的預設命名容器為那個網頁被要求時所產生的 Page 類別執行個體。

您可以使用這個屬性,決定特定伺服器控制項所在的命名容器。

TopicLocation
HOW TO:存取 Web 伺服器控制項命名空間的成員建置 ASP .NET Web 應用程式
HOW TO:存取 Web 伺服器控制項命名空間的成員在 Visual Studio 中建立 ASP .NET Web 應用程式
範例

下列範例示範 NamingContainer 屬性的用法。

Visual Basic
<%@ Import Namespace = "System.Data"  %>
<html>
   <head>
      <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>
C#
<% @ Import Namespace = "System.Data"  %>
<html>
   <head>
      <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>
平台

Windows 98、 Windows 2000 SP4、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition

.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。

版本資訊

.NET Framework

支援版本:2.0、1.1、1.0
請參閱

標記 :


Page view tracker