Master Page controllers event handle in content page

4.1k Views Asked by At

I have a Master page that have an Image Button and a GridView.

<asp:ImageButton ID="ImageButton2" runat="server" Height="48px" ImageUrl="~/IconsPack/Add.png" ToolTip="Add Record" CssClass="morph" Width="48px"/>

        <asp:GridView ID="GridView1" runat="server" CssClass="EU_DataTable" EmptyDataText="No Data Available.">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1" runat="server" />  
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>

In my content page (.aspx) I am able to attach a data source to my gridview and it works good. Where is need some help is .. I would like to handle the events for the image button (as well as interacting with the gridview) in my CONTENT PAGE (.aspx) Again, the image button and the gridview are in the master and it will be populate when the content page is load. An example of what i would like to do .. the grive view has a check box inside..so i want to check it for a particular row and click the imagebutton to delete that row. It might sound a little confusing.. if anyone dont understand .. i can explain further. please assist..im lost.

1

There are 1 best solutions below

1
On

You can do like this:

In the masterpage codebehind:

public event EventHandler MasterControlClicked;

protected void ImageButton2_Click(object sender, EventArgs e)
{
    OnMasterControlClick(e);

}

protected void OnMasterControlClick(EventArgs e)
{
    if (this.MasterControlClicked != null)
    {
        this.MasterControlClicked(this, e);
    }
}

And in the content page:

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    if (this.Master is SiteMaster)
    {
        ((SiteMaster)this.Master).MasterControlClicked += new EventHandler(ContentControlClicked);
    }
}

private void ContentControlClicked(object sender, EventArgs e)
{
    //do whatever you need
}