protected void EventDuration_DDL_SelectedIndexChanged(object sender, EventArgs e)
{
con.Open();
DateTime dt = DateTime.Parse(StartDate_TB.Text);
int n = Int32.Parse(EventDuration_DDL.SelectedItem.ToString());
for (int i = 0; i < n; i++)
{
Label NewLabel = new Label();
NewLabel.ID = "Label" + i;
var eventDate = dt.AddDays(i);
NewLabel.Text = eventDate.ToLongDateString();
CheckBox newcheck = new CheckBox();
newcheck.ID = "CheckBox" + i;
newcheck.AutoPostBack = true;
newcheck.CausesValidation = false;
newcheck.CheckedChanged += new EventHandler(newcheck_CheckedChanged);
this.Labeldiv.Controls.Add(new LiteralControl("<span class='h1size'>"));
this.Labeldiv.Controls.Add(NewLabel);
this.Labeldiv.Controls.Add(new LiteralControl("</span>"));
this.Labeldiv.Controls.Add(new LiteralControl("<div class='make-switch pull-right' data-on='info'>"));
this.Labeldiv.Controls.Add(newcheck);
this.Labeldiv.Controls.Add(new LiteralControl("</div>"));
this.Labeldiv.Controls.Add(new LiteralControl("<br/>"));
}
con.Close();
}
protected void Done_Button_Click(object sender, EventArgs e)
{
int n = Int32.Parse(EventDuration_DDL.SelectedItem.ToString());
for (int i = 0; i < n; i++)
{
string labelId = "Label" + i.ToString();
Label NewLabel = (Label)this.Labeldiv.FindControl(labelId);
string checkBoxId = "Checkbox" + i.ToString();
CheckBox newcheck = (CheckBox)this.Labeldiv.FindControl(checkBoxId);
SqlCommand cmd = new SqlCommand("insert into EventDays(EventDay,EventStatus)values(@EventDay,@EventStatus)", con);
cmd.Parameters.AddWithValue("@EventDay", NewLabel.Text);
cmd.Parameters.AddWithValue("@EventStatus", newcheck.Checked ? "true" : "false");
cmd.ExecuteNonQuery();
}
con.Close();
}
我创建了标签&amp;在EventDuration_DDL_SelectedIndexChanged ......中动态选中复选框。
我想在选中复选框后存储它们.....
但CheckedChange事件未被触发......... ??????
答案 0 :(得分:0)
newcheck .AutoPostBack = true;
newcheck .CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
这是添加事件处理程序的方法
在代码中更改此行:
newcheck.CheckedChanged += newcheck_CheckedChanged;
答案 1 :(得分:0)
在SelectedIndexChanged中添加复选框并不是一种好方法,尤其是当您需要绑定服务器事件时。因为在页面的“LoadViewState”时它们不可用。
最好的方法是,
添加复选框&amp;然后,aspx页面中的标签会设置Visible="false"
并绑定所需的事件。
然后,您只需设置newCheck.Visible = true
通过这种方式,事件将会触发