0

In my function createCheckboxes() I dynamically create checkboxes - all of them with assigned checkedChanged event.

In the event handler i test if the checkbox is checked and if so I sore the value attribute in a session.

But for example if a client decides to check one checkbox and alue is added to the session, but then he suddenly decides that he want to uncjheck that checbox - so I should delete this value from the arraylist and then from the session.

The big problem is that it seems that when unchecking the Checkedchanged event handler is never executed and this code is never executed

else if (!chk.Checked)
        {
             lblProba.Text += "You wll be delited";
            for (int i = 0; i < element.Count; i++)
            {

                if (element[i].ToString().Equals(chk.InputAttributes["value"]) == true)

                    element.Remove(element[i]);
            }

        }

My full code


        protected void checkChanged(object sender, EventArgs e)
{
        CheckBox chk = (CheckBox)sender;
        if (chk.Checked)
        {
            element.Add(chk.InputAttributes["value"]);

        }

        else if (!chk.Checked)
        {
             lblProba.Text += "You wll be delited";
            for (int i = 0; i < element.Count; i++)
            {

                if (element[i].ToString().Equals(chk.InputAttributes["value"]) == true)

                    element.Remove(element[i]);
            }

        }

            for (int t = 0; t < element.Count; t++)
        { 
            Session["chk"]+= element[t].ToString(); 

        }


}
        protected void createCheckboxes()
        {
            chkddlchange = true;
            int numTourists = 2;
            for (int i = 0; i < numTourists; i++)
            {

                Label myLabel = new Label();
                myLabel.ID = "lblAccomodation" + (i + 1).ToString();
                myLabel.Text = "Настаняване Турист" + (i + 1).ToString();
                Page.FindControl("form1").Controls.Add(myLabel);
                DropDownList myDropDownList = new DropDownList();
                myDropDownList.ID = "ddlTourist" + i.ToString();
                Page.FindControl("form1").Controls.Add(myDropDownList);
                Page.FindControl("form1").Controls.Add(new LiteralControl("<br />"));

                string connectionString = "Server=localhost\\SQLEXPRESS;Database=excursion;Trusted_Connection=true";
                string query =
          "SELECT Extra_Charge_ID, Excursion_ID, Amout, Extra_Charge_Description FROM EXTRA_CHARGES WHERE Excursion_ID=" + mynewstring;
                SqlConnection conn = new SqlConnection(connectionString);
                SqlCommand cmd = new SqlCommand(query, conn);

                try
                {

                    conn.Open();
                    SqlDataReader rd= cmd.ExecuteReader();
                    int s = 0;


                    while (rd.Read())
                    {   
                        CheckBox mycheckbox = new CheckBox();
                        mycheckbox.ID = "chkblextracharge" + i.ToString() + s.ToString();
                       mycheckbox.Text = rd["Extra_Charge_Description"].ToString();
                       mycheckbox.InputAttributes.Add("value", rd["Extra_Charge_ID"].ToString());

                       mycheckbox.AutoPostBack = true;
                       mycheckbox.EnableViewState =true ;
                       mycheckbox.CheckedChanged += new EventHandler(checkChanged);
                       Page.FindControl("form1").Controls.Add(mycheckbox);


                        s++;

                    }

                       //myche.Add(mycheckbox.Items[s].Text);


                }//End of try

                catch (Exception ex)
                { }


            }//end of for


        }




    }
}
1
  • You should create the checkboxes only if there was no PostBack otherwise you're probably losing the ViewState. Commented Jun 20, 2013 at 7:14

1 Answer 1

1

Have you mentioned Autopostback=true while creating your checkbox? If not than please do it and see if it works or not.

Sign up to request clarification or add additional context in comments.

Comments

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.