0

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Zee
  • 622
  • 4
  • 13
  • which calendar control are you using? – McNets Dec 02 '16 at 15:35
  • asp.net calendar control, the Calendar_OnSelectionChanged method is firing and good, only the event handler is always null. – Zee Dec 02 '16 at 15:44
  • `public void Calendar_OnSelectionChanged(object sender, EventArgs e)` is fired? – McNets Dec 02 '16 at 15:50
  • yeah, the calendar and all is good, only the custom event is never raised. :( – Zee Dec 02 '16 at 15:51
  • have you tried finding the control: `Calendar cal = this.Master.FindControl("Calendar1"); cal.OnSelectionChanged += OnMainCalendarSelectionChanged_SubContent;` – McNets Dec 02 '16 at 16:01
  • http://stackoverflow.com/questions/13620435/calling-content-page-method-from-masterpage-method this is the link to the method i've used. everything works except the eventhandler is always null. – Zee Dec 02 '16 at 16:08
  • have you added this line: `<%@ MasterType VirtualPath="~/MasterPage.master" %> ` – McNets Dec 02 '16 at 16:10
  • yeah <%@ MasterType VirtualPath="MasterPage2.master" %> – Zee Dec 02 '16 at 16:12
  • @McNets: I figured it out, thanks for your help. I've posted the code that works for me now. – Zee Mar 19 '17 at 23:13

1 Answers1

0

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" %> 
Zee
  • 622
  • 4
  • 13