Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8423137
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T03:38:46+00:00 2026-06-10T03:38:46+00:00

Question in Brief* When raising an event in a top level master page through

  • 0

Question in Brief*

When raising an event in a top level master page through one or more nested master pages, do you need to catch the event in the nested master page(s) and re-raise it? If not, what is the better approach to take?

* This is the question I’m hoping to have answered on SO.SE – the rest is just for more background information to help explain it.

Detailed Question, with Examples

An event in a masterpage (let’s call it “TopLevel.master”) needs to fire through all nested master pages (let’s assume there’s just one and call it “MidLevel.master”) to the page itself (Page.aspx). What I’m doing (below) works fine, but I’m concerned that I’m using the wrong approach and this will end up biting me in the derriere at some point in the future (e.g. when I come to maintain the code). It may also be a more costly approach, as several additional events are being created where they may not be needed.

Each nested master page and the page itself has a @MasterType declaration, so public properties, methods and events of the master pages are available via the code behind.

So far, I have something along the lines of (simplified for brevity):

TopLevel.master:

' A public event available from this master page
Public Event MyEvent(sender As Object, e As EventArgs)

' A function that can be called via "Master.DoSomething" to initiate the process
Public Sub DoSomething(someArgs As Object)
    Me._DoSomething(someArgs)
End Sub

' A local function that starts the process. SomeEventFiringObject is
' just an object that fires an event when the process is complete.
Private Sub _DoSomething(someArgs As Object)
    _someEventFiringObject.DoSomething(someArgs)
End Sub

' A local function that handles the process-complete event fired by
' SomeEventFiringObject and fires my public masterpage event
Private Sub Me_HandleEvent(sender As Object, e As EventArgs) Handles _someEventFiringObject.HasDoneSomething
    RaiseEvent MyEvent(sender, e)
End Sub

MidLevel.master:

' Handle the event from the masterpage.
Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
    AddHandler Master.MyEvent, AddressOf Master_MyEvent
End Sub

' A public event available from this master page 
' NOTE: This is essentially the same event as in the TopLevel master page.
Public Event MyEvent(sender As Object, e As EventArgs)

' A function that can be called via "Master.DoSomething" to initiate the process
' NOTE: Just passing this up to the TopLevel master page.
Public Sub DoSomething(someArgs As Object)
    Master.DoSomething(someArgs)
End Sub

' A local function that re-raises the event from this master page
' NOTE: Handler added on Page.Init.
Private Sub Master_MyEvent(sender As Object, e As EventArgs)
    RaiseEvent MyEvent(sender, e)
End Sub

Page.aspx:

' EDIT: This needs to be done on Page.Init as well.
Private Sub Page_Init(sender As Object, e As EventArgs) Handles Me.Init
    AddHandler Master.MyEvent, AddressOf Master_MyEvent
End Sub

' Some process initiation (button click in this example could be anything)
' The master page does the deed and will eventually raise the MyEvent event.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Master.DoSomething(someArgs)
End Sub

' The page handles the master MyEvent event (which is actually from MidLevel.master)
Private Sub Master_MyEvent(sender As Object, e As EventArgs)
    ' Finally I can do something with this event
End Sub

The Question:

It seems to me that there must be some way to access the MyEvent event fired in the TopLevel master page without having to effectively catch it in the MidLevel master page, then create a new event and copy it to send it on, but I can’t seem to work it out. Is there a way to do this? Or is the approach I’ve taken valid enough?

My concern is that I actually have two layers of master pages within MidLevel.master, so there seems to be a lot of copied code and several events being created for seemingly no reason other than to pass the event on.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T03:38:47+00:00Added an answer on June 10, 2026 at 3:38 am

    Ok, the first that you need is to create interface describing common functionality of your master pages. Something like this:

    public interface IMaster
    {
        event EventHandler MyEvent;
    }
    

    The in top master page inherited from this interface define your event:

    private static readonly object MyEventObject = new object();
    
    public event EventHandler MyEvent
    {
        add
        {
            Events.AddHandler(MyEventObject, value);
        }
        remove
        {
            Events.RemoveHandler(MyEventObject, value);
        }
    }
    
    private void RaiseMyEvent()
    {
        var handler = Events[MyEventObject] as EventHandler;
        if (handler != null)
        {
            handler(this, EventArgs.Empty);
        }
    }
    

    Then define event in nested master page but delegate adding it to invocation list to parent

    public event EventHandler MyEvent
    {
        add
        {
            ((IMaster)Master).MyEvent += value;
        }
        remove
        {
            ((IMaster)Master).MyEvent -= value;
        }
    }
    

    After all these steps you may use this event on page:

    protected void Page_Init(object sender, EventArgs e)
    {
        ((IMaster)Master).MyEvent += new EventHandler(WebForm3_MyEvent);
    }
    

    VB.NET version without interface

    Top Master:

    <asp:Button runat="server" Text="Click Me" OnClick="FireMyEvent" />
    
    Private ReadOnly MyEventObject As Object = New Object()
    
    Public Custom Event MyEvent As EventHandler
        AddHandler(value As EventHandler)
            Events.AddHandler(MyEventObject, value)
        End AddHandler
    
        RemoveHandler(value As EventHandler)
            Events.RemoveHandler(MyEventObject, value)
        End RemoveHandler
    
        RaiseEvent(sender As Object, e As System.EventArgs)
            Dim handler As EventHandler = TryCast(Events.Item(MyEventObject), EventHandler)
            If Not handler Is Nothing Then
                handler(Me, EventArgs.Empty)
            End If
        End RaiseEvent
    End Event
    
    Protected Sub FireMyEvent(sender As Object, e As EventArgs)
        RaiseEvent MyEvent(Me, EventArgs.Empty)
    End Sub
    

    Nested master:

    Public Custom Event MyEvent As EventHandler
        AddHandler(value As EventHandler)
            AddHandler DirectCast(Master, TopMaster).MyEvent, value
        End AddHandler
    
        RemoveHandler(value As EventHandler)
            RemoveHandler DirectCast(Master, TopMaster).MyEvent, value
        End RemoveHandler
    
        RaiseEvent(sender As Object, e As System.EventArgs)
        End RaiseEvent
    End Event
    

    Content page

    Protected Sub Page_Load(sender As Object, e As EventArgs)
        AddHandler DirectCast(Master, MasterPage2).MyEvent, AddressOf Foobar
    
    End Sub
    
    Private Sub Foobar(sender As Object, e As EventArgs)
        Response.Write("MyEvent handled from top master page at " & DateTime.Now.ToLongTimeString())
    End Sub
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Brief background: I have two-step login page, which after step 1 sends one-time code
I have a brief question regarding the vpa command one may use to evaluate
Brief question What command can I use to make my DataSet refresh it's connection
Here's the question in brief: My blog posts at... http://www.seanbradley.biz/blog ...totally lacks formatting. They're
In brief, my question is about member variables as pointers in unmanaged C++. In
I need a brief explanation on how the two commands isdigit() and isalpha() work.
Brief question, beginner's work on Ruby/Rails. Here's my view: <h1>News</h1> <%= @posts.each do |post|
I'll try and make this question as brief as possible. I was wondering if
I'm new to PHP, so this question might best be answered by a brief
I have a newbie question in JSF, and particular in Richfaces. I need my

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.