C# 收集被修改;枚举操作不能执行

C# 收集被修改;枚举操作不能执行,c#,asp.net,C#,Asp.net,我在生产ASP.Net应用程序中不断收到以下警告(不是错误): System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Web.

我在生产ASP.Net应用程序中不断收到以下警告(不是错误):

System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.InvalidOperationException: Collection was modified; enumeration operation may not execute. at System.Web.UI.ControlCollection.ControlCollectionEnumerator.MoveNext() at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter writer) at System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter output) at System.Web.UI.HtmlControls.HtmlForm.RenderControl(HtmlTextWriter writer) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at DMAC.UI.Master.Render(HtmlTextWriter writer) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at System.Web.UI.Page.Render(HtmlTextWriter writer) at Telerik.Web.UI.RadAjaxControl.RenderPageInAjaxMode(HtmlTextWriter writer, Control page) at System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter writer, ICollection children) at System.Web.UI.Page.Render(HtmlTextWriter writer) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) --- End of inner exception stack trace --- at System.Web.UI.Page.HandleError(Exception e) at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) at System.Web.UI.Page.ProcessRequest() at System.Web.UI.Page.ProcessRequest(HttpContext context) at ASP.scan_history_aspx.ProcessRequest(HttpContext context) at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) System.Web.HttpUnhandledException:引发了类型为“System.Web.HttpUnhandledException”的异常。-->System.InvalidOperationException:集合已修改;枚举操作不能执行。 在System.Web.UI.ControlCollection.ControlCollectionEnumerator.MoveNext()中 位于System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter编写器,ICollection子对象) 位于System.Web.UI.HtmlControls.HtmlForm.RenderChildren(HtmlTextWriter) 位于System.Web.UI.HtmlControls.HtmlForm.Render(HtmlTextWriter输出) 位于System.Web.UI.HtmlControl.HtmlForm.RenderControl(HtmlTextWriter) 位于System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter编写器,ICollection子对象) 在DMAC.UI.Master.Render(HtmlTextWriter) 位于System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter编写器,ICollection子对象) 在System.Web.UI.Page.Render(HtmlTextWriter) 在Telerik.Web.UI.RadAjaxControl.RenderPageInAjaxMode(HtmlTextWriter编写器,控制页) 位于System.Web.UI.Control.RenderChildrenInternal(HtmlTextWriter编写器,ICollection子对象) 在System.Web.UI.Page.Render(HtmlTextWriter) 在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint) ---内部异常堆栈跟踪的结束--- 位于System.Web.UI.Page.HandleError(异常e) 在System.Web.UI.Page.ProcessRequestMain(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint) 在System.Web.UI.Page.ProcessRequest(布尔includeStagesBeforeAsyncPoint,布尔includeStagesAfterAsyncPoint) 在System.Web.UI.Page.ProcessRequest()中 在System.Web.UI.Page.ProcessRequest(HttpContext上下文)中 在ASP.scan\u history\u aspx.ProcessRequest(HttpContext上下文)中 在System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()中 在System.Web.HttpApplication.ExecuteStep(IExecutionStep步骤,布尔值&同步完成) 以下是发生错误的渲染方法的代码:

protected override void Render(HtmlTextWriter writer)
    {
        Header header = new Header();
        List<Menu> menus = DMAC.Application.Current().Menus;
        Menu menu = menus.Find(m => m.MenuId == MenuId);

        SiteMapWindow siteWindow = new SiteMapWindow();
        siteWindow.OpenerId = Menu.PATHID;
        this.Page.Form.Controls.Add(siteWindow);

        SupportWindow supportWindow = new SupportWindow();
        supportWindow.OpenerId = Menu.SUPPORTID;
        this.Page.Form.Controls.Add(supportWindow);

        this.Page.Form.Controls.AddAt(0, menu);
        this.Page.Form.Controls.AddAt(0, header);

        Scripts.GoogleAnalytics.RegisterScript(this.Page);
        base.Render(writer);
    }
受保护的覆盖无效渲染(HtmlTextWriter)
{
页眉=新页眉();
列表菜单=DMAC.Application.Current().menus;
Menu Menu=menus.Find(m=>m.MenuId==MenuId);
SiteMapWindow siteWindow=新建SiteMapWindow();
siteWindow.OpenerId=Menu.PATHID;
this.Page.Form.Controls.Add(siteWindow);
SupportWindow SupportWindow=新的SupportWindow();
supportWindow.OpenerId=Menu.SUPPORTID;
this.Page.Form.Controls.Add(supportWindow);
this.Page.Form.Controls.AddAt(0,菜单);
this.Page.Form.Controls.AddAt(0,页眉);
Scripts.GoogleAnalytics.RegisterScript(this.Page);
base.Render(writer);
}

我自己无法重现这个错误。有什么想法吗?

我怀疑问题是由于在
Render

也就是说,这些控件被添加到页面控件集合中,该集合可能会被迭代以呈现子控件。(我想它包括这个代码;在一个子控件中。)

将布局修改(实际上是方法中的所有内容)移动到
CreateChildControls
或适当的位置。(我不确定
CreateChildControls
在这里是否还可以——我认为问题在于页面控件不是子控件。)


快乐的编码。

我并不总是测试我的代码;“但当我这么做的时候,我是在生产中做的。”布兰登,保持干渴,我的朋友。