简体   繁体   中英

Calling a content page method from nested master pages

I'm currently having two master pages M1 and M2 and several content pages.

M2 has a calendar control and I want to call a content page function each time the selection on the calendar changes.

Here's the code I have in my master page:

public partial class Master2 : BaseMasterPage
{
  public event EventHandler CalendarSelectionChanged;

  public void Calendar_OnSelectionChanged(object sender, EventArgs e)
  {
        if (CalendarSelectionChanged != null)
            CalendarSelectionChanged(this, EventArgs.Empty);
  }
}

And here's the code in the content Page C1:

protected void Page_PreInit(object sender, EventArgs e)
    {
        Master.CalendarSelectionChanged += new EventHandler(OnMainCalendarSelectionChanged_SubContent);
    }

 private void OnMainCalendarSelectionChanged_SubContent(object sender, EventArgs e)
    {
        DoSomething();
    }

but the CalendarSelectionChanged is always null and hence the function isn't called.

After some serious researching and dabbling around, I've finally made the calendar in the Master page to trigger an event in the content page.

The Code in the Content Page is the same as mentioned in the question.

The Code in the Second Master page(Nested) is Changed to :

public partial class Master2 : BaseMasterPage
{
  public event EventHandler CalendarSelectionChanged;

  public void Calendar_OnSelectionChanged(object sender, EventArgs e)
  {
        OnCalendar_SelectionChanged_CustomEvent(e);

      ///.....
  }
 public virtual void OnCalendar_SelectionChanged_CustomEvent(EventArgs e)
    {
        if (Calendar_SelectionChanged_CustomEvent != null)
            Calendar_SelectionChanged_CustomEvent(this, EventArgs.Empty);
    }
}

Add the virtual reference path in the content page:

<%@ MasterType VirtualPath="~/MasterPage.master" %> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM