Click EventHandler not firing
本文关键字:firing not EventHandler Click | 更新日期: 2023-09-27 18:19:34
我的gridviewrow中新创建的按钮没有启动其EventHandler或RowCmd事件,然后当按下按钮后重新加载页面时,新添加的行丢失,可能是因为我无法调用BindData方法。我已经从按钮中删除了Click事件处理程序,只使用了OnRowCommand事件,但我仍然没有得到
protected void CustomGridView_DataBound(object sender, EventArgs e)
{
int count = ((GridView)sender).Rows.Count;
GridViewRow row = new GridViewRow(-1, -1, DataControlRowType.DataRow, DataControlRowState.Insert);
TableCell cell = new TableCell();
Button button = new Button();
button.Text = "Insert";
button.ID = "Insert";
button.CommandName = "Insert";
button.Click += new EventHandler(insertButton_Click);
cell.Controls.Add(button);
row.Cells.Add(cell);
for (int i = 0; i < ((GridView)sender).Columns.Count; i++)
{
cell = new TableCell();
cell.Controls.Add(new TextBox { ID = "Text" + i });
row.Cells.Add(cell);
}
Table table = ((GridView)sender).Rows[0].Parent as Table;
table.Rows.AddAt(count + 1, row);
}
protected void insertButton_Click(object sender, EventArgs e)
{
lblInsert.Text = "Hello World";
}
protected void CustomGridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
lblInsert.Text = "Hello World";
}
}
从代码中删除button.Click += new EventHandler(insertButton_Click);
语句。按下按钮时,GridView控件的RowCommand
事件将被触发。有关更多信息-如何:响应GridView控件中的按钮事件。
您需要在数据绑定之前附加到网格的RowCommand事件。在页面或用户控件的构造函数或Init事件上执行,而不是在数据绑定中执行。在数据绑定中,您将设置CommandName和CommandArgument属性。
希望能有所帮助。