Home arrow GIS Data & Resources arrow Scripts and Code arrow C# Code arrow C# ASP.NET Removing child and parent menu items
C# ASP.NET Removing child and parent menu items PDF Print E-mail

Written by Master Po,

When working with an asp:menu control it is nice to have control over what MenuItems get displayed based on user privileges or the state of the application rather than maintaining multiple menu's.  ASP.NET makes this very simple by hooking into the MenuItemDataBound event.  This event allows you to iterate through all the MenuItems - both parent and child.

Here is a diagram of what I mean by parent child menu items

  • parent
  • parent
  • child
  • child
  • parent


The line: menu.Items.Remove(eventArgs.Item);  will only remove a parent menu item, not children!  It's pretty obvious after you see this line: eventArgs.Item.Parent.ChildItems.Remove(eventArgs.Item); that you can only remove menu items from the parent item. 

Hopefully this can save you some head scratching and time when working with your next asp menu.

protected void TitleMenu_MenuItemDataBound(object sender, EventArgs e)

    {

        MenuEventArgs eventArgs = e as MenuEventArgs;

        Menu menu = sender as Menu;

 

        //user not logged in, remove both

        if (Membership.GetUser() == null && (eventArgs.Item.Text.Equals("Admin") || eventArgs.Item.Text.Equals("Registered User Pages")))

        {

            menu.Items.Remove(eventArgs.Item);

        }

        //user logged in not admin, remove admin

        else if (Membership.GetUser() != null && (eventArgs.Item.Text.Equals("Admin") && !Roles.IsUserInRole(Membership.GetUser().UserName, "Admin")))

        {

            menu.Items.Remove(eventArgs.Item);

        }

        //user logged in as admin remove user pages

        else if (Membership.GetUser() != null && (eventArgs.Item.Text.Equals("Registered User Pages") && Roles.IsUserInRole(Membership.GetUser().UserName, "Admin")))

        {

            menu.Items.Remove(eventArgs.Item);

        }

        //user hasn't created a facility remove well link

        if (Membership.GetUser() != null && Roles.IsUserInRole(Membership.GetUser().UserName, "User") && eventArgs.Item.Text.Equals("Create New Well"))

        {

            ProfileCommon p = new ProfileCommon();

            p = p.GetProfile(Membership.GetUser().UserName);

 

            Guid guid = new Guid(p.UserId);

 

            UICDataContext db = new UICDataContext();

            var facilityQuery = from value in db.UICFACILITYTOCONTACTs

                                where value.ContactGUID == guid

                                select value.FacilityGUID;

 

            if (facilityQuery.Count() < 1)

            {

                eventArgs.Item.Parent.ChildItems.Remove(eventArgs.Item);

            }

        }

    }


Users' Comments  
 

No comment posted

Add your comment

29, Oct. 2008
Last Updated ( 29, Oct. 2008 )
 
< Prev   Next >

AGRC Contacts | UGIC Contacts

feed image feed image

Utah GIS Portal © 2009 AGRC

Optimized for