asp.net form control ID is null on enumeration
<p>I have a simple ASP.Net page, and I am enumerating through the controls doing things to the controls via reflection (localising the page - but that is whole other can of managerial dictat type worms). So...I have a test page thus:</p>

<pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/Master Pages/Root.master" AutoEventWireup="true" CodeFile="refelectiontest.aspx.cs" Inherits="refelectiontest" %&gt;

&lt;asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"&gt;

    &lt;asp:Button ID="Button1" runat="server" Text="Button" /&gt;

    &lt;asp:Label ID="Label1" runat="server" Text="Label"&gt;&lt;/asp:Label&gt;&lt;asp:Label ID="Label2"
        runat="server" Text="Label"&gt;&lt;/asp:Label&gt;

    &lt;asp:Label ID="Label3" runat="server" Text="Label"&gt;&lt;/asp:Label&gt;

&lt;/asp:Content&gt;
</code></pre>

<p>And then in the code behind, I want to iterate through the controls...</p>

<p>So I have this (the master page can deal with itself)</p>

<pre><code>protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            try
            {

                foreach (Control ctrl in Form.Controls)
                {
                    if (ctrl is ContentPlaceHolder)
                    {
                        ContentPlaceHolder chp = ctrl as ContentPlaceHolder;
                        foreach (Control control in chp.Controls)
                        {
                string strID = control.ID;
                //do stuff based on the the ID
                        }
                    }


                }

            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
        }

    }
</code></pre>

<p>Now, if I just run this, it barfs with an object null exception. And if I step through the code, putting a breakpoint on the control.ID line, then indeed the ID is null. However, if I then expand the control object, the ID fills itself out and all is well.</p>

<p>It looks like the ID is only getting completed at the last moment - how can I ensure that I can get the ID without getting null?
The same thing happens if I don't use the master page.
Thanks.</p>
