tags:

views:

56

answers:

1

I have an event in the Master page that I want to access in the pages that use that master page but it doesn't seem to be working.

In the Master

public delegate void NotifyRequest(object sender, EventArgs e);
public class MyMaster
{
   public event NotifyRequest NewRequest;
   protected void uiBtnNewTask_Click(object sender, EventArgs e)
   { 
      if(NewRequest!= null)
         NewRequest(this, e)
   }
}

Inherited Page

MyMaster mm = new MyyMaster();
mm.NewRequest += new NotifyRequest(mm_NotifyRequest);
void mm_NotifyRequest(object sender, EventArgs e)
{
    Label1.Text = "Wow";
    this.Label1.Visible = true;
}

My problem is the event is always null. Any ideas?

+2  A: 

You probably need to use the following syntax to access your event in the Master Page

((MyMaster)Master).NewRequest += new NotifyRequest(mm_NotifyRequest);

If you wish to access members of a master page you need to use the page Master attribute and cast it to your master page.

Alternately

If you wish do not wish to use a cast you can use the @MasterType directive to create a strong reference to your master page, in this case MyMaster. So you would be able to access your event like so: Master.NewRequest.

More Reading about Master Pages

cgreeno
Yes that was the problem thanks