Question

I'm fairly new to asp.net CompositeControls... I have a task which requires me to add a 1 button on load, then when the user clicks it, another button gets added, and when the second button is clicked a label is shown.

The problem is after clicking the 2nd button and the page finishes postback, the whole page returns to its initial state and no label is shown. So the 1st Button click fires but the 2nd event doesn't. I even created a breakpoint to check, it doesn't access "btn_submit2_Click"

test1Composite.cs:

using System;
using System.ComponentModel;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace test1File
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:test1 runat=server></{0}:test1>")]

    public class test1 : CompositeControl 
    {
        protected Button btn_submit1;
        protected Button btn_submit2;

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        protected override void CreateChildControls()
        {
            btn_submit1 = new Button();
            btn_submit1.Text = "Click me!";
            btn_submit1.Click +=new EventHandler(btn_submit1_Click);
            this.Controls.Add(btn_submit1);
            this.ChildControlsCreated = true;
        }
        protected void btn_submit1_Click(object sender, EventArgs e)
        {
            btn_submit2 = new Button();
            btn_submit2.Text = "Click me!";
            btn_submit2.Click += new EventHandler(btn_submit2_Click);
            this.Controls.Add(btn_submit2);
        }
        protected void btn_submit2_Click(object sender, EventArgs e)
        {
            Label lbl_done = new Label();
            lbl_done.Text = "Thank you :)";
            this.Controls.Add(lbl_done);
        }
    }
}

Default.cs:

using System;

namespace test1File
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            test1 pl = new test1();
            form1.Controls.Add(pl);
        }
    }
}

I've searched online and found that "maybe" i need to override OnInit but if i put CreateChildControls() it doesn't help... maybe i'm doing something wrong here.. any help would be appreciated, thank you.

Was it helpful?

Solution

You need to keep track of btn_submit2 whether it is created or not.

If it is created (by btn_submit1_Click), you need to load it back in CreateChildControls event.

Otherwise, btn_submit2 becomes null and it cannot fire the btn_submit2_Click event.

In the following code, it keeps track of the btn_submit2 by using ViewState.

public class test1 : CompositeControl
{
    protected Button btn_submit1;
    protected Button btn_submit2;

    private bool Submit2Created
    {
        get { return (bool) (ViewState["Submit2Created"] ?? false); }
        set { ViewState["Submit2Created"] = value; }
    }

    [Bindable(true)]
    [Category("Appearance")]
    [DefaultValue("")]
    [Localizable(true)]
    protected override void CreateChildControls()
    {
        btn_submit1 = new Button();
        btn_submit1.Text = "Click me1!";
        btn_submit1.Click += btn_submit1_Click;
        this.Controls.Add(btn_submit1);

        // Button 2 is created previously, 
        // so we need to load it back.
        if (Submit2Created)
        {
            AddSubmit2();
        }

        this.ChildControlsCreated = true;
    }

    protected void btn_submit1_Click(object sender, EventArgs e)
    {
        if (!Submit2Created)
        {
            AddSubmit2();
            Submit2Created = true;
        }
    }

    protected void btn_submit2_Click(object sender, EventArgs e)
    {
        Label lbl_done = new Label();
        lbl_done.Text = "Thank you :)";
        this.Controls.Add(lbl_done);
    }

    private void AddSubmit2()
    {
        var btn_submit2 = new Button();
        btn_submit2.Text = "Click me2!";
        btn_submit2.Click += btn_submit2_Click;
        this.Controls.Add(btn_submit2);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top