Control.NamingContainer Proprietà

Definizione

Ottiene un riferimento al contenitore dei nomi del controllo server, che crea uno spazio dei nomi univoco per distinguere i controlli server che hanno lo stesso valore della proprietà 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

Valore della proprietà

Contenitore dei nomi del controllo server.

Attributi

Esempio

Nell'esempio seguente viene illustrato l'utilizzo della NamingContainer proprietà .

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

Commenti

Ogni pagina di un'applicazione Web ASP.NET contiene una gerarchia di controlli. Questa gerarchia non dipende dal fatto che un controllo generi l'interfaccia utente visibile all'utente. Il contenitore di denominazione per un determinato controllo è il controllo padre sopra di esso nella gerarchia che implementa l'interfaccia INamingContainer . Un controllo server che implementa questa interfaccia crea uno spazio dei nomi univoco per i ID valori delle proprietà dei relativi controlli server figlio. È possibile usare la NamingContainer proprietà del controllo figlio di un contenitore di denominazione per ottenere un riferimento al relativo contenitore padre.

La creazione di uno spazio dei nomi univoco per i controlli server è particolarmente importante quando si associano controlli server Web ai dati, ad esempio i Repeater controlli server e DataList . Quando più voci nell'origine dati creano più istanze di un controllo server figlio del controllo ripetuto, il contenitore di denominazione garantisce che ogni istanza di questi controlli figlio abbia UniqueID valori di proprietà che non sono in conflitto. Il contenitore di denominazione predefinito per una pagina è l'istanza della Page classe generata quando viene richiesta tale pagina.

La ClientID proprietà contiene il valore di cui viene eseguito il rendering come attributo dell'elemento id nel markup HTML. A seconda del valore assegnato alla ClientIDMode proprietà, il valore generato per la ClientID proprietà potrebbe includere l'ID dell'oggetto NamingContainer . Quando si imposta su ClientIDModeStatic, il ClientID valore non include l'ID dell'oggetto NamingContainer . Quando si imposta su ClientIDModeAutoID o Predictable, il ClientID valore includerà l'ID dell'oggetto NamingContainer . Per altre informazioni, vedere ASP.NET Web Server Control Identification (Identificazione del controllo server Web ASP.NET).

Si applica a

Vedi anche