locked
Making Menu Item visible or not visible in master page RRS feed

  • Question

  • User-1340344713 posted

    I have five menu items that i have created as a user control and placed on the master page. Now i want one of the menu items to be visible only for particular user role and not visible for others. Here is what I did. Its not working though. Please suggest how should i go about it. Thanks!!!<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>

    Dim DST As New DataSet<o:p></o:p>

    DST = dataObject.RunSPReturnDataSet("uspUserProfile '" & Session("UserID").ToString & "'", "mytbl")<o:p></o:p>

    'Dim US As New DataSet <o:p></o:p>

    'US = dataObject.RunSPReturnDataSet("uspUserProfile'" & Session("UserName").ToString & "'", "mytbl") <o:p></o:p>

    If DST.Tables(0).Rows.Count > 0 Then<o:p></o:p>

    Dim MyMasterMenu As Web.UI.Page = Master.FindControl("NavigationMenu1")<o:p></o:p>

    If Session("UserID").ToString = DST.Tables(0).Rows(0).Item("UserName").ToString Then<o:p></o:p>

    ' And then you can access individual menu items<o:p></o:p>

    MyMasterMenu.Items(1).Visible = True<o:p></o:p>

    Else<o:p></o:p>

    MyMasterMenu.Items(1).Visible = False<o:p></o:p>

    End If<o:p></o:p>

    End If<o:p></o:p>

     

    Tuesday, January 25, 2011 8:44 AM

Answers

  • User1224194097 posted

    yes, you have to add new menu item with same text and value that you used before removing the item

    'To hide the menu item
            masterMenu.Items.RemoveAt(1)

            'To show the menu item
            Dim newMenuItem As New MenuItem() With { _
                                                    .Text = "End Of Year Reports", _
                                                    .Value = "Rev Report", _
                                                    .NavigateUrl = "~/EndOfYearReports.aspx" _
                                                    }
            masterMenu.Items.AddAt(1, newMenuItem)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 25, 2011 10:20 AM

All replies

  • User1224194097 posted

    Dim MyMasterMenu As Web.UI.Page = Master.FindControl("NavigationMenu1")

    You should cast the control as menu, not page

    Dim MyMasterMenu As Menu = DirectCast(Master.FindControl("NavigationMenu1"),Menu)

    Tuesday, January 25, 2011 9:10 AM
  • User-1340344713 posted

    Thanks for your quick response Santosh,

    I tried this but then I get this error..???

    Error 1 'Visible' is not a member of 'System.Web.UI.WebControls.MenuItem'

    at

    MyMasterMenu.Items(1).Visible = True

    What should I do for making visible false and true??

    Tuesday, January 25, 2011 9:20 AM
  • User1224194097 posted

    Yes, Menu Item does not have Visible property to hide/show the item.

    You can either disable/enable or remove/add to achieve similar functionality as Visible property. Check this one out

    'To hide the menu item
            masterMenu.Items.RemoveAt(1)

            'To show the menu item
            Dim newMenuItem As New MenuItem() With {.Text = "new one", .Value = "new value"}
            masterMenu.Items.AddAt(1, newMenuItem)

    Tuesday, January 25, 2011 9:48 AM
  • User-1340344713 posted

    That means I need to specify new text and value for new menu item. What if I want to display same menu item that I removed. Should i need to specify the text and value of the existing menu item here again then?? Sorry for too many questions...:-( 

    Tuesday, January 25, 2011 9:54 AM
  • User-1340344713 posted

    This is how i am making menu items

     <asp:MenuItem NavigateUrl="~/EndOfYearReports.aspx" Text="End Of Year Reports"
                Value="Rev Report" ></asp:MenuItem>

    Tuesday, January 25, 2011 9:57 AM
  • User1224194097 posted

    yes, you have to add new menu item with same text and value that you used before removing the item

    'To hide the menu item
            masterMenu.Items.RemoveAt(1)

            'To show the menu item
            Dim newMenuItem As New MenuItem() With { _
                                                    .Text = "End Of Year Reports", _
                                                    .Value = "Rev Report", _
                                                    .NavigateUrl = "~/EndOfYearReports.aspx" _
                                                    }
            masterMenu.Items.AddAt(1, newMenuItem)

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, January 25, 2011 10:20 AM