Search code examples
c#asp.netmaster-pagescontent-pages

Passing int and string values from masterpage to content page


I have a radio list in my master page and they fire an event when I select one of them.

Now this event isn't not controlled by my master page instead it is controlled by my content page. My question is, is it possible to pass int/Strings from the master page method to content page method somehow.

P.S i want to pass the int i to content page method in this case

This is how i tide them up.

Master page Code to handle event

public event EventHandler Master_Save;
  ...
public void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
{
    int i=RadioButtonList1.SelectedIndex;        
    if(Master_Save!=null)
    { Master_Save(this, EventArgs.Empty); }
}

and my content page code to handle the event

protected override void OnPreInit(EventArgs e)
{
    base.OnPreInit(e);
    (this.Page.Master as Pages_MasterPage).Master_Save += new EventHandler(ContentPage_Save);
}

 private void ContentPage_Save(object sender, EventArgs e)
{
    //Code that changes a query   

 }

Solution

  • Sure you can, just define a custom event args class and parametrize your event with it:

    public class MasterSaveEventArgs : EventArgs
    {
        public int Index { get; private set; }
        public MasterSaveEventArgs(int index)
        {
            this.Index = index;
        }
    }
    

    And then just use it:

    public event EventHandler<MasterSaveEventArgs> Master_Save;
    ...
    { Master_Save(this, new MasterSaveEventArgs(i)); }
    ...
    (this.Page.Master as Pages_MasterPage).Master_Save += ContentPage_Save;
    // notice the shortened syntax here
    ...
    private void ContentPage_Save(object sender, MasterSaveEventArgs e)