1

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; 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:

        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

2 Answers 2

1

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();
        }
}
Sign up to request clarification or add additional context in comments.

Comments

0
  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

1 Comment

thanks for the reply, i got your point but i don't know where to recreate the checkboxes after each postback, could you please put more light on it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.