我想知道为什么事件没有触发以及如何找到哪个复选框控件触发了事件。
chkList1 = new CheckBox();
chkList1.Text = row["subj_nme"].ToString();
chkList1.ID = row["subjid"].ToString();
chkList1.Checked = true;
chkList1.Font.Name = "Verdana";
chkList1.Font.Size = 12;
chkList1.AutoPostBack = true;
chkList1.CheckedChanged += new EventHandler(CheckBox_CheckedChanged);
Panel1.Controls.Add(chkList1);
protected void CheckBox_CheckedChanged(object sender, EventArgs e)
{
Label1.Text = "Called";
}
如果事件没有触发,可能是由于以下两个原因之一:
- 控件在页面生命周期中重新创建得太晚。尝试在期间创建控件
OnInit
.
- 验证正在阻止回发。要解决此问题,您可以设置
CausesValidation
对所有 CheckBox 控件设置为 false。
您可以使用以下命令找出哪个控件触发了该事件sender
争论。
protected void CheckBox_CheckChanged(object sender, EventArgs e)
{
//write the client id of the control that triggered the event
Response.Write(((CheckBox)sender).ClientID);
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)