集合被修改枚举操作可能无法执行。 [英] collection was modified enumeration operation may not execute.

查看:64
本文介绍了集合被修改枚举操作可能无法执行。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在页面中使用asp Table。



Hi,

I am using asp Table in my page.

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                TableRow tr = new TableRow();
            }
            else
            {
                if (Session["tblSystem"] != null)
                {
                    Table tb = (Table)Session["tblSystem"];
                    int count = tb.Rows.Count;
                    foreach (TableRow tr in tb.Rows)
                    {
                        tblSystem.Rows.Add(tr);
                    }
                }
            }
        }
        protected void CreateTextBox(object sender, ImageClickEventArgs e)
        {
            TextBox MyTextBox = new TextBox();
            //Assigning the textbox ID name 
            ImageButton img = sender as ImageButton;
            TableRow row = new TableRow();

            string button = img.AlternateText;
            switch (button)
            {
                case "System":
                    TableCell cell = new TableCell();
                    TableCell cell1 = new TableCell();
                    ImageButton minus = new ImageButton();

                    cell.ID = "tblCell" + ++system;
                    cell1.ID = "tblCell1" + system;
                    row.ID = "tblRow" + system;

                    minus.ID = "imgSystem" + system;
                    minus.ImageUrl = "~/Images/minus.png";

                    MyTextBox.ID = "txtSystem" + system;
                    MyTextBox.Width = 145;
                    MyTextBox.Height = 17;
                    MyTextBox.TextMode = TextBoxMode.SingleLine;

                    cell.Controls.Add(MyTextBox);
                    cell1.Controls.Add(minus);
                    //cell.Text = "Iteration : " + system.ToString();
                    //cell1.Text = "Iteration : " + system.ToString();
                    row.Cells.Add(cell);
                    row.Cells.Add(cell1);
                    tblSystem.Rows.Add(row);
                    Session["tblSystem"] = tblSystem;
                    minus.Click += new System.Web.UI.ImageClickEventHandler(deleteRow);
                    //pnlSystem.Controls.Add(MyTextBox);
                    break;
            }
        }
        protected void deleteRow(object sender, System.Web.UI.ImageClickEventArgs e)
        {
            TableRow row = new TableRow();
            ImageButton img = sender as ImageButton;
            string val = img.ID;
            string id = val.Substring(val.Length - 1, 1);
            if (val.Contains("System"))
            {
                TableRow rw = (TableRow)row.FindControl("tblRow" + id);
                rw.Visible = false;
            }
            Session["tblSystem"] = tblSystem;
        }







I am getting error in foreach in load event. Please help.
Thanks in advance

推荐答案

在foreach中使用集合时出现此错误,并且在循环中集合被操作通过添加或删除项目。一种可能的替代方法是使用for循环。祝你好运。
You get this error when using the collection in foreach and with in the loop the collection is manipulated by adding or removing the items. One possible alternative is to use for loop instead. Good luck.


这篇关于集合被修改枚举操作可能无法执行。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆