0

Hi im trying to solve this problem, I'm fetching data from DB and create the menu:

  if (Context.User.IsInRole("Admin") == true)
  {


                mnMenu.Items.Clear();
                List<menuRole> menuParentList = new List<menuRole>();
                List<MenuItem> menuItemList = new List<MenuItem>();
                List<MenuItem> hoverItem = new List<MenuItem>();
                int parentPosition = 0;
                string menuName;

                menuParentList = roles.getMenuItems().ToList();

                for (int i = 0; i < menuParentList.Count; i++)
                {
                    if (menuParentList[i].menuType == "Admin")
                    {
                        if (menuParentList[i].MenuRole1 == "parent")
                        {
                            menuName = menuParentList[i].menuName.ToString();
                            menuItemList[i].Text = menuName;
                            parentPosition = i;
                        }
                        else if (menuParentList[i].MenuRole1 == "child")
                        {
                           hoverItem[i].Text = menuParentList[i].menuName;
                           hoverItem[i].NavigateUrl = menuParentList[i].MenuLink;
                           menuItemList[i].ChildItems.Add(hoverItem[i]);
                        }
                    }
                }
                mnMenu.Items.Add(menuItemList[0]);
                mnMenu.Items.Add(menuItemList[1]);
                mnMenu.Items.Add(menuItemList[2]);

      }

now its giving me this exception at the moment:

System.ArgumentOutOfRangeException was unhandled by user code
  Message=Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
  Source=mscorlib
  ParamName=index
  StackTrace:
       at System.ThrowHelper.ThrowArgumentOutOfRangeException()
       at System.Collections.Generic.List`1.get_Item(Int32 index)
       at PresentationLayer.UserControls.Menu.Page_Load(Object sender, EventArgs e) in C:\Users\Kurt\documents\visual studio 2010\Projects\aaa\PresentationLayer\UserControls\Menu.ascx.cs:line 44
       at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
       at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
       at System.Web.UI.Control.OnLoad(EventArgs e)
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Control.LoadRecursive()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
  InnerException: 
1
  • menuItemList[i].Text = menuName; this adds the name of the parent header. Commented Dec 31, 2011 at 11:57

1 Answer 1

1

Well , this seems to make a few (too many) assumptions:

  mnMenu.Items.Add(menuItemList[2]);

You should check how many items you got.


if (menuParentList[i].MenuRole1 == "parent")
{
  menuName = menuParentList[i].menuName.ToString();
  menuItemList.Add(new MenuItem());  // add this 
  menuItemList[i].Text = menuName;
  parentPosition = i;
}

And the same for HoverItem .


Edit, take 3.

You cannot use i to index the menuList and hoverItem collections, they don't grow every step.

    if (menuParentList[i].MenuRole1 == "parent")
    {
        menuName = menuParentList[i].menuName.ToString();
        var newItem = new MenuItem();
        newItem.Text = menuName;
        menuItemList.Add(newItem);                             
        parentPosition = menuItemList.Count-1;   // flaky
    }
    else if (menuParentList[i].MenuRole1 == "child")
    {
        var newItem = new MenuItem();
        newItem.Text = menuParentList[i].menuName;
        newItem.NavigateUrl = menuParentList[i].MenuLink;
        hoverItem.Add(newItem);                             

        //menuItemList[i].ChildItems.Add(hoverItem[i]);
        menuItemList[parentPosition].ChildItems.Add(newItem);  // I think
    }
Sign up to request clarification or add additional context in comments.

6 Comments

Im getting the error at this line of code : menuItemList[i].Text = menuName;
Right, I spotted it. See Edit.
well it worked fine for the parent but over the hover item still gives the same error :S else if (menuParentList[i].MenuRole1 == "child") { hoverItem.Add(new MenuItem()); hoverItem[i].Text = menuParentList[i].menuName; hoverItem[i].NavigateUrl = menuParentList[i].MenuLink; menuItemList[i].ChildItems.Add(hoverItem[i]); }
Correct, I'll change my answer.
Worked just fine just some minor changes thanks! youre the best!!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.