在我的函数createCheckboxes
()中,我动态创建了复选框 - 所有复选框都分配了checkedChanged
个事件。
在事件处理程序中,我测试是否选中了复选框,如果是,我在会话中输入了value属性。
但是,例如,如果客户决定选中一个复选框并将alue添加到会话中,但随后他突然决定要取消检查该checbox - 所以我应该从arraylist中删除此值,然后从会话中删除。
最大的问题是,当取消选中Checkedchanged事件处理程序时,似乎从未执行过,而且此代码永远不会被执行
else if (!chk.Checked)
{
lblProba.Text += "You wll be delited";
for (int i = 0; i < element.Count; i++)
{
if (element[i].ToString().Equals(chk.InputAttributes["value"]) == true)
element.Remove(element[i]);
}
}
我的完整代码
protected void checkChanged(object sender, EventArgs e)
{
CheckBox chk = (CheckBox)sender;
if (chk.Checked)
{
element.Add(chk.InputAttributes["value"]);
}
else if (!chk.Checked)
{
lblProba.Text += "You wll be delited";
for (int i = 0; i < element.Count; i++)
{
if (element[i].ToString().Equals(chk.InputAttributes["value"]) == true)
element.Remove(element[i]);
}
}
for (int t = 0; t < element.Count; t++)
{
Session["chk"]+= element[t].ToString();
}
}
protected void createCheckboxes()
{
chkddlchange = true;
int numTourists = 2;
for (int i = 0; i < numTourists; i++)
{
Label myLabel = new Label();
myLabel.ID = "lblAccomodation" + (i + 1).ToString();
myLabel.Text = "Настаняване Турист" + (i + 1).ToString();
Page.FindControl("form1").Controls.Add(myLabel);
DropDownList myDropDownList = new DropDownList();
myDropDownList.ID = "ddlTourist" + i.ToString();
Page.FindControl("form1").Controls.Add(myDropDownList);
Page.FindControl("form1").Controls.Add(new LiteralControl("<br />"));
string connectionString = "Server=localhost\\SQLEXPRESS;Database=excursion;Trusted_Connection=true";
string query =
"SELECT Extra_Charge_ID, Excursion_ID, Amout, Extra_Charge_Description FROM EXTRA_CHARGES WHERE Excursion_ID=" + mynewstring;
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(query, conn);
try
{
conn.Open();
SqlDataReader rd= cmd.ExecuteReader();
int s = 0;
while (rd.Read())
{
CheckBox mycheckbox = new CheckBox();
mycheckbox.ID = "chkblextracharge" + i.ToString() + s.ToString();
mycheckbox.Text = rd["Extra_Charge_Description"].ToString();
mycheckbox.InputAttributes.Add("value", rd["Extra_Charge_ID"].ToString());
mycheckbox.AutoPostBack = true;
mycheckbox.EnableViewState =true ;
mycheckbox.CheckedChanged += new EventHandler(checkChanged);
Page.FindControl("form1").Controls.Add(mycheckbox);
s++;
}
//myche.Add(mycheckbox.Items[s].Text);
}//End of try
catch (Exception ex)
{ }
}//end of for
}
}
}
答案 0 :(得分:1)
创建复选框时是否提到了Autopostback=true
?如果不是,请这样做,看看它是否有效。