I have a Page where I have placed a User Control. The User Control is responsible for dynamically showing a menu based on values in a database. The code for adding the menu looks like this:
1 private void AddMenuContent(Control container, Menu menu, List<Tab> tabs, BasePageAdmin myPage)
2 {
3 Version currentSystemVersion = SessionUtils.SessionContainer.CurrentDepartment.Version;
4 List currentUserRoles = SessionUtils.SessionContainer.User.Roles;
5
6 foreach (Tab t in tabs)
7 {
8 // Only add tab to this menu if it is supposed
9 // to be shown here, is in the current version and this user has access to it.
10 if (t.Menu != null && menu.Id == t.Menu.Id && !t.IsExcluded(currentSystemVersion) && t.IsAuthorized(currentUserRoles))
11 {
12
13 // Mark as isActiveTab if this Tab is the currently displayed tab.
14 bool isActiveTab = false;
15 if (t.Id == myPage.ActiveTab.Id)
16 {
17 isActiveTab = true;
18 }
19
20 // Decide what kind of menu to display
21 switch (_menuType)
22 {
23 case MenuTypes.Image:
24 this.AddImageElement(t, container, isActiveTab);
25 break;
26 case MenuTypes.Text:
27 this.AddTextElement(t, container, isActiveTab);
28 break;
29 default:
30 this.AddTextElement(t, container, isActiveTab);
31 break;
32 }
33
34 // Decide if we should display the menu as vertical or horizontal
35 if (_orientation == Orientations.Vertical)
36 {
37 MenuContent.Controls.Add(new LiteralControl("<br />"));
38 }
39
40 }
41 } // End foreach
42 }
43
This method is called from Page_Load of the User Control. Some times, this method throws this exception:
System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.InvalidOperationException: Collection was modified; enumeration operation may not execute.
at System.ThrowHelper.ThrowInvalidOperationException(ExceptionResource resource)
at System.Collections.Generic.List`1.Enumerator.MoveNext()
Can anyone explain why this exception is being thrown? And why is it not thrown on every request?