Friday, September 3, 2010

Handle events in Master and Content pages

If you have a web control on a master page and intend to access it on a content page, you can do so by exposing the control as a public property. This way you can register a handler for the control which is actually a property, in the content page.
Another way is to define your custom delegate and use it to define your custom event in the master page. Then in your content page, you can register a handler for the master page event as the code below shows:

protected void Page_Load(object sender, EventArgs e)
{
      this.Master.selectionChangeEvent += Content_Changed;
}

public void Content_Changed(object sender, TypeSelectionEventArgs args)
{
      Label1.Text = "Content changed to " + args.SelectedType.ToString();
}

You will need to define @MasterType directive in your content page. Also, you will more likely have your own custom data that needs to be transferred through this procedure. You can define your own Enums and Classes and pass it to your delegate to accomplish the job.

No comments:

Post a Comment