The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Control.NamingContainer Property


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

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)

public virtual Control NamingContainer { get; }

Property Value

Type: System.Web.UI.Control

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. You can use the NamingContainer property of a naming container's child control to get a reference to its parent container.

Creating a unique namespace for server controls is particularly important when you bind Web server controls to data, 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 that is generated when that page is requested.

The ClientID property contains the value that is rendered as the element's id attribute in the HTML markup. Depending on the value that you assign to the ClientIDMode property, the value that is generated for the ClientID property might include the ID of the NamingContainer object. When you set ClientIDMode to Static, the ClientID value does not include the ID of the NamingContainer object. When you set ClientIDMode to either AutoID or Predictable, the ClientID value will include the ID from the NamingContainer object. For more information, see ASP.NET Web Server Control Identification.

The following example demonstrates using the NamingContainer property.

<% @ Import Namespace = "System.Data"  %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<html xmlns="" >
            Control NamingContainer Example
<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();
         DataView myDataView = new DataView(myDataTable);
         return myDataView;

      void Page_Load(Object sender, EventArgs e) 
         if (!IsPostBack) 
            // Bind 'DataView' to the DataSource.
            myDataList.DataSource = CreateDataSource();
         // 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;

      <form runat="server" id="Form1">
            Control NamingContainer Example
            Click an item to view it's Naming Container and UniqueID
         <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>
               <asp:LinkButton id="button1" Text='<%# DataBinder.Eval(Container.DataItem, "EmployeeName") %>' CommandName="select" runat="server" />

.NET Framework
Available since 1.1
Return to top