Share via


Using the NamingContainer Property to Determine a Control's Naming Container

You can walk up the control tree on a page by means of the NamingContainer property. In contrast to the Container keyword, which is available only in inline code (that is, in a declarative <%#   %> expression), the NamingContainer property is available in code to any instance of that class or of a derived class.

The following code example illustrates 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 using the FindControl method, and then determines the NamingContainer object for that control. It 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. (Note that the WalkContainers method will also add the type of the control at the lowest level, which is not itself 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

Concepts

Web Forms Control ID Resolution