动态创建的复选框在第二次点击时触发,而不是先点击
Dynamically created CheckBoxs fires on second click instead first
我在 GridView 中动态创建了 CheckBoxes,但是当我点击两次时 CheckedChanged 事件会触发。
我哪里错了?
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// check if it's not a header and footer
if (e.Row.RowType == DataControlRowType.Row)
{
CheckBox chk = new CheckBox();
chk.AutoPostBack = true;
// add checked changed event to checkboxes
chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
e.Row.Cells[1].Controls.Add(chk); // add checkbox to second column
}
}
您必须在 GridView 的 OnRowCreated
和 OnRowDataBound
事件中使用以下代码。
这只会在第一次点击时触发 CheckedChanged
。
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = e.Row.Cells[1].FindControl("chk") as CheckBox;
if (chk == null)
{
chk = new CheckBox();
chk.ID = "CheckBox1";
chk.AutoPostBack = true;
chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
e.Row.Cells[1].Controls.Add(chk);
}
}
我在 GridView 中动态创建了 CheckBoxes,但是当我点击两次时 CheckedChanged 事件会触发。
我哪里错了?
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
// check if it's not a header and footer
if (e.Row.RowType == DataControlRowType.Row)
{
CheckBox chk = new CheckBox();
chk.AutoPostBack = true;
// add checked changed event to checkboxes
chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
e.Row.Cells[1].Controls.Add(chk); // add checkbox to second column
}
}
您必须在 GridView 的 OnRowCreated
和 OnRowDataBound
事件中使用以下代码。
这只会在第一次点击时触发 CheckedChanged
。
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chk = e.Row.Cells[1].FindControl("chk") as CheckBox;
if (chk == null)
{
chk = new CheckBox();
chk.ID = "CheckBox1";
chk.AutoPostBack = true;
chk.CheckedChanged += new EventHandler(chk_CheckedChanged);
e.Row.Cells[1].Controls.Add(chk);
}
}