Getting data back from composite control added to placeholder on postback

209 views Asked by At

I'm trying to create a factory pattern based on the CompositeControl class.

Everything is working fine. Except as I understand it I have to add the control back onto the placeholder control on my actual page. I've searched around for examples and got one to almost work expect it only worked on the first postback.

I've created a sample here and I'll post all the code here.

Code on my webpage, the most important here is I guess on OnInit where I'm trying to add the control back onto the placeholder, I guess this is what I might be doing wrong.

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);

    }
}

This is composite control classes, I don't know if I have to do something here to handle the viewstate or how?

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();
}

TextBox control

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);
    }
}

Checkbox control

    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);
    }
}
1

There are 1 answers

0
cr1t On BEST ANSWER

I asked this a long time ago,

But I think this is how you should do it. Using a factory pattern for user controls.

http://weblogs.asp.net/sfeldman/archive/2007/12/17/factory-pattern-for-user-controls.aspx