ASP.NET用户控件事件的定义和实践-- 自定义事件数据类

 事件功能是由三个互相联系的元素提供的:提供事件数据的类、事件委托和引发事件的类。.NET Framework 具有命名与事件相关的类和方法的约定。如果想要您的类引发一个名为 EventName 的事件,您需要以下元素。
  • 持有事件数据的类,名为 EventNameEventArgs。该类必须从 tabindex="" keywords="frlrfSystemEventArgsClassTopic" />System.EventArgs 导出。
  • 事件的委托,名为 EventNameEventHandler。
  • 引发事件的类。该类必须提供:
    1. 事件声明。 public event EventNameEventHandler EventName;

       

    2. 事件的方法,名为 OnEventName

   下面 分别用一个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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值