using System;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebFrame
{
[
DefaultEvent("Login"),
DefaultProperty("Name")
]
public class CompositeLogin : WebControl, INamingContainer
{
private Button _button;
private TextBox _nameTextBox;
private Label _nameLabel;
private TextBox _passwordTextBox;
private Label _passwordLabel;
private RequiredFieldValidator _nameValidator;
private RequiredFieldValidator _passwordValidator;
private static readonly object EventLogin = new object();
#region 重載System.Web.UI.Control類的Controls屬性
//獲取ControlCollection對象,該對象表示UI層次結構中指定的服務器控的子控件
public override ControlCollection Controls
{
get
{
//確定服務器控件是否包含子控件,如果不包含,則創建子控件
EnsureChildControls();
return base.Controls;
}
}
#endregion
public string ButtonText
{
get
{
EnsureChildControls();
return _button.Text;
}
set
{
EnsureChildControls();
_button.Text = value;
}
}
public string Name
{
get
{
EnsureChildControls();
return _nameTextBox.Text;
}
set
{
EnsureChildControls();
_nameTextBox.Text = value;
}
}
public string NameErrorMessage
{
get
{
EnsureChildControls();
return _nameValidator.ErrorMessage;
}
set
{
EnsureChildControls();
_nameValidator.ErrorMessage = value;
_nameValidator.ToolTip = value;
}
}
public string NameLabel
{
get
{
EnsureChildControls();
return _nameLabel.Text;
}
set
{
EnsureChildControls();
_nameLabel.Text = value;
}
}
public string PassWord
{
get
{
EnsureChildControls();
return _passwordTextBox.Text;
}
}
public string PassWordErrorMessage
{
get
{
EnsureChildControls();
return _passwordValidator.ErrorMessage;
}
set
{
EnsureChildControls();
_passwordValidator.ErrorMessage = value;
_passwordValidator.ToolTip = value;
}
}
public string PassWordLabel
{
get
{
EnsureChildControls();
return _passwordLabel.Text;
}
set
{
EnsureChildControls();
_passwordLabel.Text = value;
}
}
public event EventHandler Login
{
add
{
Events.AddHandler(EventLogin,value);
}
remove
{
Events.AddHandler(EventLogin,value);
}
}
protected virtual void OnLogin(EventArgs e)
{
EventHandler loginHandler = (EventHandler)Events[EventLogin];
if(loginHandler!=null)
loginHandler(this,e);
}
protected override bool OnBubbleEvent(object source, EventArgs e)
{
bool handled = false;
if(e is CommandEventArgs)
{
CommandEventArgs ce = (CommandEventArgs)e;
if(ce.CommandName =="Login")
{
OnLogin(EventArgs.Empty);
handled = true;
}
}
return handled;
}
#region 動態創建子控件
protected override void CreateChildControls()
{
Controls.Clear();
_nameLabel = new Label();
_nameTextBox = new TextBox();
_nameTextBox.ID = "nameTextBox";
_nameValidator = new RequiredFieldValidator();
_nameValidator.ID = "validator1";
_nameValidator.ControlToValidate = _nameTextBox.ID;
_nameValidator.Text = "*";
_nameValidator.Display = ValidatorDisplay.Dynamic;
_passwordLabel = new Label();
_passwordTextBox = new TextBox();
_passwordTextBox.ID = "passwordTextBox";
_passwordValidator = new RequiredFieldValidator();
_passwordValidator.ID = "validator2";
_passwordValidator.ControlToValidate = _passwordTextBox.ID;
_passwordValidator.Text = "*";
_passwordValidator.Display = ValidatorDisplay.Dynamic;
_button = new Button();
_button.ID = "button1";
_button.CommandName = "Login";
this.Controls.Add(_nameLabel);
this.Controls.Add(_nameTextBox);
this.Controls.Add(_nameValidator);
this.Controls.Add(_passwordLabel);
this.Controls.Add(_passwordTextBox);
this.Controls.Add(_passwordValidator);
this.Controls.Add(_button);
}
#endregion
#region 控件輸出
protected override void Render(HtmlTextWriter writer)
{
//將需要呈現的Html屬性與樣式添加到指定的System.Web.UI.HtmlTextWriter中
AddAttributesToRender(writer);
writer.AddAttribute(HtmlTextWriterAttribute.Cellpadding,"1",false);
writer.RenderBeginTag(HtmlTextWriterTag.Table);
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
_nameLabel.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
_nameTextBox.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
_nameValidator.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
_passwordLabel.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
_passwordTextBox.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
_passwordValidator.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.AddAttribute(HtmlTextWriterAttribute.Colspan,"3");
writer.AddAttribute(HtmlTextWriterAttribute.Align,"right");
writer.RenderBeginTag(HtmlTextWriterTag.Td);
_button.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
}
#endregion
}
}
posted on 2004-10-21 08:04 冷风.net 阅读(122) 评论(4) 编辑 收藏
评论
# re: 服务器控件之组合控件(用户登录)完成于2004-10-21
.aspx文件
<%@ Register TagPrefix="LoginOn" Namespace="WebFrame" Assembly="WebFrame"%>
<LoginOn:CompositeLogin id="CompositeLogin1" runat="server" ButtonText="提交" NameLabel="用戶:" PassWordLabel="密碼:"
BackColor="Silver" BorderColr="Gray" BorderWidth="1px" NameErrorMessage="請輸入用戶名" PassWordErrorMessage="請輸入密碼" />
2004-10-21 08:05 | 冷风.net
# re: 服务器控件之组合控件(用户登录)完成于2004-10-21
.cs文件
this.CompositeLogin1.Login += new EventHandler(CompositeLogin1_Login);
private void CompositeLogin1_Login(object sender, System.EventArgs e)
{
Response.Write("User:" + CompositeLogin1.Name + "<br>");
Response.Write("PassWord:" + CompositeLogin1.PassWord);
}
2004-10-21 08:06 | 冷风.net
# re: 服务器控件之组合控件(用户登录)完成于2004-10-21
其继承自接口INamingContainer是必不可少的
INamingContainer为子控件提供了一个新的命名范围,保证子控件的ID唯一性
2004-10-21 08:06 | 冷风.net
# re: 服务器控件之组合控件(用户登录)完成于2004-10-21
不错。昨天写组合控件,遇上了点小麻烦,找好好久没找到,今天早上总算让我找到一个了。原来我每次都少了EnsureChildControls()函数。所以一测试老出错。。鼓励一下。。