How to call Master page ImageButton Click event to Nested or Derived Pages in Asp.net and C#.net


Introduction:

In this tutorial i will explain how the single button in the master page works for all the nested pages for their click events. Here i have shown you by creating a master page and a ImageButton in it. and a text box in a derived page. I am calling the Masterpage Click event to the derived Page.

***  Important Steps to follow  ***                                                    

1. Creating Property class in Master page.
2. Giving Masterpage virtual directory in Nested Page(.aspx) at page directory.
3. Creating Event by Event handler in Page_Init in Nested page (.cs)

Master page Design: ( .Master )                                                       

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="Masterpage.master.cs" Inherits="MastereventstoClient.Masterpage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">

        </asp:ContentPlaceHolder>
        <asp:ImageButton ID="Approve" ImageUrl="~/Approve.png" runat="server" />
    </div>
    </form>
</body>
</html>

Master page Codebehind( .cs )                                                          

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MastereventstoClient
{
    public partial class Masterpage : System.Web.UI.MasterPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        public ImageButton somename      //Property class !important
        {
            get { return btnApprove; }
        }
    }
}

Nested page ( .Aspx )                                                                         

<%@ Page Title="" Language="C#" MasterPageFile="~/Masterpage.Master" AutoEventWireup="true" CodeBehind="nestedpage.aspx.cs" Inherits="MastereventstoClient.nestedpage" MaintainScrollPositionOnPostback="true" %>

<%@ MasterType VirtualPath="~/Masterpage.Master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">

<asp:TextBox ID="txtname" runat="server"></asp:TextBox>
</asp:Content>

Nested page code Behind ( .cs )                                                         

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MastereventstoClient
{
    public partial class nestedpage : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void Page_Init(object sender, EventArgs e)
        {
            Master.somename.Click += new ImageClickEventHandler(somenameClick); //!important
        }
        protected void somenameClick(object sender, EventArgs e)
        {
            ClientScript.RegisterStartupScript(this.GetType(), "Scriptname", "javascript:alert('" + txtname.Text+" It is working"+ "');", true);
        }
    }
}