我有一个asp日历视图,我在其中生成对日历日期更改的所选索引的动态控制。所以我面临一个回发问题,因为动态控件在点击后分散,所以我需要将这个选定的索引更改事件调用到页面加载中,以便动态控件保持其状态
protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
{
try
{
DateTime sDate = new DateTime();
sDate = Calendar1.SelectedDate;
sDay = sDate.ToString("MM/dd/yyyy");
string doc = ddlDoctor.SelectedValue.ToString();
DataTable dt = tsbll.GetAvailableSlotsbyDocAndDate(doc, sDay);
foreach (DataRow dr in dt.Rows)
{
Button label = new Button();
label.Text = dr["SlotTime"].ToString();
//label.Style["Left"] = "20px";
label.CssClass = "btn btn-primary";
//label.CssClass = "background - color: #19bc49;";
label.Click += new EventHandler(labelClick);
pnlLabel.Controls.Add(label);
}
}
catch (Exception ex) { throw new Exception(ex.ToString()); }
}
答案 0 :(得分:1)
您无需在SelectedIndexChanged
中调用Page_Load
事件处理程序。您必须调用生成动态控件的方法。可以从Page_Load
和事件处理程序调用此方法。
protected void GenerateDocSlotLabels(string doc, DateTime day)
{
string sDay = day.ToShortDateString();
DataTable dt = tsbll.GetAvailableSlotsbyDocAndDate(doc, sDay);
foreach (DataRow dr in dt.Rows)
{
Button label = new Button();
label.Text = dr["SlotTime"].ToString();
//label.Style["Left"] = "20px";
label.CssClass = "btn btn-primary";
//label.CssClass = "background - color: #19bc49;";
label.Click += new EventHandler(labelClick);
pnlLabel.Controls.Add(label);
}
}
现在从两个事件处理程序中调用它。例如:
protected void Calendar1_SelectionChanged1(object sender, EventArgs e)
{
DateTime sDate = Calendar1.SelectedDate;
string doc = ddlDoctor.SelectedValue.ToString();
GenerateDocSlotLabels(doc, sDate);
}
...
protected void Page_Load(object sender, EventArgs e)
{
DateTime sDate = Calendar1.SelectedDate;
string doc = ddlDoctor.SelectedValue.ToString();
GenerateDocSlotLabels(doc, sDate);
}
旁注:我删除了您的Catch
,因为它没有任何用处。应该使用throw
而非throw ex
来重新排除异常以保留原始堆栈跟踪。