

Java技术
2005: 03 04 05 06 07 08
09 10 11 12
2006: 01 02
Asp.net
2005: 07 08 09 10 11 12
2006: 01 02

在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> |
| public delegate void LogInOutClickHandler(object sender,LogInOutEventArgs e); |
| public event LogInOutClickHandler LogInOutClick; |
| private Language language; |
| namespace ZZ { using System; using System.Data; using System.Drawing; using System.Web; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; // 定义代理 |
| 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 } } |
| <%@ 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> |
| this.LogInOutControl1.LogInOutClick += new LogInOutClickHandler(LogInOutControl1_LogInOutClick); |
| using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace ZZ |