简体   繁体   English

动态创建的复选框checkedchanged 事件问题

[英]Dynamically created checkbox checkedchanged event issue

My issue is that i need to create checkboxes dynamically on a textchanged event of a textbox which are all checked and keeping their checked state count in an int variable assiging it to a label;我的问题是我需要在文本框的 textchanged 事件上动态创建复选框,这些复选框都被选中并将其选中状态计数保存在一个 int 变量中,并将其分配给标签; till here all is successfully done but the problem is now if i uncheck any of the checkboxes i want their count to get decreased by one but the checkchanged event is not firing and by unchecking any of the checkboxes all are gone... here is my code:到这里一切都已成功完成,但问题是如果我取消选中任何复选框,我希望它们的计数减少一个,但 checkchanged 事件不会触发,并且通过取消选中任何复选框都消失了......这是我的代码:

        if (DDLType.SelectedItem.Text == "Sick Leave")
            {


                DateTime f = DateTime.Parse(txtFrom.Text);
                DateTime t = DateTime.Parse(txtTo.Text);
                double daydiff = (t - f).TotalDays;

                double p = daydiff;
                for (int i = 1; i <= daydiff; i++)
                {
                    string a = f.ToString("ddd");
                    chklist = new CheckBox();
                    chklist.AutoPostBack = true;
                    chklist.CheckedChanged += new EventHandler(CheckChanged);

                    chklist.ID = "chk" + i;
                    chklist.Text = a;
                    chklist.Font.Name = "Trebuchet MS";
                    chklist.Font.Size = 9;
                    chklist.Checked = true;
                    checkcount++;
                    pnlCheck.Controls.Add(chklist);

                    if (a == "Thu" || a == "Fri")
                    {
                        p--;
                        chklist.Checked = false;
                        checkcount--;

                    }
                    f = f.AddDays(1);
                }
                daydiff = p;
                lblCheck.Text = checkcount.ToString();

} }

      protected void CheckChanged(object sender, EventArgs e)
       {

        if (!chklist.Checked)
        {
            checkcount--;
            lblCheck.Text = checkcount.ToString();
        }
    }

I don't know what is going wrong...我不知道出了什么问题...

Any help in this regard will highly appreciated Thanks in advance在这方面的任何帮助将不胜感激 提前致谢

You need to make changes to your event handler and check cliked checkbox only.您需要对事件处理程序进行更改并仅选中单击的复选框。

protected void CheckChanged(object sender, EventArgs e)
{
        chk = (CheckBox)sender
        if (!chk.Checked)
        {
            checkcount--;
            lblCheck.Text = checkcount.ToString();
        }
        else
        {
            checkcount++;
            lblCheck.Text = checkcount.ToString();
        }
}
  1. This is because when you send the postback after unchecking a checkbox the remaining checkboxes needs to add dynamically, like you do after the text change event这是因为当您在取消选中复选框后发送回发时,剩余的复选框需要动态添加,就像您在文本更改事件之后所做的那样

  2. add this to your CheckChanged event: chklist = (CheckBox)sender before the if clause将此添加到您的 CheckChanged 事件: chklist = (CheckBox)sender 在if子句之前

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM