ASP.NET中如何为用户控件自定义事件?

先建立一个用户控件吧,这里就用一个简单登录用户控件来做演示。
 先来看看用户控件的前台代码(LogInOutControl.ascx文件):

  1. <%@ Control Language="c#" AutoEventWireup="false" Codebehind="LogInOutControl.ascx.cs" Inherits="ZZ.LogInOutControl" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
  2. <TABLE id="Table1" style="FONT-SIZE: 9pt; WIDTH: 183px; HEIGHT: 125px" cellSpacing="1"
  3.  cellPadding="1" width="183" align="center" border="1">
  4.  <TR>
  5.   <TD height="20">
  6.    <asp:Label id="LabelUser" runat="server">用户:</asp:Label>
  7.    <asp:TextBox id="TextBoxUserName" Width="128px" runat="server"></asp:TextBox></TD>
  8.  </TR>
  9.  <TR>
  10.   <TD height="20"><FONT face="宋体">
  11.     <asp:Label id="LabelPassword" runat="server">密码:</asp:Label>
  12.     <asp:TextBox id="TextBoxPassword" Width="128px" runat="server" TextMode="Password"></asp:TextBox></FONT></TD>
  13.  </TR>
  14.  <TR>
  15.   <TD align="center" height="20"><FONT face="宋体">
  16.     <asp:Button id="ButtonLogIn" Width="50px" Text="登录" runat="server"></asp:Button>
  17.     <asp:Button id="ButtonLogOut" Width="49px" Text="注销" runat="server"></asp:Button></FONT></TD>
  18.  </TR>
  19. </TABLE>

我们简单简单的放了两个Label,两个TextBox,两个Button以及一个Html表。
接下去就是为LogInOutControl.ascx.cs文件添加代码了。
首先定义一个delegate,其中LogInOutEventArgs类是从EventArgs类继承,
public delegate void LogInOutClickHandler(object sender,LogInOutEventArgs e);
我觉得把这个delegate放在LogInOutControl类外面更为合适。
接下去为控件声明了LogInOutClick事件,如下:
public event LogInOutClickHandler LogInOutClick;
另外为了更好的使用属性,加了Language枚举,
private Language language;
当然外部通过public Language Lg {get;set;}属性来访问。目的就是改变或者获取当前控件的显示。
接下去就是定义控件事件触发函数OnLogInOutClick,由按钮单击事件处理函数来完成对用户控件事件的触发。
完整代码如下:
  1. namespace ZZ
  2. {
  3.  using System;
  4.  using System.Data;
  5.  using System.Drawing;
  6.  using System.Web;
  7.  using System.Web.UI.WebControls;
  8.  using System.Web.UI.HtmlControls;
  9.  // 定义代理
  10.  public delegate void LogInOutClickHandler(object sender,LogInOutEventArgs e);
  11.  public class LogInOutControl : System.Web.UI.UserControl
  12.  {
  13.   protected System.Web.UI.WebControls.Button ButtonLogIn;
  14.   protected System.Web.UI.WebControls.TextBox TextBoxUserName;
  15.   protected System.Web.UI.WebControls.TextBox TextBoxPassword;
  16.   protected System.Web.UI.WebControls.Button ButtonLogOut;
  17.   protected System.Web.UI.WebControls.Label LabelUser;
  18.   protected System.Web.UI.WebControls.Label LabelPassword;
  19.   public event LogInOutClickHandler LogInOutClick;
  20.   private Language language;
  21.   //方法
  22.   public void ChangeLanguage(Language language)
  23.   {
  24.    this.Lg = language;
  25.   }
  26.   //属性
  27.   public Language Lg
  28.   {
  29.    set
  30.    {
  31.     if(value!=this.language)
  32.     {
  33.      if(value==Language.English)
  34.      {
  35.       this.LabelUser.Text = "User:";
  36.       this.LabelPassword.Text ="Password:";
  37.       this.ButtonLogIn.Text = "LogIn";
  38.       this.ButtonLogOut.Text = "LogOut";
  39.      }
  40.      else
  41.      {
  42.       this.LabelUser.Text = "用户:";
  43.       this.LabelPassword.Text ="密码:";
  44.       this.ButtonLogIn.Text = "登录";
  45.       this.ButtonLogOut.Text = "注销";
  46.      }
  47.     }
  48.    }
  49.   }
  50.   private void Page_Load(object sender, System.EventArgs e)
  51.   {
  52.    if(this.LabelUser.Text=="User:")
  53.      this.language = Language.English;
  54.     else
  55.      this.language = Language.Chinese;
  56.   }
  57.   private void OnLogInOutClick(object sender,LogInOutEventArgs e)
  58.   {
  59.    if(LogInOutClick!=null)
  60.     LogInOutClick(this,e);
  61.   }
  62.   #region Web 窗体设计器生成的代码
  63.   override protected void OnInit(EventArgs e)
  64.   {
  65.    InitializeComponent();
  66.    base.OnInit(e);
  67.   }
  68. private void InitializeComponent()
  69.   {
  70.    this.ButtonLogIn.Click += new System.EventHandler(this.ButtonLogIn_Click);
  71.    this.ButtonLogOut.Click += new System.EventHandler(this.ButtonLogOut_Click);
  72.    this.Load += new System.EventHandler(this.Page_Load);
  73.   }
  74.   #endregion
  75.   private void ButtonLogIn_Click(object sender, System.EventArgs e)
  76.   {
  77.    OnLogInOutClick(this,new LogInOutEventArgs(LogInClickType.LongIn,CustomValidate(this.TextBoxUserName.Text,this.TextBoxPassword.Text)));
  78.   }
  79.   private void ButtonLogOut_Click(object sender, System.EventArgs e)
  80.   {
  81.    //注销代码省略
  82.    OnLogInOutClick(this,new LogInOutEventArgs(LogInClickType.LongOut,true));
  83.   }
  84.   //验证函数
  85.   private bool CustomValidate(string userName,string password)
  86.   {
  87.    //验证代码省略,假设通过
  88.    return true;
  89.   }
  90.  }
  91. }

  1. namespace ZZ
  2. {
  3.  using System;
  4.  using System.Data;
  5.  using System.Drawing;
  6.  using System.Web;
  7.  using System.Web.UI.WebControls;
  8.  using System.Web.UI.HtmlControls;
  9.  // 定义代理
  10.  public delegate void LogInOutClickHandler(object sender,LogInOutEventArgs e);
  11.  public class LogInOutControl : System.Web.UI.UserControl
  12.  {
  13.   protected System.Web.UI.WebControls.Button ButtonLogIn;
  14.   protected System.Web.UI.WebControls.TextBox TextBoxUserName;
  15.   protected System.Web.UI.WebControls.TextBox TextBoxPassword;
  16.   protected System.Web.UI.WebControls.Button ButtonLogOut;
  17.   protected System.Web.UI.WebControls.Label LabelUser;
  18.   protected System.Web.UI.WebControls.Label LabelPassword;
  19.   public event LogInOutClickHandler LogInOutClick;
  20.   private Language language;
  21.   //方法
  22.   public void ChangeLanguage(Language language)
  23.   {
  24.    this.Lg = language;
  25.   }
  26.   //属性
  27.   public Language Lg
  28.   {
  29.    set
  30.    {
  31.     if(value!=this.language)
  32.     {
  33.      if(value==Language.English)
  34.      {
  35.       this.LabelUser.Text = "User:";
  36.       this.LabelPassword.Text ="Password:";
  37.       this.ButtonLogIn.Text = "LogIn";
  38.       this.ButtonLogOut.Text = "LogOut";
  39.      }
  40.      else
  41.      {
  42.       this.LabelUser.Text = "用户:";
  43.       this.LabelPassword.Text ="密码:";
  44.       this.ButtonLogIn.Text = "登录";
  45.       this.ButtonLogOut.Text = "注销";
  46.      }
  47.     }
  48.    }
  49.   }
  50.   private void Page_Load(object sender, System.EventArgs e)
  51.   {
  52.    if(this.LabelUser.Text=="User:")
  53.      this.language = Language.English;
  54.     else
  55.      this.language = Language.Chinese;
  56.   }
  57.   private void OnLogInOutClick(object sender,LogInOutEventArgs e)
  58.   {
  59.    if(LogInOutClick!=null)
  60.     LogInOutClick(this,e);
  61.   }
  62.   #region Web 窗体设计器生成的代码
  63.   override protected void OnInit(EventArgs e)
  64.   {
  65.    InitializeComponent();
  66.    base.OnInit(e);
  67.   }
  68. private void InitializeComponent()
  69.   {
  70.    this.ButtonLogIn.Click += new System.EventHandler(this.ButtonLogIn_Click);
  71.    this.ButtonLogOut.Click += new System.EventHandler(this.ButtonLogOut_Click);
  72.    this.Load += new System.EventHandler(this.Page_Load);
  73.   }
  74.   #endregion
  75.   private void ButtonLogIn_Click(object sender, System.EventArgs e)
  76.   {
  77.    OnLogInOutClick(this,new LogInOutEventArgs(LogInClickType.LongIn,CustomValidate(this.TextBoxUserName.Text,this.TextBoxPassword.Text)));
  78.   }
  79.   private void ButtonLogOut_Click(object sender, System.EventArgs e)
  80.   {
  81.    //注销代码省略
  82.    OnLogInOutClick(this,new LogInOutEventArgs(LogInClickType.LongOut,true));
  83.   }
  84.   //验证函数
  85.   private bool CustomValidate(string userName,string password)
  86.   {
  87.    //验证代码省略,假设通过
  88.    return true;
  89.   }
  90.  }
  91. }
另外一个文件定义了枚举和参数类:
  1. using System;
  2. namespace ZZ
  3. {
  4.  public class LogInOutEventArgs : EventArgs
  5.  {
  6.   private LogInClickType type;
  7.   private bool result;
  8.  
  9.   public LogInOutEventArgs(LogInClickType type,bool result):base()
  10.   {
  11.    this.type = type;
  12.    this.result = result;
  13.   }
  14.   public LogInClickType Type
  15.   {
  16.    get{return this.type;}
  17.   }
  18.   //操作结果,
  19.   public bool Result
  20.   {
  21.    get{return this.result;}
  22.   }
  23.  }
  24.  //操作类型
  25.  public enum LogInClickType : int
  26.  {
  27.   LongIn,
  28.   LongOut
  29.  }
  30.  //定义语言
  31.  public enum Language
  32.  {
  33.   Chinese,
  34.   English
  35.  }
  36. }

接下去看看在aspx页面里面使用。
新建一个Default.aspx页面,拖一个LogInOutControl用户控件到上面。
  1. <%@ Register TagPrefix="uc1" TagName="LogInOutControl" Src="LogInOutControl.ascx" %>
  2. <%@ Page language="c#" Codebehind="Default.aspx.cs" AutoEventWireup="false" Inherits="ZZ.Default" %>
  3. <%@ Import Namespace="ZZ" %>
  4. <HTML>
  5.  <HEAD>
  6.   <title>WebForm1</title>
  7.  </HEAD>
  8.  <body>
  9.   <form id="Form1" method="post" runat="server">
  10.    <FONT face="宋体">
  11.     <uc1:LogInOutControl id="LogInOutControl1" runat="server">
  12.     </uc1:LogInOutControl>
  13.     <asp:Label id="LabelMsg" runat="server"></asp:Label>
  14.     <asp:DropDownList id="DropDownList1" runat="server" AutoPostBack="True">
  15.      <asp:ListItem Value="0" Selected="True">中文</asp:ListItem>
  16.      <asp:ListItem Value="1">英文</asp:ListItem>
  17.     </asp:DropDownList></FONT>
  18.   </form>
  19.  </body>
  20. </HTML>

在后台代码中添加事件和属性。
虽然在前台添加了LogInOutControl1,但是后台代码中不会生成protected LogInOutControl LogInOutControl1;这条语句,我觉得很奇怪,不管先加上他。
接着在Page_Load事件中注册LogInOutClick事件:
this.LogInOutControl1.LogInOutClick += new LogInOutClickHandler(LogInOutControl1_LogInOutClick);

完整代码如下:

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.SessionState;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.HtmlControls;
  11. namespace ZZ
  12. {
  13.  public class Default : System.Web.UI.Page
  14.  {
  15.   protected System.Web.UI.WebControls.Label LabelMsg;
  16.   protected System.Web.UI.WebControls.DropDownList DropDownList1;
  17.   protected LogInOutControl LogInOutControl1;
  18.   private void Page_Load(object sender, System.EventArgs e)
  19.   {
  20.    //注册用户控件事件
  21.    this.LogInOutControl1.LogInOutClick += new LogInOutClickHandler(LogInOutControl1_LogInOutClick);
  22.   }
  23.   #region Web 窗体设计器生成的代码
  24.   override protected void OnInit(EventArgs e)
  25.   {
  26.    InitializeComponent();
  27.    base.OnInit(e);
  28.   }
  29.   private void InitializeComponent()
  30.   {   
  31.    this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
  32.    this.Load += new System.EventHandler(this.Page_Load);
  33.   }
  34.   #endregion
  35.   private void LogInOutControl1_LogInOutClick(object sender, LogInOutEventArgs e)
  36.   {
  37.    switch(e.Type)
  38.    {
  39.     case LogInClickType.LongIn:
  40.      this.LabelMsg.Text = "你点击了登录按钮,操作结果:"+e.Result.ToString();
  41.      break;
  42.     case LogInClickType.LongOut:
  43.      this.LabelMsg.Text = "你点击了注销按钮,操作结果:"+e.Result.ToString();
  44.      break;
  45.    }
  46.   }
  47. private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
  48.   {
  49.    this.LogInOutControl1.Lg = (Language)this.DropDownList1.SelectedIndex;
  50.    //this.LogInOutControl1.ChangeLanguage((Language)this.DropDownList1.SelectedIndex);
  51.   }
  52.  }
  53. }

  1. using System;
  2. using System.Collections;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Web;
  7. using System.Web.SessionState;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.HtmlControls;
  11. namespace ZZ
  12. {
  13.  public class Default : System.Web.UI.Page
  14.  {
  15.   protected System.Web.UI.WebControls.Label LabelMsg;
  16.   protected System.Web.UI.WebControls.DropDownList DropDownList1;
  17.   protected LogInOutControl LogInOutControl1;
  18.   private void Page_Load(object sender, System.EventArgs e)
  19.   {
  20.    //注册用户控件事件
  21.    this.LogInOutControl1.LogInOutClick += new LogInOutClickHandler(LogInOutControl1_LogInOutClick);
  22.   }
  23.   #region Web 窗体设计器生成的代码
  24.   override protected void OnInit(EventArgs e)
  25.   {
  26.    InitializeComponent();
  27.    base.OnInit(e);
  28.   }
  29.   private void InitializeComponent()
  30.   {   
  31.    this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
  32.    this.Load += new System.EventHandler(this.Page_Load);
  33.   }
  34.   #endregion
  35.   private void LogInOutControl1_LogInOutClick(object sender, LogInOutEventArgs e)
  36.   {
  37.    switch(e.Type)
  38.    {
  39.     case LogInClickType.LongIn:
  40.      this.LabelMsg.Text = "你点击了登录按钮,操作结果:"+e.Result.ToString();
  41.      break;
  42.     case LogInClickType.LongOut:
  43.      this.LabelMsg.Text = "你点击了注销按钮,操作结果:"+e.Result.ToString();
  44.      break;
  45.    }
  46.   }
  47. private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
  48.   {
  49.    this.LogInOutControl1.Lg = (Language)this.DropDownList1.SelectedIndex;
  50.    //this.LogInOutControl1.ChangeLanguage((Language)this.DropDownList1.SelectedIndex);
  51.   }
  52.  }
  53. }
当 用户在前台通过选择下拉框列表来改变控件的语言,这里通过Lg属性来完成,不过这里也加了一个方法ChangeLanguage也可以实现同样的功能。另 外,通过点击登陆或注销按钮触发LogInOutClick事件来给页面中的LabelMsg.Text属性赋值从而得到操作结果。
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值