转至繁体中文版     | 网站首页 | 图文教程 | 资源下载 | 站长博客 | 图片素材 | 武汉seo | 武汉网站优化 | 
最新公告:     敏韬网|教学资源学习资料永久免费分享站!  [mintao  2008年9月2日]        
您现在的位置: 学习笔记 >> 图文教程 >> 站长学院 >> Web开发 >> 正文
在asp.net中为Web用户控件添加属性和事件         ★★★★

在asp.net中为Web用户控件添加属性和事件

作者:闵涛 文章来源:闵涛的学习笔记 点击数:1752 更新时间:2009/4/23 10:50:01

在asp.net中为Web用户控件添加属性和事件

         在90年代初,Microsoft为Web程序员提供的 Active Server Pages(ASP)革命性地改变了Web的编程。它可以利用十分易用的模型在Web服务器上动态生成HTML,并且很容易的实现了对数据库的访问,就当时来说,这是一项多么吸引人的技术,包括现在Internet上的许多web站点都是用Asp写的,我的同事前辈们更是玩Asp的高手,经历这么多年而不衰,可见他的成功。
         但是,技术是在不断的发展着,引用某位Net专家的话讲――如今Web编程的状态还是落后的。因此Microsoft提出了第二代编程模型――Web窗体。Web窗体模型作为Asp.net的一部分,而Asp.net又是.Net框架的一个部分。他的编程模型是基于事件的,使用他更像是在进行Windows窗体编程,这一点也正是我决定去学习使用他的一个重要原因,也胡乱看了一些这方面的书,写这篇文章的目的也就是和各位Asp.net初学者和还没有为用户控件添加过自定义事件的同行分享一下经验。
 废话少说,下面就让我们先建立一个用户控件吧,这里就用一个简单登录用户控件来做演示。
 先来看看用户控件的前台代码(LogInOutControl.ascx文件):
<%@ Control Language="c#" AutoEventWireup="false" Codebehind="LogInOutControl.ascx.cs" Inherits="ZZ.LogInOutControl" TargetSchema="http://schemas.microsoft.com/intellisense/ie5"%>
<TABLE id="Table1" style="FONT-SIZE: 9pt; WIDTH: 183px; HEIGHT: 125px" cellSpacing="1"
 cellPadding="1" width="183" align="center" border="1">
 <TR>
  <TD height="20">
   <asp:Label id="LabelUser" runat="server">用户:</asp:Label>
   <asp:TextBox id="TextBoxUserName" Width="128px" runat="server"></asp:TextBox></TD>
 </TR>
 <TR>
  <TD height="20"><FONT face="宋体">
    <asp:Label id="LabelPassword" runat="server">密码:</asp:Label>
    <asp:TextBox id="TextBoxPassword" Width="128px" runat="server" TextMode="Password"></asp:TextBox></FONT></TD>
 </TR>
 <TR>
  <TD align="center" height="20"><FONT face="宋体">
    <asp:Button id="ButtonLogIn" Width="50px" Text="登录" runat="server"></asp:Button>
    <asp:Button id="ButtonLogOut" Width="49px" Text="注销" runat="server"></asp:Button></FONT></TD>
 </TR>
</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,由按钮单击事件处理函数来完成对用户控件事件的触发。
完整代码如下:
namespace ZZ
{
 using System;
 using System.Data;
 using System.Drawing;
 using System.Web;
 using System.Web.UI.WebControls;
 using System.Web.UI.HtmlControls;

 // 定义代理
 public delegate void LogInOutClickHandler(object sender,LogInOutEventArgs e);
 public class LogInOutControl : System.Web.UI.UserControl
 {
  protected System.Web.UI.WebControls.Button ButtonLogIn;
  protected System.Web.UI.WebControls.TextBox TextBoxUserName;
  protected System.Web.UI.WebControls.TextBox TextBoxPassword;
  protected System.Web.UI.WebControls.Button ButtonLogOut;
  protected System.Web.UI.WebControls.Label LabelUser;
  protected System.Web.UI.WebControls.Label LabelPassword;
  public event LogInOutClickHandler LogInOutClick;
  private Language language;
  //方法
  public void ChangeLanguage(Language language)
  {
   this.Lg = language;
  }
  //属性
  public Language Lg
  {
   set
   {
    if(value!=this.language)
    {
     if(value==Language.English)
     {
      this.LabelUser.Text = "User:";
      this.LabelPassword.Text ="Password:";
      this.ButtonLogIn.Text = "LogIn";
      this.ButtonLogOut.Text = "LogOut";
     }
     else
     {
      this.LabelUser.Text = "用户:";
      this.LabelPassword.Text ="密码:";
      this.ButtonLogIn.Text = "登录";
      this.ButtonLogOut.Text = "注销";
     }
    }
   }
  }
  private void Page_Load(object sender, System.EventArgs e)
  {
   if(this.LabelUser.Text=="User:")
     this.language = Language.English;
    else
     this.language = Language.Chinese;
  }
  private void OnLogInOutClick(object sender,LogInOutEventArgs e)
  {
   if(LogInOutClick!=null)
    LogInOutClick(this,e);
  }
  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   InitializeComponent();
   base.OnInit(e);
  }
private void InitializeComponent()
  {
   this.ButtonLogIn.Click += new System.EventHandler(this.ButtonLogIn_Click);
   this.ButtonLogOut.Click += new System.EventHandler(this.ButtonLogOut_Click);
   this.Load += new System.EventHandler(this.Page_Load);
  }
  #endregion
  private void ButtonLogIn_Click(object sender, System.EventArgs e)
  {
   OnLogInOutClick(this,new LogInOutEventArgs(LogInClickType.LongIn,CustomValidate(this.TextBoxUserName.Text,this.TextBoxPassword.Text)));
  }
  private void ButtonLogOut_Click(object sender, System.EventArgs e)
  {
   //注销代码省略
   OnLogInOutClick(this,new LogInOutEventArgs(LogInClickType.LongOut,true));
  }
  //验证函数
  private bool CustomValidate(string userName,string password)
  {
   //验证代码省略,假设通过
   return true;
  }
 }
}


另外一个文件定义了枚举和参数类:
using System;
namespace ZZ
{
 public class LogInOutEventArgs : EventArgs
 {
  private LogInClickType type;
  private bool result;
  
  public LogInOutEventArgs(LogInClickType type,bool result):base()
  {
   this.type = type;
   this.result = result;
  }
  public LogInClickType Type
  {
   get{return this.type;}
  }
  //操作结果,
  public bool Result
  {
   get{return this.result;}
  }
 }
 //操作类型
 public enum LogInClickType : int
 {
  LongIn,
  LongOut
 }
 //定义语言
 public enum Language
 {
  Chinese,
  English
 }
}

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

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

完整代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web

[1] [2]  下一页


[C语言系列]NET 中C#的switch语句的语法  [聊天工具]Gmail推出新功能:Web Clip__天极Yesky
[聊天工具]Web MSN你玩了吗__天极Yesky  [系统软件]托拽Explore中的文件到VB.net的窗口
[系统软件]Web Browser Express 概述  [系统软件]对Internet Explorer Web 控件做一点修改
[系统软件]Boost库在XP+Visual C++.net中的安装  [常用软件]小技巧:三步实现Web迅雷录制PPLive节目
[常用软件]新配色面板:Paint.Net3.0RC1官方下载  [常用软件]天网防火墙:打开WEB和FTP服务
教程录入:mintao    责任编辑:mintao 
  • 上一篇教程:

  • 下一篇教程:
  • 【字体: 】【发表评论】【加入收藏】【告诉好友】【打印此文】【关闭窗口
      注:本站部分文章源于互联网,版权归原作者所有!如有侵权,请原作者与本站联系,本站将立即删除! 本站文章除特别注明外均可转载,但需注明出处! [MinTao学以致用网]
      网友评论:(只显示最新10条。评论内容只代表网友观点,与本站立场无关!)

    同类栏目
    · Web开发  · 网页制作
    · 平面设计  · 网站运营
    · 网站推广  · 搜索优化
    · 建站心得  · 站长故事
    · 互联动态
    更多内容
    热门推荐 更多内容
  • 没有教程
  • 赞助链接
    更多内容
    闵涛博文 更多关于武汉SEO的内容
    500 - 内部服务器错误。

    500 - 内部服务器错误。

    您查找的资源存在问题,因而无法显示。

    | 设为首页 |加入收藏 | 联系站长 | 友情链接 | 版权申明 | 广告服务
    MinTao学以致用网

    Copyright @ 2007-2012 敏韬网(敏而好学,文韬武略--MinTao.Net)(学习笔记) Inc All Rights Reserved.
    闵涛 投放广告、内容合作请Q我! E_mail:admin@mintao.net(欢迎提供学习资源)

    站长:MinTao ICP备案号:鄂ICP备11006601号-18

    闵涛站盟:医药大全-武穴网A打造BCD……
    咸宁网络警察报警平台