Enumerating through the Page Controls Demo

This demo illustrates iterating through the Controls collection.


A List of the Controls in the Controls Collection

What's your name?


Source Code
<%@ Page Language="C#" %>
<script runat="server">

    void IterateThroughChildren(Control parent)
    {
      foreach (Control c in parent.Controls)
      {
        lblControlList.Text += "<li>" + c.ToString() + "</li>";
        if (c.Controls.Count > 0)
        {
          lblControlList.Text += "<ul>";
          IterateThroughChildren(c);
          lblControlList.Text += "</ul>";
        }
      }
    }

    void Page_Load(Object sender, EventArgs e)
    {      
      lblControlList.Text += "<ul>";
      IterateThroughChildren(this);
      lblControlList.Text += "</ul>";
    }

</script>

<html>
<body>
<h1>Enumerating through the Page Controls Demo</h1>
This demo illustrates iterating through the <code>Controls</code> collection.
<p><hr><p>
    <b>A List of the Controls in the <code>Controls</code> Collection</b><br>
    <asp:label runat="server" id="lblControlList" />
    <p>
    <form runat="server">
        What's your name?
        <asp:textbox runat="Server" id="txtName" />
    </form>


[Return to the article]