Here is an example how to call event in UserControl placed in Master Page from Content Page.
UserControl ASPX
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="MyControl.ascx.cs"
Inherits="UserControls_MyControl" %>
<asp:Button ID="Button1" runat="server" Text="Button in UserControl" OnClick="Submit" />
UserControl Code
Make sure the event has to be public and not protected if you want to access it everywhere.
public void Submit(object sender, EventArgs e)
{this.Page.ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Button is clicked!');", true);
}
MasterPage
<%@ Register Src="~/UserControls/MyControl.ascx" TagName="Control" TagPrefix="uc" %>
<!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>
<asp:ContentPlaceHolder ID="head" runat="server">
</asp:ContentPlaceHolder>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc:Control ID="ucButtonUserControl" runat="server" />
<asp:ContentPlaceHolder ID="ContentPlaceHolder1" runat="server">
</asp:ContentPlaceHolder>
</div>
</form>
</body>
</html>
Content Page ASPX
Here it is very important that you need to Reference the UserControl so that you can access its class.
<%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Reference Control = "~/UserControls/MyControl.ascx" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<asp:Button ID="btnPageButton" runat="server" Text="PageButton" OnClick="PageButtonClick" />
</asp:Content>
Content Page Code
protected void PageButtonClick(object sender, EventArgs e)
{
UserControls_MyControl uc = this.Master.FindControl("ucButtonUserControl") as UserControls_MyControl;
uc.Submit(null, null);
}