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?
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.
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:
' MasterMeny is the name of the Menu control
Dim MyMasterMenu as Web.Ui.Menu = Master.FindControl("MasterMenu")
' And then you can access individual menu items
MyMasterMenu.Items(1).Visible = false
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?
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:
Dim MyMasterMenu As Web.UI.Menu = Master.FindControl("ContentMenu")
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!
Re: Hiding a Menu Item on a MasterPage?
Nah, you got something wrong, it should be Menu, not Page. Post your code please.
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
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
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
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
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.
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;
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.
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