}
#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;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace ZZ
{
public class Default : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label LabelMsg;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected LogInOutControl LogInOutControl1;
private void Page_Load(object sender, System.EventArgs e)
{
//注册用户控件事件
this.LogInOutControl1.LogInOutClick = new LogInOutClickHandler(LogInOutControl1_LogInOutClick);
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.DropDownList1.SelectedIndexChanged = new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.Load = new System.EventHandler(this.Page_Load);
}
#endregion
private void LogInOutControl1_LogInOutClick(object sender, LogInOutEventArgs e)
{
switch(e.Type)
{
case LogInClickType.LongIn:
this.LabelMsg.Text = "你点击了登录按钮,操作结果:" e.Result.ToString();
break;
case LogInClickType.LongOut:
this.LabelMsg.Text = "你点击了注销按钮,操作结果:" e.Result.ToString();
break;
}
}
private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
this.LogInOutControl1.Lg = (Language)this.DropDownList1.SelectedIndex;
//this.LogInOutControl1.ChangeLanguage((Language)this.DropDownList1.SelectedIndex);
}
}
}
当用户在前台通过选择下拉框列表来改变控件的语言,这里通过Lg属性来完成,不过这里也加了一个方法ChangeLanguage也可以实现同样的功能。另外,通过点击登陆或注销按钮触发LogInOutClick事件来给页面中的LabelMsg.Text属性赋值从而得到操作结果。
总结,用户控件为程序员带来了很高的开发效率和重用性,更是在性能方面有了很大的提高,以前称为Asp ,其实我认为Asp.net跟Asp没有什么直接联系。而且我想做应用程序的朋友和我一样在开发Web程序时更喜欢采用代码分离方式,这样结构更清晰,便与修改和管理。同Asp程序相比,他是编译型的,引入了面向对象的设计思想,也就不可避免的带来了他的复杂性,要想开发高水准的Asp.net程序,对于模式的设计,层次结构的划分,这里还是比较讲究的。总之,他更像是在编Windows窗体程序,而不是在写VB脚本。
文章整理:西部数码--专业提供域名注册、虚拟主机服务
http://www.west263.com
以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!




