Reputation: 471
I keep getting the following warning (not error) in a production ASP.Net application:
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)
Here is the code for the Render method where the error is occurring:
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);
}
I cannot recreate the error myself. Any ideas?
Upvotes: 1
Views: 2040
Reputation:
I would suspect the problem arises from adding (non-child) controls in Render
That is, the controls are added to the Page Control Collection which is likely being iterated over in order to Render children. (And I am presuming that includes this code; in a child control.)
Move the layout modifications (really, everything in the method) into CreateChildControls
or what may be appropriate. (I'm not sure CreateChildControls
is actually okay here -- the issue, I believe, is the Page Controls aren't children.)
Happy coding.
Upvotes: 0