简体   繁体   English

将click事件处理程序添加到在另一个类中动态创建的按钮

[英]Add click event handler to a button that was dynamically created in another class

I have a confirmation button being created in a different class to the code behind of the aspx page I want the button interacting with. 我有一个确认按钮正在与要与按钮进行交互的aspx页面后面的代码不同的类中创建。

Basically, say I have this Confirmation class: 基本上,说我有这个确认类:

public class Confirmation
{
    public void GenerateButtons()
    {
        Button btnConfirm = new Button();

        btnConfirm.Text = "Confirm";
        btnConfirm.CommandName = "Variable1,Variable2,Variable3";

        _Default def = new _Default();
        btnConfirm.Click += new EventHandler(def.btnConfirmBook_Click);
    }
}

The code above is a very paraphrased version of the code. 上面的代码是该代码的非常解释性的版本。 But multiple buttons are generated in a loop and added to a table. 但是,会在循环中生成多个按钮并将其添加到表中。 The table is displayed on the Default.aspx page mentioned below. 该表显示在下面提到的Default.aspx页上。 For each row in the table, the value of the CommandName property contains different values. 对于表中的每一行, CommandName属性的值包含不同的值。

The aspx page I am working with is the Default page in a Web Forms .NET Web App. 我正在使用的aspx页面是Web Forms .NET Web App中的“默认”页面。

I want the event triggered when one of these buttons are clicked to be carried over back into the code behind of the Default.aspx page (Default.aspx.cs). 我希望单击这些按钮之一时触发事件,以将其转回Default.aspx页面(Default.aspx.cs)后面的代码中。

This is what I have in the Default.aspx.cs: 这就是Default.aspx.cs中的内容:

public void btnConfirm_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;

    DisplayConfirmation(btn.CommandName);
}

protected void DisplayConfirmation(string result)
{
    // I split result and manipulate it as necessary to get a confirmationText string

    pnlMainPanel.Visible = false; // This is where it throws NullReferenceException
    pnlConfirmationPanel.Visible = true;

    lblConfirmationText.Text = confirmationText;
}

I assume it's throwing the NullReferenceException when trying to change the visibility of the panels because I created a new instance of the _Default class so that I could set the EventHandler in the last line of the first code snippet. 我假定在尝试更改面板的可见性时会引发NullReferenceException,因为我创建了_Default类的新实例,以便可以在第一个代码段的最后一行中设置EventHandler。

But I don't know how to get this working. 但是我不知道该如何工作。

You have assumed correct. 您假设正确。 Don't create a new instance of _Default class. 不要创建_Default类的新实例。

ASPX: ASPX:

<form id="form1" runat="server">
<div>
    <asp:Panel ID="Panel1" runat="server">
        make me invisible;
    </asp:Panel>
</div>
</form>

Code Behind: 背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
    GenerateButtons();
}

public void GenerateButtons()
{
    AnotherClass anotherClass = new AnotherClass(this);
}

public void btnConfirmBook_Click(object sender, EventArgs e)
{
    Button btn = sender as Button;
    DisplayConfirmation();
}

protected void DisplayConfirmation()
{
    Panel1.Visible = false;
}

Another Class: 另一类:

public class AnotherClass
{
    public AnotherClass(Default def)
    {

        Button btnConfirm = new Button();

        btnConfirm.Text = "Confirm";
        btnConfirm.CommandName = "Variable1,Variable2,Variable3";
        def.Form.Controls.Add(btnConfirm);

        btnConfirm.Click += new EventHandler(def.btnConfirmBook_Click);

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM