ASP.NET CompositeControl:在回发后触发2个(或更多)事件

时间:2013-06-07 15:43:53

标签: c# asp.net composite-controls

我对asp.net CompositeControls相当新...我有一个任务需要我在加载时添加一个按钮,然后当用户点击它时,另一个按钮被添加,当点击第二个按钮时显示标签。

问题是在点击第二个按钮并且页面完成回发后,整个页面返回到其初始状态,并且没有显示标签。因此,第一个按钮点击会触发,但第二个事件不会触发。我甚至创建了一个断点来检查,它不访问“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);
        }
    }
}

我在网上搜索并发现“也许”我需要覆盖OnInit,但如果我把CreateChildControls()它没有帮助......也许我在这里做错了...任何帮助将不胜感激,谢谢。

1 个答案:

答案 0 :(得分:1)

您需要跟踪 btn_submit2 是否已创建。

如果已创建(通过 btn_submit1_Click ),则需要将其加载回 CreateChildControls 事件。

否则, btn_submit2 变为空,无法触发 btn_submit2_Click 事件。

在以下代码中,它使用 ViewState 跟踪 btn_submit2

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