1

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.

Dinesh Persaud
  • 155
  • 2
  • 6
  • 18
  • Why do you want your content page handling events for your master page, when the master page is certainly capable of handling its own events? – Karl Anderson Aug 20 '13 at 18:56
  • @KarlAnderson because the master page is acting as a template that has an empty gridview and some buttons (add, edit, delete). It is at the content page where the gridview will get data as well as processed. – Dinesh Persaud Aug 20 '13 at 18:57

1 Answers1

4

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
}
afzalulh
  • 7,925
  • 2
  • 26
  • 37