2012-01-16 94 views
0

我正在創建一個包含幾個按鈕的表格。這些按鈕連接到觸發更新特定數據庫項目的方法的事件。由於某種原因,這個事件並不合適。應該執行的方法根本不被執行。我在做什麼錯以編程方式掛鉤事件不會觸發?

僞代碼:

public void createTable(List<BLL> itemlist) 
{ 
    //newtable; 

    foreach (BLL item in itemlist) 
    { 
    //newrow; 
    //create multiple cells... 

    TableCell cell = new TableCell(); 
    Button button = new Button(); 
    button.ID = "buttonname" + counter.ToString(); 
    button.Text = "Update"; 
    button.Click += new System.EventHandler(this.UpdateButton_Click); 
    cell.Controls.Add(button); 

    //addCellToTableRow 
    } 
    //addRowToTable 
} 

public void UpdateButton_Click(object sender, EventArgs e) 
{ 
    //logic to get sender and update database. 
    //debugger doesn't get to the breakpoint here. 
} 
+0

你在調用page_load中的createTable方法嗎? – Dave 2012-01-16 09:54:59

回答

4

您需要在ASP.NET page life cycle讀了。

當您創建動態控件時,您需要在每個回帖中重新創建它們 - 最好在OnInit事件處理程序中完成。

如果你不這樣做,對象和任何附加事件都不存在,所以事件不會觸發它們。

+0

謝謝你的回答。我知道關於頁面生命週期的一些事情,但在創建動態控件時沒有考慮它。我永遠不會忘記它! – 2012-01-16 10:14:54

相關問題