使用WebUserControl来创建控件的时候,事件的定义方法:
控件:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SuperControl.ascx.cs" Inherits="SuperControl" %>
<div>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</div>
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" OnTextChanged="DropDownList1_TextChanged">
<asp:ListItem>1</asp:ListItem>
<asp:ListItem>2</asp:ListItem>
<asp:ListItem>3</asp:ListItem>
<asp:ListItem>4</asp:ListItem>
</asp:DropDownList>
</div>
后台:
public partial class SuperControl : System.Web.UI.UserControl
{
public event EventHandler TextChanged;//事件委托
protected void DropDownList1_TextChanged(object sender, EventArgs e)
{
if (TextChanged != null)
{
TextChanged(this, e);
}
}
}
在页面中的使用方法:
<SuperControl:SupperControl ID="myControl" runat="server" OnTextChanged="myControl_OnTextChanged"/>