添加动态复选框处理的CheckedChanged事件ASP.NET [英] Add Dynamic CheckBox Handle CheckedChanged Event ASP.NET

查看:287
本文介绍了添加动态复选框处理的CheckedChanged事件ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道为什么该事件不触发&安培;如何找到哪些CheckBox控件触发事件。

  chkList1 =新的复选框();
                            chkList1.Text =行[subj_nme]的ToString()。
                            chkList1.ID =行[subjid]的ToString()。
                            chkList1.Checked = TRUE;
                            chkList1.Font.Name =宋体;
                            chkList1.Font.Size = 12;
                            chkList1.AutoPostBack = TRUE;
                            chkList1.CheckedChanged + =新的EventHandler(CheckBox_CheckedChanged);
                            Panel1.Controls.Add(chkList1);保护无效CheckBox_CheckedChanged(对象发件人,EventArgs的发送)
            {
                Label1.Text =称为;
            }


解决方案

如果该事件不烧,很可能为两个原因:


  1. 的控件在重新创建页面生命周期为时已晚。尝试在的OnInit 创建控件。

  2. 验证是preventing回发。要解决这一点,你可以设置的CausesValidation 为false的所有CheckBox控件的。

您可以找出哪些控件触发使用发送事件参数。

 保护无效CheckBox_CheckChanged(对象发件人,EventArgs的发送)
{
    //写触发事件控制的客户端ID
    的Response.Write(((复选框)发送方).ClientID);
}

I would like to know why the event is not firing & how to find which checkbox control fired the event.

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

解决方案

If the events aren't firing, it's likely for one of two reasons:

  1. The controls are recreated too late in the page lifecycle. Try creating the controls during OnInit.
  2. Validation is preventing the postback. To work around this you can set CausesValidation to false on all of the CheckBox controls.

You can find out which control triggered the event using the sender argument.

protected void CheckBox_CheckChanged(object sender, EventArgs e)
{
    //write the client id of the control that triggered the event
    Response.Write(((CheckBox)sender).ClientID);
}

这篇关于添加动态复选框处理的CheckedChanged事件ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆