简体   繁体   English

复选框会触发多个事件

[英]Checkbox fires more than one event

I have a dynamic tab where I have checkboxes. 我有一个动态选项卡,其中有复选框。 I put an event onCheckedChanged on them. 我在他们身上放了一个事件onCheckedChanged 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 : 这是page_Load事件:

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 的每次迭代artlistArticle将导致事件处理程序由线被添加到事件

  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. 另外,如果未破坏页面并在其间再次加载,则每次运行initCatalog也会导致此情况。

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? 但是在回发之后,可以再次执行initCatalog() ,从而再次添加事件吗?

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);
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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