概念:
所謂復合控件:簡單的理解就是將多個基本的控件組合成一個控件,從而實現自己想要的效果。微軟為ASP.NET2.0中推出的登錄控件等就是一個復合控件。從功能的實現上,復合式控件有點像用戶控件,只是一個是.ascx文件,一個是.dll文件。
呈現簡單的復合控件:
要想呈現一個復合控件,需要了解以下幾個方面:
-->實現INamingContainer接口。
任何實現該接口的控件都創建一個新的命名空間,在這個新的命名空間中,所有子控件 ID 屬性在整個應用程序內保證是唯一的。
-->Control.CreateChildControls 方法。
由 ASP.NET 頁面框架調用,以通知使用基於合成的實現的服務器控件創建它們包含的任何子控件,以便為回發或呈現做准備。 當開發復合服務器控件或模板服務器控件時,必須重寫此方法。重寫 CreateChildControls 方法的控件應實現 INamingContainer 接口以避免命名沖突。
-->Control.ChildControlsCreated 屬性。
獲取一個值,該值指示是否已創建服務器控件的子控件。
-->Control.EnsureChildControls 方法。
確定服務器控件是否包含子控件。如果不包含,則創建子控件。
下面就通過實例來呈現個簡單的復合登陸控件:創建ASP.NET服務器控件工程。complexControl。
先來看代碼:
namespace complexControl
{
[DefaultProperty("Text")]
[ToolboxData("<{0}:LoginControl runat=server ButtonText='登錄' NameLabel='用戶名:' PasswordLabel='用戶密碼:'></{0}:LoginControl>")]
public class LoginControl : WebControl, INamingContainer
{
private Button _button;
private TextBox _nameTextBox;
private Label _nameLabel;
private TextBox _passwordTextBox;
private Label _passwordLabel;
private RequiredFieldValidator _nameValidator;
private RequiredFieldValidator _passwordValidator;
[Bindable(true),Category("Appearance"),DefaultValue(""),Description("按鈕文本")]
public string ButtonText
{
get
{
EnsureChildControls();//確定服務器控件是否包含子控件
return _button.Text;
}
set
{
EnsureChildControls();
_button.Text = value;
}
}
[Bindable(true),Category("Default"),DefaultValue(""),Description("姓名")]
public string Name
{
get
{
EnsureChildControls();
return _nameTextBox.Text;
}
set
{
EnsureChildControls();
_nameTextBox.Text = value;
}
}
[Bindable(true),Category("Appearance"),DefaultValue(""),Description("必須輸入姓名")]
public string NameErrorMessage
{
get
{
EnsureChildControls();
return _nameValidator.ErrorMessage;
}
set
{
EnsureChildControls();
_nameValidator.ErrorMessage = value;
_nameValidator.ToolTip = value;
}
}
[Bindable(true),Category("Apperance"),DefaultValue(""),Description("姓名標簽")]
public string NameLabel
{
get
{
EnsureChildControls();
return _nameLabel.Text;
}
set
{
EnsureChildControls();
_nameLabel.Text = value;
}
}
[Browsable(false),DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public string Password
{
get
{
EnsureChildControls();
return _passwordTextBox.Text;
}
}
[Bindable(true),Category("Appearance"),DefaultValue(""),Description("必須輸入密碼")]
public string PasswordErrorMessage
{
get
{
EnsureChildControls();
return _passwordValidator.ErrorMessage;
}
set
{
EnsureChildControls();
_passwordValidator.ErrorMessage = value;
_passwordValidator.ToolTip = value;
}
}
[Bindable(true),Category("Appearance"),DefaultValue(""),Description("密碼標簽")]
public string PasswordLabel
{
get
{
EnsureChildControls();
return _passwordLabel.Text;
}
set
{
EnsureChildControls();
_passwordLabel.Text = value;
}
}
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.Static;
_passwordLabel = new Label();
_passwordTextBox = new TextBox();
_passwordTextBox.TextMode = TextBoxMode.Password;
_passwordTextBox.ID = "passwordTextBox";
_passwordValidator = new RequiredFieldValidator();
_passwordValidator.ID = "validator2";
_passwordValidator.ControlToValidate = _passwordTextBox.ID;
_passwordValidator.Text = "*";
_passwordValidator.Display = ValidatorDisplay.Static;
_button = new Button();
_button.ID = "button1";
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);
}
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(); // Td
writer.RenderBeginTag(HtmlTextWriterTag.Td);
_nameTextBox.RenderControl(writer);
writer.RenderEndTag(); // Td
writer.RenderBeginTag(HtmlTextWriterTag.Td);
_nameValidator.RenderControl(writer);
writer.RenderEndTag(); // Td
writer.RenderEndTag(); // Tr
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.RenderBeginTag(HtmlTextWriterTag.Td);
_passwordLabel.RenderControl(writer);
writer.RenderEndTag(); // Td
writer.RenderBeginTag(HtmlTextWriterTag.Td);
_passwordTextBox.RenderControl(writer);
writer.RenderEndTag(); // Td
writer.RenderBeginTag(HtmlTextWriterTag.Td);
_passwordValidator.RenderControl(writer);
writer.RenderEndTag(); // Td
writer.RenderEndTag(); // Tr
writer.RenderBeginTag(HtmlTextWriterTag.Tr);
writer.AddAttribute(HtmlTextWriterAttribute.Colspan, "2");
writer.AddAttribute(HtmlTextWriterAttribute.Align, "right");
writer.RenderBeginTag(HtmlTextWriterTag.Td);
_button.RenderControl(writer);
writer.RenderEndTag(); // Td
writer.RenderBeginTag(HtmlTextWriterTag.Td);
writer.Write(" ");
writer.RenderEndTag(); // Td
writer.RenderEndTag(); // Tr
writer.RenderEndTag(); // Table
}
}
}