ASP.NET: button's OnClick event and anonymous event not firing
<p>I am creating a time picker user control that first shows a button for the current day, then the next few days:</p>

<p>| Today | | Monday | | Tuesday | | Wednesday |</p>

<p>When a user clicks on one of the buttons, the control will display a set of hour buttons below the day buttons:</p>

<p>| Today | | Monday | | Tuesday | | Wednesday |</p>

<p>| 7:00 | | 8:00 | | 9:00 | | 10:00 | | 11:00 | | Noon | | 1:00 | | 2:00 |</p>

<p>My control works just fine up to this point. Then, what is supposed to happen when the user clicks on an hour button is that a set of minute buttons (5 minutes apart) will show up for that hour:</p>

<p>| Today | | Monday | | Tuesday | | Wednesday |</p>

<p>| 7:00 | | 8:00 | | 9:00 | | 10:00 | | 11:00 | | Noon | | 1:00 | | 2:00 |</p>

<p>| 9:00 | | 9:05 | | 9:10 | | 9:15 | | 9:20 | | 9:25 | | 9:30 | | 9:35 |</p>

<p>Instead of this happening, the control seems to reset itself and go back to the part where it shows only the day buttons. The OnClick event for the hour buttons does not even fire (I tested it by overloading the OnClick method and putting Page.Response.Write("its firing") in it -- this did work when I put it in an overloaded OnClick method in the day button).</p>

<p>Here's my code (everything is put on the user control with code -- the .ascx markup page contains only the &lt;@% Control %> tag):</p>

<pre><code>public partial class TimePicker : System.Web.UI.UserControl
{
    public TimePicker()
    {
        this.Controls.Add(new TeeTimePanel());
    }

    private class TimePanel : Panel
    {
        public TimePanel()
        {
            Days = new Panel();
            this.Controls.Add(Days);

            Hours = new Panel();
            this.Controls.Add(Hours);

            Minutes = new Panel();
            this.Controls.Add(Minutes);

            DayButton.CreateDayButtons(this);
        }

        public void CreateHourButtons(DateTime day)
        {
            HourButton.CreateHourButtons(day, this);
        }

        public void CreateMinuteButtons(DateTime time) 
        {
            MinuteButton.CreateMinuteButtons(time.Date, time.Hour, this);
        }

        public Panel Days, Hours, Minutes;
    }

    private class DayButton : Button
    {
        private DayButton(DateTime day, TimePanel parent)
        {
            this.date = day;

            if (day.Date == DateTime.Today)
            {
                this.Text = "Today";
            }
            else
            {
                this.Text = day.ToString("dddd");
            }

            this.Click += delegate
            {
                parent.CreateHourButtons(day);
            };
        }

        private DateTime date;

        public static void CreateDayButtons(TimePanel parent)
        {
            for (int i = 0; i &lt; 5; i++)
            {
                DayButton b = new DayButton(DateTime.Today.AddDays(i), parent);
                parent.Days.Controls.Add(b);
            }
        }
    }

    private class HourButton : Button
    {
        private HourButton(DateTime day, int hour, bool enabled, TimePanel parent)
        {
            this.Enabled = enabled;
            this.time = day.Date.AddHours(hour);

            if (hour &lt; 12)
            {
                this.Text = hour.ToString() + ":00";
            }
            else if (hour == 12)
            {
                this.Text = "Noon";
            }
            else
            {
                this.Text = (hour - 12).ToString() + ":00";
            }

            this.Click += delegate
            {
                parent.CreateMinuteButtons(day.AddHours(hour));
            };
        }

        public static void CreateHourButtons(DateTime day, TimePanel parent) 
        {
            Dictionary&lt;int, bool&gt; availableHours = new Dictionary&lt;int, bool&gt;();

            //This randomly generates available hours and will eventually
            //be replaced with code that accesses a database
            Random r = new Random();
            for (int i = 7; i &lt;= 19; i++)
            {
                availableHours.Add(i, r.Next(5) != 0);
            }

            foreach (KeyValuePair&lt;int, bool&gt; kvp in availableHours)
            {
                HourButton b = new HourButton(day.Date, kvp.Key, kvp.Value, parent);
                parent.Hours.Controls.Add(b);
            }
        }

        private DateTime time;
    }

    private class MinuteButton : Button
    {
        private MinuteButton(DateTime day, int hour, int minute)
        {
            this.time = day.Date.AddHours(hour).AddMinutes(minute);

            if (hour &lt;= 12)
            {
                this.Text = hour.ToString() + ":";
            }
            else
            {
                this.Text = (hour - 12).ToString() + ":";
            }

            this.Text += minute.ToString("d2");
        }

        public static void CreateMinuteButtons(DateTime day, int hour, Panel target)
        {
            List&lt;int&gt; availableTimes = new List&lt;int&gt;();

            //This randomly generates available times
            Random r = new Random();
            for (int i = 0; i &lt; 60; i += 5)
            {
                if (r.Next(2) != 0)
                {
                    availableTimes.Add(i);
                }
            }

            foreach (int i in availableTimes)
            {
                MinuteButton b = new MinuteButton(day, hour, i);
                target.Controls.Add(b);
            }
        }

        private DateTime time;
    }
}
</code></pre>

<p>Can anyone explain why the anonymous method does work for the DayButton, but when I use nearly identical code for the HourButton, it doesn't? I am convinced it has something to do with how ASP.NET works rather than C#.</p>
