N久前写的一个基于1.1的ASP.NET登录复合控件
代码如下:
using System;
using System.ComponentModel; //大多和可视化相关
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Complex
{
/*
*Login是本控件的默认事件,[DefaultEvent("Login")]
如果在设计器上双击此控件,将会自动生成Login的事件响应
*/
[DefaultEvent("Login")]
public class LoginControl :
System.Web.UI.WebControls.WebControl,
System.Web.UI.INamingContainer
//实现System.Web.UI.INamingContainer接口
//此接口无任何成员,只要控件类实现了
//此接口,就具备,控件ID + 冒号 + 内部控件ID
//作为客户端HTML标记name属性,而控件ID + 下划线
//+ 内部控件ID作为客户端HTML标记id属性的功能
{
private TextBox txtUserName;
private TextBox txtPassword;
private Button btnLogin;
public LoginControl()
{
//确保控件被拖放到ASP.NET设计器的时候,
//马上创建子控件
this.EnsureChildControls();
}
protected override void CreateChildControls()
{
this.Controls.Clear();
this.txtUserName = new TextBox();
this.txtUserName.ID = "TxtUserName";
this.txtUserName.Width = Unit.Parse("100px");
this.txtPassword = new TextBox();
this.txtPassword.ID = "TxtPassword";
this.txtPassword.TextMode = TextBoxMode.Password;
this.txtPassword.Width = Unit.Parse("100px");
this.btnLogin = new Button();
this.btnLogin.ID = "BtnLogin";
this.btnLogin.Text = "登陆";
this.Controls.Add(new LiteralControl("<table><tr><td>用户名</td><td>"));
this.Controls.Add(this.txtUserName);
this.Controls.Add(new LiteralControl("</td></tr><tr><td>密码</td><td>"));
this.Controls.Add(this.txtPassword);
this.Controls.Add(new LiteralControl("</td></tr><tr><td colspan=\"2\" align=\"right\">"));
this.Controls.Add(this.btnLogin);
this.Controls.Add(new LiteralControl("</td></tr></table>"));
this.ChildControlsCreated = true;
}
[
Category("登陆"), //设置属性窗口(按类别察看)中,属性的类别
Description("获取或设置用户名") //设置属性窗口下方对该属性的文本提示
]
public virtual string UserName
{
get
{
return this.txtUserName.Text;
}
set
{
this.txtUserName.Text = value;
}
}
[
Category("登陆"), //设置属性窗口(按类别察看)中,属性的类别
Description("获取或设置密码") //设置属性窗口下方对该属性的文本提示
]
public virtual string Passord
{
get
{
return this.txtPassword.Text;
}
set
{
this.txtPassword.Text = value;
}
}
protected override bool OnBubbleEvent(object source, EventArgs args)
{
if(source == this.btnLogin)
{
EventHandler handler =
(EventHandler)this.Events[keyLogin];
if(handler != null)
handler(this, EventArgs.Empty);
return true;
}
return base.OnBubbleEvent (source, args);
}
private readonly static object keyLogin = new object();
[
Category("登陆"), //设置事件窗口(按类别察看)中,属性的类别
Description("当用户点击登陆按钮的时候触发") //设置事件窗口下方对该属性的文本提示
]
public event EventHandler Login
{
add
{
this.Events.AddHandler(keyLogin, value);
}
remove
{
this.Events.RemoveHandler(keyLogin, value);
}
}
}
}
using System;
using System.ComponentModel; //大多和可视化相关
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Complex
{
/*
*Login是本控件的默认事件,[DefaultEvent("Login")]
如果在设计器上双击此控件,将会自动生成Login的事件响应
*/
[DefaultEvent("Login")]
public class LoginControl :
System.Web.UI.WebControls.WebControl,
System.Web.UI.INamingContainer
//实现System.Web.UI.INamingContainer接口
//此接口无任何成员,只要控件类实现了
//此接口,就具备,控件ID + 冒号 + 内部控件ID
//作为客户端HTML标记name属性,而控件ID + 下划线
//+ 内部控件ID作为客户端HTML标记id属性的功能
{
private TextBox txtUserName;
private TextBox txtPassword;
private Button btnLogin;
public LoginControl()
{
//确保控件被拖放到ASP.NET设计器的时候,
//马上创建子控件
this.EnsureChildControls();
}
protected override void CreateChildControls()
{
this.Controls.Clear();
this.txtUserName = new TextBox();
this.txtUserName.ID = "TxtUserName";
this.txtUserName.Width = Unit.Parse("100px");
this.txtPassword = new TextBox();
this.txtPassword.ID = "TxtPassword";
this.txtPassword.TextMode = TextBoxMode.Password;
this.txtPassword.Width = Unit.Parse("100px");
this.btnLogin = new Button();
this.btnLogin.ID = "BtnLogin";
this.btnLogin.Text = "登陆";
this.Controls.Add(new LiteralControl("<table><tr><td>用户名</td><td>"));
this.Controls.Add(this.txtUserName);
this.Controls.Add(new LiteralControl("</td></tr><tr><td>密码</td><td>"));
this.Controls.Add(this.txtPassword);
this.Controls.Add(new LiteralControl("</td></tr><tr><td colspan=\"2\" align=\"right\">"));
this.Controls.Add(this.btnLogin);
this.Controls.Add(new LiteralControl("</td></tr></table>"));
this.ChildControlsCreated = true;
}
[
Category("登陆"), //设置属性窗口(按类别察看)中,属性的类别
Description("获取或设置用户名") //设置属性窗口下方对该属性的文本提示
]
public virtual string UserName
{
get
{
return this.txtUserName.Text;
}
set
{
this.txtUserName.Text = value;
}
}
[
Category("登陆"), //设置属性窗口(按类别察看)中,属性的类别
Description("获取或设置密码") //设置属性窗口下方对该属性的文本提示
]
public virtual string Passord
{
get
{
return this.txtPassword.Text;
}
set
{
this.txtPassword.Text = value;
}
}
protected override bool OnBubbleEvent(object source, EventArgs args)
{
if(source == this.btnLogin)
{
EventHandler handler =
(EventHandler)this.Events[keyLogin];
if(handler != null)
handler(this, EventArgs.Empty);
return true;
}
return base.OnBubbleEvent (source, args);
}
private readonly static object keyLogin = new object();
[
Category("登陆"), //设置事件窗口(按类别察看)中,属性的类别
Description("当用户点击登陆按钮的时候触发") //设置事件窗口下方对该属性的文本提示
]
public event EventHandler Login
{
add
{
this.Events.AddHandler(keyLogin, value);
}
remove
{
this.Events.RemoveHandler(keyLogin, value);
}
}
}
}