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

本文关键字:事件 2个 CompositeControl NET ASP | 更新日期: 2023-09-27 18:17:37

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

问题是单击第二个按钮后,页面完成回发,整个页面返回到其初始状态,没有标签显示。所以第一个按钮点击触发,但第二个事件没有。我甚至创建了一个断点来检查,它不访问"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()它没有帮助…也许我做错了什么…

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

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

如果它被创建(通过btn_submit1_Click),你需要在CreateChildControls事件中加载它。

否则,btn_submit2变为null,并且不能触发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);
    }
}