Please note: If you add a event called ButtonClicked to the usercontrol, in the markup, .net convert it to OnButtonClicked.
UserControl : Code
public partial class ActiveUserControl : UserControl { public event EventHandler ButtonClicked; protected void InvokeButtonClicked(object sender, EventArgs e) { if (this.ButtonClicked != null) this.ButtonClicked(sender, e); } }
Control : Markup
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ActiveUserControl.ascx.cs" Inherits="ActiveTest.UserControls.ActiveUserControl" %> <asp:Button runat="server" ID="btnButton" Text="Button" OnClick="InvokeButtonClicked" />
Consuming page:
<%@ Page Language="C#" %> <%@ Register TagName="uc" TagPrefix="active" Src="~/UserControls/ActiveUserControl.ascx" %> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <script runat="server"> protected void ButtonClicked(object sender, EventArgs e) { /// /// Bubbled up event /// } </script> </head> <body> <form id="form1" runat="server"> <active:uc runat="server" ID="ucUserControl" OnButtonClicked="ButtonClicked" /> </form> </body> </html>
No comments:
Post a Comment