Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Really simple question here, I'm amazed I haven't discovered this before but my background is mainly WinForms and middle tier stuff.

I have an ASP.Net masterpage

I have an ASP.Net Web Form that is based on that Masterpage.

I have a button on the masterpage.
the click event of that buttons calls a Sub that is defined as follows
in the Masterpage

Protected Overridable Sub DoStuff()

End Sub


I had assumed that much like inheritance in WinForms that I could override this Sub in my Web Form and therefore all my inherited Web Form
react to the click of the button on the MasterPage.

I appear to be wrong. I never looked closely but Web Forms don't seem to inherit from Masterpages in the way I would have thought.

Can anyone shed some light on this?

-Rd
Posted

This seems to be a problem that lots of people stumble upon.
Here's the VB solution I've gone with.
Thanks to the following link for the inspiration:
See Here

In my master page I declare a delegate outside the Class

VB
Public Delegate Sub SaveButtonClickHandler(ByVal sender As Object, _
    ByVal e As System.Web.UI.ImageClickEventArgs)


Inside the class I declare an event based on the delegate

VB
Public Event SaveButtonClick As SaveButtonClickHandler


In the click event of my button on the master page I raise the above event

VB
RaiseEvent SaveButtonClick(Me, e)


In my content page (the aspx page) I add the following

<%@ MasterType virtualPath="~/WorkflowFrameCommon.master"%>

In my Content page (the code behond page), I add a button Handler as normal

VB
Protected Sub Save_Click(ByVal sender As Object, _
     ByVal e As System.Web.UI.ImageClickEventArgs)

End Sub


And in the page_load of my code behind page I hook up my event to the
event in the Masterpage

VB
AddHandler Master.SaveButtonClick, AddressOf Save_Click



Simple? No.

But I get to go home, rather than spending all night re-architecting a system, so I'll call this a lucky escape.

Thanks Simon for pointing me in the right direction.

-Rd
 
Share this answer
 
Comments
Simon_Whale 16-Sep-10 10:44am    
why not create a tip / trick for your solution?
Richard A. Dalton 16-Sep-10 11:07am    
Yeah, I will, what I've brain dumped above isn't particularly clear.
I'll type it up properly with a sample project.
But first, I have a project manager drumming fingers on table wondering why this code isn't in test yet. :-)
Try telling them that Microsoft made it difficult to implement inheritence with MasterPages.
Have a look at this I know its in C# but conversion would be easy I think

Master page Tricks and Tips
 
Share this answer
 
Comments
Richard A. Dalton 16-Sep-10 9:40am    
Reason for my vote of 5
Automatic vote of 5 for accepting answer.
Richard A. Dalton 16-Sep-10 9:42am    
Dammit. How does Microsoft keep doing this to me? I trundle along quite happily solving all sorts of complex issues. Then when it comes to something simple like reacting to the click event of a button on a MasterPage, I find I'm stuck.

Oh well.

Thanks for confirming my fears! :)
Simon_Whale 16-Sep-10 9:44am    
No hassle I had this issue to a while back and kept it in my bookmarks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900