Search code examples
asp.neteventsmaster-pagescontent-pages

attach two ASP.NET content page buttons to one masterpage event


I have one master page with two content page each content page has submit button:

<asp:ImageButton runat="server" type="image" id="buttonSubmit" name="buttonSubmit"
    alt="ImageButton 1" src="images/button.png" OnClientClick="PreventExitPop=true"/>

I want to be able to create one onclick event on the masterpage.cs:

protected void buttonSubmit_Click(object sender, EventArgs e)
{
    //
}

and attach the two buttons from the content pages to this event.

<asp:ImageButton runat="server" type="image" id="buttonSubmit" name="buttonSubmit"
    alt="ImageButton 1" src="images/button.png"
    onclick="buttonSubmit_Click"
    OnClientClick="PreventExitPop=true"/>

The problem is that each content page knows only his code file and not the masterpage.cs.


Solution

  • You could write event handlers for each control and in those handlers you call a event handler method in the master page

    protected void buttonSubmit_Click(object sender, EventArgs e) 
    { 
        ((MasterType) this.Master).buttonSubmit_Click(sender, e);
    }