jeudi 13 août 2015

Checkbox fires more than one event

I've a dynamic tab where I've checkbox. I put an event onCheckedChanged on them. It works first time, but after postback, my tab is recreated and when I click on an other checkbox, I get more than one event being triggered.

Here is the code to create the tab :

private void initCatalog()
        {
            foreach (Article art in listArticle)
            {
                TableRow ligne = new TableRow();
                ligne.Width = Unit.Percentage(100);

                TableCell celluleITMREF = new TableCell();
                celluleITMREF.Width = Unit.Percentage(10);
                celluleITMREF.Text = art._ITMREF;

                TableCell celluleCBOX = new TableCell();
                celluleCBOX.Width = Unit.Percentage(8);

                CheckBox cbox = new CheckBox();
                cbox.ID = "cbox." + f._FOURNISSEUR + "." + art._ITMREF;
                cbox.Checked = hfArticlesPaniers.Value.Contains(cbox.ID);
                //cbox.Enabled = !(hfArticlesPaniers.Value.Contains(cbox.ID));
                cbox.CheckedChanged += new EventHandler(cbox_CheckedChanged);
                cbox.AutoPostBack = true;
                cbox.CssClass = "c";

                celluleCBOX.Controls.Add(cbox);
                ligne.Cells.Add(celluleITMREF);
                ligne.Cells.Add(celluleCBOX);

                tabArticle.Rows.Add(ligne);
            }
        }

Here is the event for the checkbox :

protected void cbox_CheckedChanged(object sender, EventArgs e)
        {
            CheckBox c = sender as CheckBox;

            Response.Write("<script>alert(\"" + c.ID + "\");</script>");
        }

Here is the page_Load event :

protected void page_Load(object sender, EventArgs e){
       this.initCatalog();
}

Thanks for your help :)




Aucun commentaire:

Enregistrer un commentaire