【发布时间】:2018-09-23 04:50:22
【问题描述】:
我在与我希望按钮交互的 aspx 页面后面的代码不同的类中创建了一个确认按钮。
基本上,假设我有这个 Confirmation 类:
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);
}
}
上面的代码是代码的一个非常释义的版本。但是会循环生成多个按钮并添加到表格中。该表显示在下面提到的 Default.aspx 页面上。对于表中的每一行,CommandName 属性的值包含不同的值。
我正在使用的 aspx 页面是 Web Forms .NET Web 应用程序中的默认页面。
我希望在单击这些按钮之一时触发的事件被带回到 Default.aspx 页面 (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;
}
我假设它在尝试更改面板的可见性时抛出 NullReferenceException,因为我创建了 _Default 类的新实例,以便我可以在第一个代码 sn-p 的最后一行中设置 EventHandler。
但我不知道如何让它工作。
【问题讨论】:
-
不是创建
_Default类的新实例,您不能将_Default 类实例作为GenerateButtons方法的引用传递吗?