今天看到有人问复合控件的问题,就写了一个例子
/// <summary>
/// 通过ControlType属性控制输出的子控件类型,在 CreateChildControls中输出子控件
/// </summary>
[DefaultProperty("ControlType"),
ToolboxData("<{0}:CompoundControl runat=server></{0}:CompoundControl>")]
public class CompoundControl : Control,INamingContainer
{
/// <summary>
/// 字控件类型,支持TextBox和DropDowList
/// </summary>
///
public string ControlType
{
get{return (string) ViewState["ControlType"] ;}
set{ViewState["ControlType"]=value;}
}
protected override void CreateChildControls()
{
base.CreateChildControls ();
Controls.Clear();
if (ControlType=="DropDownList")
{
DropDownList _DropDownList=new DropDownList();
Controls.Add(_DropDownList);
}
else
{
TextBox _TextBox=new TextBox();
Controls.Add(_TextBox);
}
}
}
/// <summary>
/// 通过ControlType属性控制输出的子控件类型,在 CreateChildControls中输出子控件
/// </summary>
[DefaultProperty("ControlType"),
ToolboxData("<{0}:CompoundControl runat=server></{0}:CompoundControl>")]
public class CompoundControl : Control,INamingContainer
{
/// <summary>
/// 字控件类型,支持TextBox和DropDowList
/// </summary>
///
public string ControlType
{
get{return (string) ViewState["ControlType"] ;}
set{ViewState["ControlType"]=value;}
}
protected override void CreateChildControls()
{
base.CreateChildControls ();
Controls.Clear();
if (ControlType=="DropDownList")
{
DropDownList _DropDownList=new DropDownList();
Controls.Add(_DropDownList);
}
else
{
TextBox _TextBox=new TextBox();
Controls.Add(_TextBox);
}
}
}