GridView中的动态按钮 – Button.Click事件未触发

我有一个绑定到AccessDataSource的GridView控件。 选择一行后,我在所选行中创建一个表。 我正在为这个表添加按钮。 他们的Click事件永远不会被解雇。 我读到了重新创建按钮和东西,但仍然没有运气解决问题。 感谢帮助!

的.aspx:

       

的.cs:

 public partial class AdminSurvey : System.Web.UI.Page { private const string ID_BUTTON_SUBMIT = "SUBMIT_BUTTON"; private const string ID_BUTTON_DELETE = "SUBMIT_DELETE"; private string _selected_survey; protected void SurveyGridView_RowCreated(Object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null && !IsPostBack) { string survey = ((DataRowView)e.Row.DataItem).Row.ItemArray[0].ToString(); if (survey.Equals(_selected_survey)) { e.Row.Cells[0].Controls.Clear(); // create table e.Row.Cells[0].Controls.Add(createSurveyTable(((DataRowView)e.Row.DataItem).Row.ItemArray[0].ToString(), ((DataRowView)e.Row.DataItem).Row.ItemArray[1].ToString(), ((DataRowView)e.Row.DataItem).Row.ItemArray[2].ToString())); ViewState["row_index"] = Convert.ToString(e.Row.RowIndex); ViewState["survey_id"] = ((DataRowView)e.Row.DataItem).Row.ItemArray[0].ToString(); ViewState["title"] = ((DataRowView)e.Row.DataItem).Row.ItemArray[1].ToString(); ViewState["description"] = ((DataRowView)e.Row.DataItem).Row.ItemArray[2].ToString(); } } else if(e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null && IsPostBack) { string row_idx = (string)ViewState["row_index"]; if (row_idx != null && e.Row.RowIndex.Equals(Convert.ToInt32(row_idx))) { _selected_survey = (string)ViewState["survey_id"]; string title = (string)ViewState["title"]; string description = (string)ViewState["description"]; e.Row.Cells[0].Controls.Clear(); // recreate table e.Row.Cells[0].Controls.Add(createSurveyTable(_selected_survey, title, description)); } } } private Table createSurveyTable(string survey_id, string title, string description) { Table table = new Table(); TableRow row = new TableRow(); TableCell cell = new TableCell(); Table questions_table = createQuestionsTable(survey_id); cell.Controls.Add(questions_table); row.Cells.Add(cell); table.Rows.Add(row); return table; } private Table createQuestionsTable(string survey_id) { // submit row TableRow submit_row = new TableRow(); TableCell submit_cell = new TableCell(); submit_button = new Button(); submit_button.Text = "Submit"; submit_button.ID = ID_BUTTON_SUBMIT; submit_button.Click += new EventHandler(submitButton_Click); submit_cell.Controls.Add(submit_button); delete_button = new Button(); delete_button.Text = "Delete"; delete_button.ID = ID_BUTTON_DELETE; delete_button.Click += new EventHandler(deleteButton_Click); submit_cell.Controls.Add(delete_button); submit_row.Cells.Add(submit_cell); table.Rows.Add(submit_row); return table; } private void submitButton_Click(object sender, EventArgs e) { } private void deleteButton_Click(object sender, EventArgs e) } } // class 

我不想回答我的问题,但我希望在查看问题时节省任何时间。 事实certificate,错误发生在if条件中:

 if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null && !IsPostBack) { // ... } else if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null && IsPostBack) { // ... } 

e.Row.DataItem != null错误地出现在两个条件中,而第二个条件, IsPostBack等于true,从未执行过。

正确的代码是:

 if (e.Row.RowType == DataControlRowType.DataRow && e.Row.DataItem != null && !IsPostBack) { // ... } else if (e.Row.RowType == DataControlRowType.DataRow && IsPostBack) { // ... } 

尝试分配命令名称,并附加到网格的RowCommand事件以侦听按钮的单击,以替代点击click事件。