我创建一个时间选择器用户控件,首先显示当天的一个按钮,然后在接下来的几天:ASP.NET:按钮的OnClick事件和匿名事件不触发
|今天| |星期一| |星期二| |星期三|
当用户点击的按钮中的一个时,控制将显示下面的天按钮一套小时按钮:
|今天| |星期一| |星期二| |星期三|
| 7:00 | | 8:00 | | 9:00 | | 10:00 | | 11:00 | |中午| | 1:00 | | 2:00 |
到目前为止,我的控件工作得很好。然后,当用户点击一个小时按钮时应该发生的事情是,一小部分按钮(相隔5分钟)将在那一小时显示:
|今天| |星期一| |星期二| |星期三|
| 7:00 | | 8:00 | | 9:00 | | 10:00 | | 11:00 | |中午| | 1:00 | | 2:00 |
| 9:00 | | 9:05 | | 9:10 | | 9:15 | | 9:20 | | 9:25 | | 9:30 | | 9:35 |
而不是发生这种情况,控制似乎自行重置,并返回到它只显示日按钮的部分。小时按钮的OnClick事件甚至不会触发(我通过重载OnClick方法并将Page.Response.Write(“它的触发”)放入它中来测试它 - 当我将它放入一个重载的OnClick方法中时,当天按钮)。
这里是我的代码(一切都放在与代码的用户控制 - 在的.ascx标记页面仅包含< @%控制%>标签):
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 < 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 < 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<int, bool> availableHours = new Dictionary<int, bool>();
//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 <= 19; i++)
{
availableHours.Add(i, r.Next(5) != 0);
}
foreach (KeyValuePair<int, bool> 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 <= 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<int> availableTimes = new List<int>();
//This randomly generates available times
Random r = new Random();
for (int i = 0; i < 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;
}
}
任何人都可以解释为什么匿名方法为DayButton工作,但是当我为HourButton使用几乎相同的代码时,它不会?我确信它与ASP.NET的工作方式有关,而不是C#。
您的<@%Control%>是否启用了viewstate? – 2009-09-11 05:32:23
如果您添加了用于显示此控件的页面代码,将会非常有帮助。 – ChaosPandion 2009-09-11 18:50:11
在默认情况下。aspx页面我把这个: –
2009-09-11 19:04:29