次の方法で共有


Control.NamingContainer プロパティ

同じ Control.ID プロパティ値を持つ複数のサーバー コントロールを区別するための一意の名前空間を作成する、サーバー コントロールの名前付けコンテナへの参照を取得します。

Public Overridable ReadOnly Property NamingContainer As Control
[C#]
public virtual Control NamingContainer {get;}
[C++]
public: __property virtual Control* get_NamingContainer();
[JScript]
public function get NamingContainer() : Control;

プロパティ値

サーバー コントロールの名前付けコンテナ。

解説

ASP.NET Web アプリケーションの各ページには、コントロールの階層構造が含まれています。この階層構造は、コントロールがユーザーから参照できる UI を生成するかどうかに依存しません。特定のコントロールの名前付けコンテナは、 INamingContainer インターフェイスを実装する階層構造において、そのコントロールの上位にある親コントロールです。このインターフェイスを実装するサーバー コントロールは、子サーバー コントロールの ID プロパティ値の一意の名前空間を作成します。

サーバー コントロールの一意の名前空間の作成は、 Repeater サーバー コントロールおよび DataList サーバー コントロールなどの Web サーバー コントロールのリストに対してデータを連結するときに特に重要です。データ ソースの複数のエントリが、連続しているコントロールの子サーバー コントロールの複数インスタンスを作成する場合、名前付けコンテナは子コントロールの各インスタンスが競合しない UniqueID プロパティ値を確実に持つように設定します。ページに対する既定の名前付けコンテナは、そのページが要求されたときに生成された Page クラスのインスタンスです。

このプロパティを使用して、特定のサーバー コントロールがある名前付けコンテナを確認できます。

使用例

[Visual Basic, C#] NamingContainer プロパティを使用する例を次に示します。

 
<%@ 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>

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 2000, Windows XP Professional, Windows Server 2003 ファミリ

参照

Control クラス | Control メンバ | System.Web.UI 名前空間 | INamingContainer | UniqueID | Control.ID