简体   繁体   中英

Checkbox fires more than one event

I have a dynamic tab where I have checkboxes. I put an event onCheckedChanged on them. It works first time, but after postback, my tab is recreated and when I click on another 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 :)

Each iteration of art in listArticle will cause the event handler to be added to the event by the line

  cbox.CheckedChanged += new EventHandler(cbox_CheckedChanged);

executing each time. (ie you're adding the handler multiple times)

In addition, each running of initCatalog will also cause this if the page is not destroyed and loaded again in between.

Move the above line to a place where it will only be executed once after the page loads.

EDIT

on re-reading your code, adding multiple times within the loop is probably what you intended as there are multiple checkboxes. But after the postback, the initCatalog() may be executed again, thereby adding the event once more?

I found the mistake. I've a method who order the list after she was create ... I deleted the method and it worked.

I don't know why it made mistakes ...

private void orderCatalog()
        {
            var tab = from TableRow tr in tabArticle.Rows
                      orderby tr.Cells[1].Text
                      select tr;

            List<TableRow> l = new List<TableRow>();

            foreach (TableRow tr in tab)
            {
                l.Add(tr);
            }

            tabArticle.Rows.Clear();

            foreach (TableRow tr in l)
            {
                tabArticle.Rows.Add(tr);
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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