ASP.NET User Controls are generally suitable for generating relative static content, so there is no Builtin's event support. This article discusses the method of returning the event of user controls.
Assume that the user control (UserControl.ascx) contains the button control Abutton, you want to implement the page with the Abutton button, the page containing the user control can receive an event. To this end, the small chick shot has processed in the code of the user control and the page.
Processing in UserControl.ascx.cs: 1. Define public event delegates such as ClickeventHandler; 2. Declare events in the UserControl class, such as Click; 3. Define the way to raise events in the UserControl class, such as an onclick () method; 4. Methods to trigger events in the relevant method of the UserControl class, such as calling onClick () in Button_Click ().
Core code to indicate the following: public delegate void ClickEventHandler (object sender, EventArgs e); public class MyUserControl: System.Web.UI.UserControl {protected System.Web.UI.WebControls.Button AButton; public event ClickEventHandler Click; protected void OnClick ( Eventargs e) {if (click! = Null) Click (this, e);} private void Abutton_click (Object sender, system.eventargs e) {this.onclick (e);}}
Handling in the page CS file containing UserControl: 1. InitializationComponent () Add the event handler, find UserControl; 2. Define the event handler method in this method, such as UserControl_Clicked (). Core code to indicate the following: private void InitializeComponent () {this.Load = new System.EventHandler (this.Page_Load); MyUserControl uc = this.FindControl ( "myUserControlID") as MyUserControl; uc.Click = new ClickEventHandler (this. UserControl_Clicked);} private void userControl_clicked (object sender, system.eventargs e) {// usercontrol_clicked event hanlder}