How to: Access Members of a Control's Naming Container

At times, you need to access properties or methods of a control's naming container. You can access the containing control in different ways, depending on context.

To access the naming container from a data-binding expression

  • In the data-binding expression, use the Container keyword, which returns a reference to the container.

    The following example shows a Label control that is inside of the ItemTemplate of a GridView control. It displays the current item number followed by the Title data item of the naming container.

    <%@ Page Language="VB" %>
    
    <!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 runat="server">
        <title>Naming Container Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:XmlDataSource ID="XmlDataSource1" 
                               runat="server" 
                               XPath="Books/LanguageBooks/Book">
            <Data>
             <Books>
                <LanguageBooks>
                  <Book Title="Pure JavaScript" 
                        Author="Wyke, Gilliam, and Ting"/>
                  <Book Title="Effective C++ Second Edition" 
                        Author="Scott Meyers"/>
                  <Book Title="Assembly Language Step-By-Step" 
                        Author="Jeff Duntemann"/>
                  <Book Title="Oracle PL/SQL" 
                        Author="Steven Feuerstein"/>
                </LanguageBooks>
                <SecurityBooks>
                  <Book Title="Counter Hack" 
                        Author="Ed Skoudis"/>
                </SecurityBooks>
              </Books>
            </Data>
            </asp:XmlDataSource>
            <asp:GridView ID="GridView1" 
                          runat="server" 
                          DataSourceID="XmlDataSource1" 
                          AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField HeaderText="Title" >
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server">
                            <%# Container.DataItemIndex + 1 %>. <%# Eval("Title") %>
                            </asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
        </form>
    </body>
    </html>
    
    <%@ Page Language="C#" %>
    
    <!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 runat="server">
        <title>Naming Container Example</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:XmlDataSource ID="XmlDataSource1" 
                               runat="server" 
                               XPath="Books/LanguageBooks/Book">
            <Data>
             <Books>
                <LanguageBooks>
                  <Book Title="Pure JavaScript" 
                        Author="Wyke, Gilliam, and Ting"/>
                  <Book Title="Effective C++ Second Edition" 
                        Author="Scott Meyers"/>
                  <Book Title="Assembly Language Step-By-Step" 
                        Author="Jeff Duntemann"/>
                  <Book Title="Oracle PL/SQL" 
                        Author="Steven Feuerstein"/>
                </LanguageBooks>
                <SecurityBooks>
                  <Book Title="Counter Hack" 
                        Author="Ed Skoudis"/>
                </SecurityBooks>
              </Books>
            </Data>
            </asp:XmlDataSource>
            <asp:GridView ID="GridView1" 
                          runat="server" 
                          DataSourceID="XmlDataSource1" 
                          AutoGenerateColumns="False">
                <Columns>
                    <asp:TemplateField HeaderText="Title" >
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server">
                            <%# Container.DataItemIndex + 1 %>. <%# Eval("Title") %>
                            </asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
            </asp:GridView>
        </div>
        </form>
    </body>
    </html>
    

To access the naming container from code

  • Get the control's NamingContainer property and cast it to the container's class type.

    Note

    The NamingContainer property might not reference the same control as the Parent property. For example, in a Repeater control, an item template might contain a table that in turn contains a Label control. In that case, the parent control of the Label control is a table cell (for example, a HtmlTableCell object), but its naming container is the DataListItem object. This is because the DataListItem defines the namespace for the Label control, not the table.

    The following example shows how to walk the control tree of an ASP.NET Web page. The button's ChangeBtn_Click method handler searches for a control named Message in the first item of a Repeater control by using the FindControl method, and then determines the NamingContainer object for that control. The code then determines the naming container for the control returned by the first call to the NamingContainer property, and so on up the control tree until it finds a control that has no naming container. (The WalkContainers method will also add the type of the control at the lowest level, which is not a naming container.)

    <html xmlns="http://www.w3.org/1999/xhtml" >
    
    <head id="head1" runat="server">
        <title>NamingContainer Example</title>
    </head>
    
    <script language="vb" runat="server">
    
        Dim list As ArrayList
    
        Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    
            list = New ArrayList
    
            list.Add("One")
            list.Add("Two")
            list.Add("Three")
            MyRepeater.DataSource = list
            MyRepeater.DataBind()
        End Sub
    
        Private Sub ChangeBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
    
            Dim x As Control = MyRepeater.Items(0).FindControl("Message")
    
            If Not x Is Nothing Then
                list = WalkContainers(x)
            End If
            MyRepeater.DataSource = list
            MyRepeater.DataBind()
        End Sub
    
        Private Function WalkContainers(ByVal ctl As Control) As ArrayList
    
            Dim ret As New ArrayList
            Dim parent As Control = ctl.NamingContainer
    
            If Not parent Is Nothing Then
                Dim sublist As ArrayList = WalkContainers(parent)
                Dim j As Integer
                For j = 0 To sublist.Count - 1
                    ret.Add(sublist(j))
                Next
            End If
            ret.Add(ctl.GetType.Name)
            Return ret
        End Function
    
    </script>
    <body>
    <form id="repeaterform" runat="server">
      <h3>Using the NamingContainer Property to Determine a 
          Control's Naming Container
      </h3>
      <table id="mytable" width="80%">
          <asp:repeater id="MyRepeater" runat="server">
          <ItemTemplate>
            <tr>
              <td align="center" style="width:100%;">
               <span id="message" runat="server">
               <%#Container.DataItem%>
               </span>
              </td>
            </tr>
          </ItemTemplate>
          </asp:repeater>
        <tr>
          <td align="center" style="width:100%;">
          <input id="changebtn" 
                 type="submit" 
                 onserverclick="changebtn_click "
                 runat="server" />
           </td>
        </tr>
      </table>
    
    </form>
    </body>
    </html>
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    
    <head id="head1" runat="server">
        <title>NamingContainer Example</title>
    </head>
    
    <script language="c#" runat="server">
    
        ArrayList list;
    
        private void Page_Load(object sender, EventArgs e)
        {
            list = new ArrayList();
            list.Add("One");
            list.Add("Two");
            list.Add("Three");
            MyRepeater.DataSource = list;
            MyRepeater.DataBind();
        }
    
        private void ChangeBtn_Click(object sender, EventArgs e)
        {
            Control x = MyRepeater.Items[0].FindControl("Message");
            if (x != null) list = WalkContainers(x);
            MyRepeater.DataSource = list;
            MyRepeater.DataBind();
        }
    
        private ArrayList WalkContainers(Control ctl)
        {
            ArrayList ret = new ArrayList();
            Control parent = ctl.NamingContainer;
            if (parent != null)
            {
                ArrayList sublist = WalkContainers(parent);
                for (int j = 0; j < sublist.Count; j++) ret.Add(sublist[j]);
            }
            ret.Add(ctl.GetType().Name);
            return ret;
        }
    </script>
    
    
    <body>
    <form id="repeaterform" runat="server">
      <h3>Using the NamingContainer Property to Determine a 
          Control's Naming Container
      </h3>
      <table id="mytable" width="80%">
          <asp:repeater id="MyRepeater" runat="server">
          <ItemTemplate>
            <tr>
              <td align="center" style="width:100%;">
               <span id="message" runat="server">
               <%#Container.DataItem%>
               </span>
              </td>
            </tr>
          </ItemTemplate>
          </asp:repeater>
        <tr>
          <td align="center" style="width:100%;">
          <input id="changebtn" 
                 type="submit" 
                 onserverclick="ChangeBtn_Click"
                 runat="server" />
           </td>
        </tr>
      </table>
    
    </form>
    </body>
    </html>
    

See Also

Reference

Data-Binding Expression Syntax

Concepts

ASP.NET Web Server Control Identification