Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 从添加到回发占位符的复合控件中获取数据_Asp.net_Postback_Placeholder_Abstract Factory_Composite Controls - Fatal编程技术网

Asp.net 从添加到回发占位符的复合控件中获取数据

Asp.net 从添加到回发占位符的复合控件中获取数据,asp.net,postback,placeholder,abstract-factory,composite-controls,Asp.net,Postback,Placeholder,Abstract Factory,Composite Controls,我试图创建一个基于CompositeControl类的工厂模式 一切正常。除非据我所知,我必须将该控件添加回实际页面上的占位符控件。我到处寻找例子,找到了一个几乎可以工作的例子,希望它只在第一次回发时起作用 我在这里创建了一个示例,我将在这里发布所有代码 我的网页上的代码,这里最重要的是我想在OnInit上,我试图将控件添加回占位符,我想这可能是我做错的地方 using SampleControls; public partial class Test : System.Web.UI.Page

我试图创建一个基于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 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);
    }
}
复选框控件

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

我很久以前问过这个问题

但我认为你应该这样做。对用户控件使用factory模式