丢失动态添加的点击事件到每个表行中的每个表格单元格

时间:2013-05-20 15:14:09

标签: asp.net dynamic data-binding webforms

我正在使用WebForms开发实时表。

当我向表中添加新行时,我正在为每个TableRow中的每个TableCell添加这样的控件:

// in some method, which creates the table

TableRow row = new TableRow();

// here goes other cells with text data

TableCell cellActions = new TableCell();
cellActions.ID = "offerActionsOfferId" + enumerator.Current.offerId;

// init button View More for cell
LinkButton buttonViewExpanded = new LinkButton();
buttonViewExpanded.ID = "buttonViewExpandedOfferId" + enumerator.Current.offerId;
buttonViewExpanded.CssClass = "table-actions-button ic-table-edit";
buttonViewExpanded.Click += new EventHandler(buttonViewExpanded_Click);
buttonViewExpanded.ToolTip = "some alt...";
cellActions.Controls.Add(buttonViewExpanded);
/* end of Action Buttons */

...

row.Cells.Add(cellActions);
this.tableMarket.Controls.Add(row);

但是使用new EventHandler(buttonViewExpanded_Click)方法失败了。

表格在ASPX页面中呈现得非常好,并且所有控件都可见,但是当我尝试单击该LinkBut​​ton时会发生失败。

当我标记了一个断点并尝试在调试器中捕获我的ASP.NET程序的执行以对程序工作进行一些分析时 - 页面只刷新并且单击事件处理程序中没有一个断点被捕获。

为什么会发生?以及如何使它工作?

1 个答案:

答案 0 :(得分:1)

我无法测试你的代码;它与InternalApi等其他类相关联。但是,这是我发现的。

您需要找出在页面初始化中加载 SetTableBodyForSceneMarket 的方法。

它基本上重新加载了回发时动态创建的控件;否则,这些控件将为null,并且无法触发事件。

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);

    if(IsPostBack)
    {
        SetTableBodyForSceneMarket();
    }
}

private void SetTableBodyForSceneMarket()
{
  ...
}
相关问题