- 持有事件数据的类,名为 EventNameEventArgs。该类必须从 tabindex="" keywords="frlrfSystemEventArgsClassTopic" />System.EventArgs 导出。
- 事件的委托,名为 EventNameEventHandler。
- 引发事件的类。该类必须提供:
- 事件声明。 [C#]public event EventNameEventHandler EventName;
- 事件的方法,名为 OnEventName。
- 事件声明。 [C#]public event EventNameEventHandler EventName;
下面 分别用一个winform控件和webform控件的实现来分别说明。
界面:
private System.Windows.Forms.Button btnSubmit;
private System.Windows.Forms.Button btnCancle;
private System.Windows.Forms.TextBox UsrID;
private System.Windows.Forms.TextBox UsrName;
private System.Windows.Forms.TextBox UsrPwd;
private System.Windows.Forms.TextBox Valid;
控件:UserControl1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace wFromControls
{
public partial class UserControl1 : UserControl
{
public UserControl1()
{
InitializeComponent();
}
public event UserLoginEventHandler SubmitLogin;
public event CancelEventHandler Cancel;
protected virtual void OnSubmitLogin(EventLogonArgs e)
{
if (this.SubmitLogin != null)
{
SubmitLogin(this, e);
}
}
protected virtual void OnCancel(EventArgs e)
{
if (this.Cancel != null)
{
Cancel(this, e);
}
}
private void btnOK_Click(object sender, System.EventArgs e)
{
if(this.UsrID.Text != "" && this.UsrName.Text !="" && this.UsrPwd.Text !="")
{
// intLoginTime++;
OnSubmitLogin(new EventLogonArgs(UsrID.Text, UsrName.Text, UsrPwd.Text));
// bLogin = TestUserInDB(new EventLogonArgs(txtID.Text,txtName.Text,txtPWD.Text));
//MessageBox.Show("this is the btnOK_click function!","In control",MessageBoxButtons.OK);
//if(!bLogin)
//MessageBox.Show("Login in Failed!","Login Error",MessageBoxButtons.OK);
}
else
{
MessageBox.Show("Your must input all the items!","Login Info",MessageBoxButtons.OK);
}
}
private void btnCancel_Click(object sender,System.EventArgs e)
{
OnCancel(e);
}
}
public class EventLogonArgs : System.EventArgs
{
private string UsrID;
private string UsrName;
private string UsrPwd;
private bool IsValid;
public EventLogonArgs(string UsrID,string UsrName,string UsrPwd)
{
this.UsrID = UsrID;
this.UsrName = UsrName;
this.UsrPwd = UsrPwd;
}
}
public delegate void UserLoginEventHandler(object sender, EventLogonArgs e);
public delegate void CancelEventHandler(object sender,EventArgs e);
}
Tset form
private void userControl11_SubmitLogin(object sender, wFromControls.EventLogonArgs e)
{
MessageBox.Show("AAAAAA");
}
private void userControl11_Cancel(object sender, EventArgs e)
{
MessageBox.Show("BBBBB");
}
下面是web下面的控件:(改掉相应的类和注册方法就行了)
loginControl.ascx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace stoneControls
{
public partial class loginControl : System.Web.UI.UserControl
{
public event UserLoginEventHandler SubmitLogin;
public event CancelEventHandler Cancel;
protected virtual void OnSubmitLogin(EventLogonArgs e)
{
if (this.SubmitLogin != null)
{
SubmitLogin(this, e);
}
}
protected virtual void OnCancel(EventArgs e)
{
if (this.Cancel != null)
{
Cancel(this, e);
}
}
private void submit_Click(object sender, System.EventArgs e)
{
if(this.UsrID.Text != "" && this.UsrName.Text !="" && this.UsrPwd.Text !="")
{
// intLoginTime++;
OnSubmitLogin(new EventLogonArgs(UsrID.Text, UsrName.Text, UsrPwd.Text));
//string strJS="<script language='javascript'>";
//strJS+="alert('OK!');";
//strJS+="</script>";
//Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"22",strJS,true);
// bLogin = TestUserInDB(new EventLogonArgs(txtID.Text,txtName.Text,txtPWD.Text));
//MessageBox.Show("this is the btnOK_click function!","In control",MessageBoxButtons.OK);
//if(!bLogin)
//MessageBox.Show("Login in Failed!","Login Error",MessageBoxButtons.OK);
}
else
{
//string strJS="<script language='javascript'>";
//strJS+="alert('Your must input all the items!');";
//strJS+="</script>";
//Page.ClientScript.RegisterClientScriptBlock(this.GetType(),"22",strJS,true);
//.......
}
}
private void cancle_Click(object sender, System.EventArgs e)
{
OnCancel(e);
}
protected void Page_Load(object sender, EventArgs e)
{
this.submit.Click+=new EventHandler(submit_Click);
this.cancle.Click+=new EventHandler(cancle_Click);
}
}
public class EventLogonArgs : System.EventArgs
{
private string UsrID;
private string UsrName;
private string UsrPwd;
private bool IsValid;
public EventLogonArgs(string UsrID,string UsrName,string UsrPwd)
{
this.UsrID = UsrID;
this.UsrName = UsrName;
this.UsrPwd = UsrPwd;
}
}
public delegate void UserLoginEventHandler(object sender, EventLogonArgs e);
public delegate void CancelEventHandler(object sender,EventArgs e);
}
loginControl.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="loginControl.ascx.cs" Inherits="stoneControls.loginControl" %>
<table id="TestControl">
<tr>
<td><asp:TextBox ID="UsrName" runat="Server"></asp:TextBox></td>
<td><asp:TextBox ID="UsrID" runat="Server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:TextBox ID="UsrPwd" runat="Server"></asp:TextBox></td>
<td><asp:TextBox ID="TextBox2" runat="Server"></asp:TextBox></td>
</tr>
<tr><td align="Center" colspan="2">
<asp:Button ID="submit" Text="提交" runat="server" />
<asp:Button ID="cancle" Text="取消" runat="server" /></td>
</tr>
</table>
引用页面:buutonListTest .aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace stoneControls
{
public partial class buutonListTest : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
this.LoginControl1.SubmitLogin+=new UserLoginEventHandler(LoginControl1_SubmitLogin);
this.LoginControl1.Cancel+=new CancelEventHandler(LoginControl1_Cancel);
}
protected void LoginControl1_SubmitLogin(object sender, EventLogonArgs e)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "00", "alert('OK');", true);
}
protected void LoginControl1_Cancel(object sender, EventArgs e)
{
Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "11", "alert('cancle');", true);
}
}
}
buutonListTest .aspx
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<uc2:loginControl id="LoginControl1" runat="server">
</uc2:loginControl></div>
</form>
</body>
</html>