Scenario: Every page on the website will have a menu bar at the top consisting of a number of buttons. Some pages have the same amount of button that do the same thing (example redirect to a page) but some of the buttons depend on information currently on the page.
Problem: I would like to be able to override the buttons on the control so that I could handle it per page. I was also thinking maybe it would be easier to use events and handle it that way instead. Are either of these approaches possible and if not is there an alternative?
The code I currently have is below
In the aspx page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim myMenu As MenuBar
myMenu = CType(Master.FindControl("MenuBar1"), MenuBar)
myMenu.ShowButtons(MenuBar.Buttons.NewOrganization, MenuBar.Buttons.NewEquipment, MenuBar.Buttons.ChangeEquipmentOwner, MenuBar.Buttons.ChangeEquipmentLocation)
End If
End Sub
The ascx Control
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
RaiseEvent myEvent(sender, e)
End If
End Sub
Public Sub ShowButtons(ByVal ParamArray args() As Buttons)
For i As Integer = 0 To args.GetUpperBound(0)
SetVisible(args(i))
Next
End Sub
Public Enum Buttons
NewOrganization = 1
NewEquipment = 2
ChangeEquipmentOwner = 3
ChangeEquipmentLocation = 4
UnderDevelopment = 5
CheckoutEquipment = 6
EditDocument = 7
CreateEquipmentLoanForm = 8
End Enum
Private Sub SetVisible(ByVal btn As Buttons)
Select Case (btn)
Case 1
btnNewOrganization.Visible = True
Case 2
btnNewEquipment.Visible = True
Case 3
btnChangeOwner.Visible = True
Case 4
btnChangeLocation.Visible = True
Case 5
btnUnderDevelopment.Visible = True
Case 6
btnCheckoutEquipment.Visible = True
Case 7
btnEditDocument.Visible = True
Case 8
btnCreateEquipment.Visible = True
End Select
End Sub
What I would do is to use a content place holder on your master page that will be reserved for things like your menu control. Each page could then provide the menu control.
Using this technique will enable each of your pages to tailor the control to the desired effect without coupling masterpage/page logic. Not only that, but each page wouldn’t be married to the 1 control that you use on the master page. You may at some point want to include another menu control or a completely different menu control without affecting any of the other pages.
BUT
If you absolutely must interact with your master page from your content page and you aren’t receptive of my ideas, you can do this for example using C# (but can easily be adapted to VB.NET in your case):
Masterpage CodeBehind
Be sure to add a method in your masterpage codebehind..something like so:
ASPX
Be sure to add the following directive to a page. You’ll see why this is helpful in a bit, also change my masterpage name to yours:
ASPX CodeBehind
Observe that I didn’t have to type cast the master..this is because of the MasterType directive I included in the aspx.