我正在尝试基于 CompositeControl 类创建工厂模式。
一切正常。除非我理解它,否则我必须将控件添加回我实际页面上的占位符控件。我四处寻找示例并得到一个几乎可以工作的示例,期望它仅在第一次回发时有效。
我在这里创建了一个示例,我将在这里发布所有代码。
我网页上的代码,这里最重要的是我想在 OnInit 上我试图将控件添加回占位符,我想这可能是我做错了。
using SampleControls;
public partial class Test : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            //Setup First Control
            Session.Clear();
            BaseCompositeControl ltb = new LabelTextBox();
            PlaceHolder1.Controls.Add(ltb);
            Session.Add("Control", ltb);
        }
    }
    protected override void OnInit(EventArgs e)
    {
        base.OnInit(e);
        // Check if the post back and recreate the control
        if (IsPostBack)
        {
            int c = this.Form.Controls.Count;
            for (int i = 0; i < Session.Count; i++)
            {
                if (Session[i].ToString().Contains("Control"))
                {
                    this.PlaceHolder1.Controls.Add((BaseCompositeControl)(Session[i]));
                }
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //Get Postback Date
        BaseCompositeControl oltb = (BaseCompositeControl)this.PlaceHolder1.Controls[0];
        lblPstBck.Text = oltb.Text;
        this.PlaceHolder1.Controls.Clear();
        Session.Clear();
        //Load next Control
        BaseCompositeControl ltb = new LabelCheckBox();
        PlaceHolder1.Controls.Add(ltb);
        Session.Add("Control", ltb);
    }
}
这是复合控件类,我不知道我是否必须在这里做一些事情来处理视图状态或如何处理?
public abstract  class BaseCompositeControl : CompositeControl
{
    protected string _Title;
    public abstract string Text
    {
        get;
        set;
    }
    public string Title
    {
        get { EnsureChildControls(); 
            return _Title; }
        set { EnsureChildControls(); 
            _Title = value; }
    }
    protected override void CreateChildControls()
    {
        // Clears child controls
        Controls.Clear();
        // Build the control tree
        CreateControlHierarchy();
        ClearChildViewState();
    }
    protected abstract void CreateControlHierarchy();
}
文本框控件
public class LabelCheckBox : BaseCompositeControl
{
    protected CheckBox _CheckBox;
    public override string Text
    {
        get
        {
            EnsureChildControls();
            return _CheckBox.Checked.ToString(); ;
        }
        set
        {
            EnsureChildControls();
            _CheckBox.Checked = Convert.ToBoolean(value);
        }
    }
    protected override void CreateControlHierarchy()
    {
        _CheckBox = new CheckBox();
        Label l = new Label();
        // Configure controls
        l.Text = "Second Control";
        // Connect to the parent
        Controls.Add(l);
        Controls.Add(_CheckBox);
    }
}
复选框控件
    public class LabelTextBox : BaseCompositeControl
{
    protected TextBox _Text;
    public override string Text
    {
        get
        {
            EnsureChildControls();
            return _Text.Text;
        }
        set
        {
            EnsureChildControls();
            _Text.Text = value;
        }
    }
    protected override void CreateControlHierarchy()
    {
        Label l = new Label();
        _Text = new TextBox();
        // Configure controls
        l.Text = "First Control";
        // Connect to the parent
        Controls.Add(l);
        Controls.Add(_Text);
    }
}