Results 1 to 15 of 15

Thread: Hiding a Menu Item on a MasterPage?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    261

    Hiding a Menu Item on a MasterPage?

    I have a Master Page called MasterPageUser that has a Menu control on it. Depending on which item in the menu item is selected, a child page is called (which also uses the MasterPageUser)

    What I want to do is hide the menu item that was used to call the specific page, i.e.

    If my menu choices on the master page are:
    "Home"
    "Support"
    "About"

    and I select "About" I want my menu choices to now be:

    "Home"
    "Support" with "About" appearing as not visible.

    Any idea on how to accomplish this? I'm thinking I need code on the child page, perhaps pageload that sets the property of the content menu, but I don't know how to reference it.

    Thoughts?

  2. #2
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: Hiding a Menu Item on a MasterPage?

    You can make a public property on the master page that recieves an int (or whatever) and depending on the value hide/display various values.

    Got to run now but i'll post an example in a few hours if you still need help.

  3. #3
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Hiding a Menu Item on a MasterPage?

    Quote Originally Posted by seidel1
    I have a Master Page called MasterPageUser that has a Menu control on it. Depending on which item in the menu item is selected, a child page is called (which also uses the MasterPageUser)

    What I want to do is hide the menu item that was used to call the specific page, i.e.

    If my menu choices on the master page are:
    "Home"
    "Support"
    "About"

    and I select "About" I want my menu choices to now be:

    "Home"
    "Support" with "About" appearing as not visible.

    Any idea on how to accomplish this? I'm thinking I need code on the child page, perhaps pageload that sets the property of the content menu, but I don't know how to reference it.

    Thoughts?
    You can reference your Master Page menu from the Child Page like:

    VB Code:
    1. ' MasterMeny is the name of the Menu control
    2. Dim MyMasterMenu as Web.Ui.Menu = Master.FindControl("MasterMenu")
    3.  
    4. ' And then you can access individual menu items
    5.  
    6. MyMasterMenu.Items(1).Visible = false
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    261

    Re: Hiding a Menu Item on a MasterPage?

    Thanks for the reply guys. I think you are on the right track, but I still can't get it to go:

    Here's what seems to make the most progress in the IDE, but this is still blue lined and I cant find the FindControl attribute anywhere and I had to add "New"

    'ContentMenu is the name of the Menu control
    Dim MyMasterMenu As New Web.UI.Menu = MasterPageEDCPages.MasterPage("ContentMenu")

    ' And then you can access individual menu items
    MyMasterMenu.Items(1).Visible = False

    Thoughts?

  5. #5
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Hiding a Menu Item on a MasterPage?

    Quote Originally Posted by seidel1
    Thanks for the reply guys. I think you are on the right track, but I still can't get it to go:

    Here's what seems to make the most progress in the IDE, but this is still blue lined and I cant find the FindControl attribute anywhere and I had to add "New"

    'ContentMenu is the name of the Menu control
    Dim MyMasterMenu As New Web.UI.Menu = MasterPageEDCPages.MasterPage("ContentMenu")

    ' And then you can access individual menu items
    MyMasterMenu.Items(1).Visible = False

    Thoughts?
    You need to do a Master.FindControl() to get a reference to the menu, and don't use a new, your not creating a new menu, you just want a reference to the existing one, like:

    VB Code:
    1. Dim MyMasterMenu As Web.UI.Menu = Master.FindControl("ContentMenu")
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    261

    Re: Hiding a Menu Item on a MasterPage?

    Sean -

    Thanks, I'm almost there.

    The IDE made me change Web.UI.Menu to Web.UI.Page I made the change and the error went away.

    Now I get an error on the MyMasterMenu.Items(1).Visible = False

    the error message is "Object reference not set to an instance of an object.", it wants me to use "New" to create a new instance.

    Suggestions? Again thanks for all the help!

  7. #7
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Hiding a Menu Item on a MasterPage?

    Nah, you got something wrong, it should be Menu, not Page. Post your code please.
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    261

    Re: Hiding a Menu Item on a MasterPage?

    Here ya go (Page is still here vs menu):

    Imports System.threading
    Imports System.Globalization
    Imports System.Data
    Imports System.Data.SqlClient

    Partial Class aaa
    Inherits System.Web.UI.Page
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    Master.HeaderText = "Property Page"
    Page.Title = "Property Page"

    'MasterMenu is the name of the Menu control
    Dim MyMasterMenu As Web.UI.Page = Master.FindControl("ContentMenu")

    ' And then you can access individual menu items
    MyMasterMenu.Items(1).Visible = False

    End Sub

  9. #9
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Hiding a Menu Item on a MasterPage?

    yeah, this line:

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

    Definately won't work. Is ContentMenu to ID of your Menu control? Kind of sounds like ContentMenu is the ID of you Page if the above works. Other than that, try replacing:

    Web.UI.Page

    with

    WebControls.Menu
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    261

    Re: Hiding a Menu Item on a MasterPage?

    No difference, it still asks for a New...This has to be easier than we are making it

  11. #11
    Frenzied Member
    Join Date
    Mar 2004
    Location
    Orlando, FL
    Posts
    1,618

    Re: Hiding a Menu Item on a MasterPage?

    When you "New" an object, you are creating another instance of it. You don't want to create another menu, you just want a pointer that points back to the original on the Master Page. Hmmm.....looking at it in the editor, it looks like the MenuItem object doesn't have a Visible property. Try just setting the text to an empty string "" and see what happens.


    Try this and see what happens:

    DirectCast(Master.FindControl("ContentMenu"),Webcontrols.Menu).Items(1).Text = ""

    Comment out:

    'MasterMenu is the name of the Menu control
    Dim MyMasterMenu As Web.UI.Page = Master.FindControl("ContentMenu")

    ' And then you can access individual menu items
    MyMasterMenu.Items(1).Visible = False
    Sean

    Some days when I think about the next 30 years or so of my life I am going to spend writing code, I happily contemplate stepping off a curb in front of a fast moving bus.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    261

    Re: Hiding a Menu Item on a MasterPage?

    No luck, it looks like its not finding the MasterPage or the control on the master page. The error is "Object Reference not set to an instance of the object"

    I agree with your logic, I just can't figure out how to touch the control to make the change.

  13. #13
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092

    Re: Hiding a Menu Item on a MasterPage?

    seidel1,

    I'm still writing my first asp.net 2.0 app and so don't know if this is the best way to do it but here's what i do and it works.... (Sorry it's in C# but should be easy enough to convert).

    Make a property on the master page...
    Code:
        public int HideItem
        {
            set
            {
                //Remove menu item at specified position
                this.MainMenu.Items.RemoveAt(Value);
            }
        }
    Then at top of .aspx page...
    Code:
    <%@ MasterType VirtualPath="~/base.master" %>
    this allows you to access the master page from your derived page.

    Then in code-behind for your aspx page...
    Code:
            //Remove the item at position 1
            this.Master.HideItem = 1;

  14. #14
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: Hiding a Menu Item on a MasterPage?

    I disagree with your logic and approach, Seidel. While it may work at the end, you are implementing a 'hack' to get the work done.

    Don't you think it'd be better if you inherited your own menu control, and added a property/method to it that did this for you? So on any of your pages, you could say

    MyControl.CurrentMenuPageItem = 4

    And MyControl would contain the logic to implement it.

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2005
    Posts
    261

    Re: Hiding a Menu Item on a MasterPage?

    Thanks Mendak and Fish -

    I am new to ASP.Net so knowing the right and wrong way to accomplish something is still being learned. I definetely want to do the efficient thing.

    From what I am hearing in your response, I need to inherit the menu (called ContentMenu) from the masterpage that is associated with the child page?

    Because I am a rookie can you show me step by step:?

    The Masterpage is called EDCMasterpage.Master
    On the EDCMasterpage is a Menu Control called ContentMenu, that has 6 Menu items
    The behaviior I want to implement is that when a menu item is clicked, i.e. menu item 1, I want to display the page associated with Menu Item 1 and hide the menu item 1 choice on the master page

    Thanks in advance

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width