I was wondering if it is possible to access Master Page Control events from a child page. What I mean is, I have a button on the master page, I need to modify its onclick event on a particular child page, is there a way to do it? I know how to access the control attributes, i.e.: Dim Button as Button = CType(Master.FindControl("Button1"), Button), but I can't figure out how to accss its events.
Page load event of the content page:
Dim mp As MasterPage = CType(Page.Master, MasterPage)
RemoveHandler mp.Button.Click, AddressOf mp.Button_Click
AddHandler mp.Button.Click, AddressOf Button_Click
a separate function
Protected Sub Button_Click(ByVal sender As Object, ByVal e As System.EventArgs)
'what I needed to do.
and some changes in the master page:
changed Private to ovveridable
Overridable Sub Button_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles Button.Click
and added this
Public ReadOnly Property Button() As Button
Hope this will be useful to anyone...
Thanks for posting this, Alexi. It very much put me on the right track. I had a similar (but probably less complicated) situation, and I thought I’d post my findings as well. For me, all I had to do was find the MasterPage control on Page_Load of my Content page, and then add the handler like this:Dim ddlFacility As DropDownList = Me.Master.FindControl("ddlFacility") AddHandler ddlFacility.SelectedIndexChanged, AddressOf MyLocalRoutineThis way I was able to execute events for this one control on both the MasterPage and the ContentPage, and I didn't have to deal with any "WithEvents" or "Overridable" calls.
Glad I could help. just for clarification, in my case I had to get rid of the code in the master page as it would have conflicted with new code I was loading on the content page, hence ovveriadble and removehandler lines.
with addhandler, i understand you just added lines to the existing code in the master page.