public abstract class CompositeControl : WebControl, INamingContainer, ICompositeControlDesignerAccessor
using System;
using System.ComponentModel;
using System.Drawing;
using System.Security.Permissions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace WebControlLibrary{
[
DefaultProperty("ButtonText"),
ToolboxData("<{0}:Register runat=\"server\"> </{0}:Register>"),
]
public class Register : CompositeControl {
// 定义私有字段
private Button submitButton;
private TextBox nameTextBox;
private Label nameLabel;
private TextBox emailTextBox;
private Label emailLabel;
private RequiredFieldValidator emailValidator;
private RequiredFieldValidator nameValidator;
// 实现属性ButtonText
[
Bindable(true),
Category("Appearance"),
DefaultValue(""),
Description("按钮上的文字内容.")
]
public string ButtonText {
get {
EnsureChildControls();
return submitButton.Text;
}
set {
EnsureChildControls();
submitButton.Text = value;
}
}
// 实现属性Name
[
Bindable(true),
Category("Default"),
DefaultValue(""),
Description("用户名.")
]
public string Name {
get {
EnsureChildControls();
return nameTextBox.Text;
}
set {
EnsureChildControls();
nameTextBox.Text = value;
}
}
// 实现属性NameErrorMessage
[
Bindable(true),
Category("Appearance"),
DefaultValue(""),
Description("用户名验证错误信息.")
]
public string NameErrorMessage {
get {
EnsureChildControls();
return nameValidator.ErrorMessage;
}
set {
EnsureChildControls();
nameValidator.ErrorMessage = value;
nameValidator.ToolTip = value;
}
}
// 实现属性NameLabelText
[
Bindable(true),
Category("Appearance"),
DefaultValue(""),
Description("用户名文本框旁的文字.")
]
public string NameLabelText {
get {
EnsureChildControls();
return nameLabel.Text;
}
set {
EnsureChildControls();
nameLabel.Text = value;
}
}
// 实现属性Email
[
Bindable(true),
Category("Default"),
DefaultValue(""),
Description("邮件地址.")
]
public string Email {
get {
EnsureChildControls();
return emailTextBox.Text;
}
set {
EnsureChildControls();
emailTextBox.Text = value;
}
}
// 实现属性EmailErrorMessage
[
Bindable(true),
Category("Appearance"),
DefaultValue(""),
Description("邮件地址验证错误信息.")
]
public string EmailErrorMessage {
get {
EnsureChildControls();
return emailValidator.ErrorMessage;
}
set {
EnsureChildControls();
emailValidator.ErrorMessage = value;
emailValidator.ToolTip = value;
}
}
// 实现属性EmailLabelText
[
Bindable(true),
Category("Appearance"),
DefaultValue(""),
Description("电子邮件文本框旁的文字.")
]
public string EmailLabelText {
get {
EnsureChildControls();
return emailLabel.Text;
}
set {
EnsureChildControls();
emailLabel.Text = value;
}
}
// 重写ICompositeControlDesignerAccessor接口的RecreateChildContrls方法
protected override void RecreateChildControls() {
EnsureChildControls();
}
// 重写Control基类的CreateChildControls方法
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 = NameErrorMessage;
nameValidator.Display = ValidatorDisplay.Static;
emailLabel = new Label();
emailTextBox = new TextBox();
emailTextBox.ID = "emailTextBox";
emailValidator = new RequiredFieldValidator();
emailValidator.ID = "validator2";
emailValidator.ControlToValidate = emailTextBox.ID;
emailValidator.Text = EmailErrorMessage;
emailValidator.Display = ValidatorDisplay.Static;
submitButton = new Button();
submitButton.ID = "button1";
this.Controls.Add(nameLabel);
this.Controls.Add(nameTextBox);
this.Controls.Add(nameValidator);
this.Controls.Add(emailLabel);
this.Controls.Add(emailTextBox);
this.Controls.Add(emailValidator);
this.Controls.Add(submitButton);
}
// 重写Render方法
protected override void Render(HtmlTextWriter writer) {
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);
emailLabel.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
emailTextBox.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
emailValidator.RenderControl(writer);
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2", false);
writer.AddAttribute(HtmlTextWriterAttribute.Align, "right", false);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
submitButton.RenderControl(writer);
writer.RenderEndTag();
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(" ");
writer.RenderEndTag();
writer.RenderEndTag();
writer.RenderEndTag();
}
}
}
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register TagPrefix="Sample" Assembly="WebControlLibrary" Namespace="WebControlLibrary" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">
<html xmlns="" >
<head runat="server">
<title>实现复合控件呈现</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<Sample:Register id="demo1" runat="server" ButtonText="注册" EmailLabelText="电子邮件" NameLabelText="用户名" EmailErrorMessage="不能为空" NameErrorMessage="不能为空" />
</div>
</form>
</body>
</html>