Link to home
Start Free TrialLog in
Avatar of vicomin
vicominFlag for United States of America

asked on

hide asp.net menu item

I'm trying to hide an asp.net menu item and i can do it but when i click a logout button it causes and error.

I have one option that does it perfect but throws an error when I click the log out button.

The other option shows the child item and disables its child items. I don't want the child item visible at all.




//usercontrol

<asp:Menu ID="Menu1" runat="server" BackColor="#E3EAEB" 
    DynamicHorizontalOffset="2" Font-Names="Verdana" Font-Size="0.8em" 
    ForeColor="#666666" Orientation="Horizontal" StaticSubMenuIndent="10px">
    <StaticSelectedStyle BackColor="#1C5E55" />
    <StaticMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
    <DynamicHoverStyle BackColor="#666666" ForeColor="White" />
    <DynamicMenuStyle BackColor="#E3EAEB" CssClass="adjustedZIndex" />
    <DynamicSelectedStyle BackColor="#1C5E55" />
    <DynamicMenuItemStyle HorizontalPadding="5px" VerticalPadding="2px" />
    <StaticHoverStyle BackColor="#666666" ForeColor="White" />
    <Items>
        <asp:MenuItem Text="Account" Value="Account">
            <asp:MenuItem NavigateUrl="~/profile.aspx" Text="Profile" Value="Profile"></asp:MenuItem>
            <asp:MenuItem NavigateUrl="~/password.aspx" Text="Change Password" Value="Change Password"></asp:MenuItem>
            <asp:MenuItem Text="Manage Accts" Value="ManageAccts">
                <asp:MenuItem Text="Select Acct" Value="SelectAcct" NavigateUrl="~/accounts.aspx"></asp:MenuItem>
                <asp:MenuItem Text="Acct Status" Value="AcctStatus" NavigateUrl="~/accountstatus.aspx"></asp:MenuItem>
            </asp:MenuItem>
        </asp:MenuItem>
    </Items>
</asp:Menu>



<asp:LoginStatus ID="LoginStatus1" runat="server" 
onloggingout="LoginStatus2_LoggingOut" Font-Size="Smaller" Visible="true" />



protected void Page_Load(object sender, EventArgs e) //, MenuEventArgs ee
    {
        System.Web.Profile.ProfileBase p = HttpContext.Current.Profile;
        MembershipUser currentMember = Membership.GetUser(p.UserName.ToString());

        if (Request.IsAuthenticated)
        {
            if (Roles.IsUserInRole(currentMember.UserName, "Customer"))
            {
                
                Menu1.Items[0].ChildItems.RemoveAt(2); //hide the Account/Manage Accts menu item


                ////disable childmenu items
                //foreach (MenuItem item in Menu1.Items)
                //{
                //    foreach (MenuItem childItem in item.ChildItems)
                //    {
                //        if (childItem.Text == "Manage Accts") childItem.Enabled = false; //*** want to hide this option
                //    }
                //}


//other stuff here as well

    }



protected void LoginStatus2_LoggingOut(object sender, LoginCancelEventArgs e)
    {
        System.Web.Profile.ProfileBase p = HttpContext.Current.Profile;
        MembershipUser currentMember = Membership.GetUser(p.UserName.ToString());

        if (Roles.IsUserInRole(currentMember.UserName, "Customer"))
        {
            Profile.SelectedCustomerId = "";
            Profile.SelectedCompany = "";
            Profile.Save();
        }
        //destroy session
        Session.RemoveAll();
        Session.Abandon();

        //destroy page history - stop user from clicking back button and reposting same data
        DestroyPageHistory();
    }


    protected void DestroyPageHistory()
    {
        //destroy page history - stop user from clicking back button and reposting same data
        Response.Expires = 0;
        Response.ExpiresAbsolute = DateTime.Now;
        Response.CacheControl = "no-cache";
    }

Open in new window

Avatar of Andre412
Andre412
Flag of United Kingdom of Great Britain and Northern Ireland image

there is not visible property on a menuitem, you will have to remove the item from the menu and add later if needed
Avatar of vicomin

ASKER

how do i got about doing that? if i use the following it works:

Menu1.Items[0].ChildItems.RemoveAt(2);

however, when i click the logout button it throws an error - out of index which it should but I dont need that portion of the code to execute.
Hi

for me to help you further I need to see the code for both the front and the back
ASKER CERTIFIED SOLUTION
Avatar of vicomin
vicomin
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vicomin

ASKER

i figure it out