问题
I'm developing live-table with WebForms.
I'm adding such control to each TableCell in each TableRow, when I'm adding new rows to the table:
// 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);
But there is a fail with the new EventHandler(buttonViewExpanded_Click) method.
Table renders in the ASPX page pretty well and all controls are visible, but a fail occurs when I try to click that LinkButton.
When I've marked a breakpoint and try to catch the execution of my ASP.NET program in the debugger to make some analyse of program work - the page only refreshed and no one breakpoint in the click event handler has been caught.
Why does it occur? And how to make it work?
回答1:
I cannot test your code; it ties to other classes like InternalApi. However, here is what I found.
You need to figure out the way to load SetTableBodyForSceneMarket in page init.
It basically reload dynamically created controls on post back; otherwise, those controls will be null, and cannot fire events.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
if(IsPostBack)
{
SetTableBodyForSceneMarket();
}
}
private void SetTableBodyForSceneMarket()
{
...
}
来源:https://stackoverflow.com/questions/16652642/lost-dynamically-added-click-events-to-each-tablecell-in-each-tablerow