【问题标题】:Add Dynamic CheckBox Handle CheckedChanged Event ASP.NET添加动态复选框句柄 CheckedChanged 事件 ASP.NET
【发布时间】:2013-11-08 06:07:26
【问题描述】:

我想知道为什么事件没有触发以及如何找到触发事件的复选框控件。

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";
            }

【问题讨论】:

  • 你在哪里动态创建复选框?您是否也最晚在 page_load 的回发中使用与以前相同的 ID 重新创建它?
  • object sender 是当您单击它时触发事件的复选框。只需将其投射到CheckBox
  • 我正在 page_load 上创建复选框。

标签: c# asp.net


【解决方案1】:

如果事件没有触发,可能是由于以下两个原因之一:

  1. 在页面生命周期中重新创建控件的时间太晚。尝试在OnInit 期间创建控件。
  2. 验证正在阻止回发。要解决此问题,您可以在所有 CheckBox 控件上将 CausesValidation 设置为 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-21
    • 1970-01-01
    • 1970-01-01
    • 2011-03-16
    • 1970-01-01
    相关资源
    最近更新 更多