Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 添加动态复选框句柄CheckedChanged事件ASP.NET_C#_Asp.net - Fatal编程技术网

C# 添加动态复选框句柄CheckedChanged事件ASP.NET

C# 添加动态复选框句柄CheckedChanged事件ASP.NET,c#,asp.net,C#,Asp.net,我想知道为什么事件没有触发&如何找到哪个复选框控件触发了事件 chkList1 = new CheckBox(); chkList1.Text = row["subj_nme"].ToString(); chkList1.ID = row["subjid"].ToString(); chkList1.Checked = true;

我想知道为什么事件没有触发&如何找到哪个复选框控件触发了事件

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
    设置为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);
    }
    

    在哪里动态创建复选框?您是否也最迟在page_load的回发中使用与以前相同的ID重新创建它?
    对象发送者是您单击它时触发事件的复选框。只需将其转换为
    复选框
    。我正在加载页面上创建复选框。