在90年代初,Microsoft为Web程序员提供的 Active Server Pages(ASP)革命性地改变了Web的编程。它可以利用十分易用的模型在Web服务器上动态生成HTML,并且很容易的实现了对数据库的访问,就当时来说,这是一项多么吸引人的技术,包括现在Internet上的许多web站点都是用Asp写的。随着技术发展,Microsoft提出了第二代编程模型――Web窗体。Web窗体作为Asp.net的一部分,编程模型是基于事件的,使用他更像是在进行Windows窗体编程。
下面就让我们建立一个简单登录用户控件来做演示。
先来看看用户控件的前台代码(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; // 定义代理 |
另外一个文件定义了枚举和参数类:
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; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace ZZ |
当用户在前台通过选择下拉框列表来改变控件的语言,这里通过Lg属性来完成,不过这里也加了一个方法ChangeLanguage也可以实现同样的功能。另外,通过点击登陆或注销按钮触发LogInOutClick事件来给页面中的LabelMsg.Text属性赋值从而得到操作结果。
总结,用户控件为程序员带来了很高的开发效率和重用性。同Asp程序相比,他是编译型的,引入了面向对象的设计思想,也就不可避免的带来了他的复杂性,要想开发高水准的Asp.net程序,对于模式的设计,层次结构的划分,这里还是比较讲究的。