Export (0) Print
Expand All

Control.NamingContainer Property

Gets a reference to the server control's naming container, which creates a unique namespace for differentiating between server controls with the same Control.ID property value.

Namespace: System.Web.UI
Assembly: System.Web (in system.web.dll)

'Declaration
<BindableAttribute(False)> _
Public Overridable ReadOnly Property NamingContainer As Control
'Usage
Dim instance As Control
Dim value As Control

value = instance.NamingContainer

/** @property */
public Control get_NamingContainer ()

public function get NamingContainer () : Control

Not applicable.

Property Value

The server control's naming container.

Each page in an ASP.NET Web application contains a hierarchy of controls. This hierarchy is not dependent on whether a control generates UI visible to the user. The naming container for a given control is the parent control above it in the hierarchy that implements the INamingContainer interface. A server control that implements this interface creates a unique namespace for the ID property values of its child server controls.

Creating a unique namespace for server controls is particularly important when you bind data against list Web server controls, such as the Repeater and DataList server controls. 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. The default naming container for a page is the instance of the Page class generated when that page is requested.

You can use this property to determine the naming container where a specific server control is located.

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="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>

<% @ 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="VJ#" runat="server">
     
// Build the DataSource.
ICollection CreateDataSource() 
{
    DataTable myDataTable =  new DataTable();
    DataRow myDataRow;
    myDataTable.get_Columns().Add(new DataColumn("EmployeeName",
        String.class.ToType()));
    for(int i=0;i < 10;i++) {
        myDataRow = myDataTable.NewRow();
        myDataRow.set_Item( 0 , "somename" + ((Int32)i).ToString());
        myDataTable.get_Rows().Add(myDataRow);
    } 
    DataView myDataView =  new DataView(myDataTable);
    return myDataView ;
} //CreateDataSource

void Page_Load(Object sender, EventArgs e)
{
    if (!(get_IsPostBack())) {
        // Bind 'DataView' to the DataSource.
        myDataList.set_DataSource(CreateDataSource());
        myDataList.DataBind();
    }
        // Attach EventHandler for SelectedIndexChanged event.
        myDataList.add_SelectedIndexChanged(new EventHandler(
            SelectedItemChanged));
} //Page_Load

// Handler function for 'SelectedIndexChanged' event.
void SelectedItemChanged(Object sender, EventArgs e) 
{
    DataListItem myCurrentItem = myDataList.get_SelectedItem();
    Control myNamingContainer = 
        myCurrentItem.get_Controls().get_Item(0).get_NamingContainer();
    
    // Display the NamingContainer.
    myLabel1.set_Text("The NamingContainer is : " 
        + myNamingContainer.get_UniqueID());
    
    // Display the UniqueID.
    myLabel2.set_Text("The UniqueID is : "
        +((Control)(myCurrentItem.get_Controls().get_Item(0))).get_UniqueID());
} //SelectedItemChanged
    </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.get_DataItem(), "EmployeeName") %>' CommandName="select" runat="server" />
               &nbsp;&nbsp;&nbsp;&nbsp;
            </ItemTemplate>
         </asp:DataList>
      </form>
   </body>
</html>

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft