0

I try to make a simple application in which I have a dropdown list with tems - numbers from 1 to 4.

Depending on the number the user choose - I create dynamically this number of checkboxes with binded checkedchanged event. So when the user checks some of the checkboxes so checkedchanged event is raised and I store the text of the checked checkbox in session and then when I click a button I want to see the text only from the checked checkboxes.

But it seems that the checkedchanged event handler is never triggered.

Thank you in advance

public partial class proba : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        dd1.Items.Add("1");
        dd1.Items.Add("2");
        dd1.Items.Add("3");
        dd1.Items.Add("4");





    }

    protected void dd1_SelectedIndexChanged1(object sender, EventArgs e)
    {
        int numTourists = Convert.ToInt32(dd1.SelectedItem.Text);

        for (int i = 0; i < numTourists; i++)
        {
            CheckBox chk = new CheckBox();
            chk.ID = "chk" + i;
            chk.Text = "box" + i;

            chk.CheckedChanged += new EventHandler(checkChanged);
            Page.FindControl("form1").Controls.Add(chk);


        }
    }

    protected void checkChanged(object sender, EventArgs e)
    {
        CheckBox chk = (CheckBox)sender;

        lblpr.Text += chk.Text;

        Session["chk"] = chk.Text;


    }

    protected void btnpr_Click(object sender, EventArgs e)

    {
        lblpr.Text = "length" + ((String [] )Session["chk"]).Length;
        for (int k = 0; k < ((String[] )Session["chk"]).Length; k++)
        {
            lblpr.Text += ((String [])Session["chk"])[k];


        }
    }




}
Tania Marinova
  • 1,788
  • 8
  • 39
  • 67

2 Answers2

2

You need to set AutoPostBack property of checkbox as true in order to post back when check changed

chk.AutoPostBack = true;

And read this also

adding an event handler to a dynamically created checkbox (aspx, c#)

change page load, you don't need to add items again and again in each page post back

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
     {
      //Load dd1
     }
}

But you need to add dynamic controls on each page post back, better do it on OnInit as above answer in the link suggested

Community
  • 1
  • 1
Damith
  • 62,401
  • 13
  • 102
  • 153
  • can you give me an example of how to recteate the control on this postback event, so from what I understood I should recreate the control in eveventhandler function – Tania Marinova May 18 '13 at 11:24
2

since you are creating checkbox dynamically, it get lost when there is the post back. So you need to add those again before pageload event so that during page load event they are visible to .net and hence .net can fire their corresponding event.

B4 you dive into dynamic control it is good idea to get the basic which can be found at https://web.archive.org/web/20210330142645/http://www.4guysfromrolla.com/articles/092904-1.aspx

Ratna
  • 2,289
  • 3
  • 26
  • 50