"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Monday, January 21, 2013

Invoking an Event in ASPX Page from UserControl

 UserControl
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />



UserControl CodeBehind
public partial class WebUserControl : System.Web.UI.UserControl
{
    public event EventHandler usrCtrlButtonClick;

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        usrCtrlButtonClick.Invoke(this, new EventArgs());
    }
}


ASPX Page
 protected void Page_Load(object sender, EventArgs e)
    {

        this.WebUserControl1.usrCtrlButtonClick += new EventHandler(this.usrctrlButtonClick);
   }

    protected void usrctrlButtonClick(object sender, EventArgs e)
    {
        Response.Write("From aspx page");
    }


If u had any trouble just ask, Happy to help u :)
Stay Tune...
Have a nice day... 'N happy Coding :)

No comments: