- ·上一篇文章:在asp.net中为Web用户控件添加属性和事件(3)
- ·下一篇文章:在asp.net中为Web用户控件添加属性和事件(1)
在asp.net中为Web用户控件添加属性和事件(2)
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>
做人要厚道,请注明转自chinazhan中国站长(www.ChinaZhan.com)。
{
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>
做人要厚道,请注明转自chinazhan中国站长(www.ChinaZhan.com)。
