在Stackoverflow和其他网站上也有类似的问题,但我似乎错过了一些东西。
我有一个绑定到来自数据库的数据表的网格视图。我的目标是使用调用以下方法的同一行中的按钮一次更新一行。
protected void TestsToBeDone_RowUpdating(object sender, GridViewEditEventArgs e)
{
SqlConnection myConnection = getConnection();
myConnection.Open();
int index = Convert.ToInt32(e.NewEditIndex);
GridViewRow selectedRow = TestsToBeDone.Rows[index];
TableCell LABAVIDc = selectedRow.Cells[1];
int LABAVID = Convert.ToInt32(LABAVIDc.Text);
TableCell assistentc = selectedRow.Cells[5];
string query = "UPDATE Labaanvraag " +
"SET Status='4' "+
"WHERE LABAVID ='"+LABAVID+"'";
}
SqlCommand commandZ = new SqlCommand(query, myConnection);
commandZ.ExecuteNonQuery();
myConnection.Close();
SetPatientTestGrid(Session["PATID"], e);
}
它从这里被调用:
<asp:GridView ID="TestsToBeDone" runat="server" EnableModelValidation="True" OnRowEditing="TestsToBeDone_RowUpdating">
<Columns>
<asp:ButtonField ButtonType="Button" CommandName="Update"
HeaderText="Afgenomen" ShowHeader="True" Text="Afgenomen" />
</Columns>
</asp:GridView>
在我第一次尝试时,我有一个OnRowCommand,在后面的代码中带有GridViewCommandEventArgs。
但它仍然抛出相同的异常,尽管我已经在几个网站上读到将其更改为OnRowEditing和GridViewEditEventArgs可以解决问题。
提前感谢,
蒂姆
·GridView 在 ASP.NET 有一些内置命令 -Deleted
、Update
等。正在发生的事情是你的按钮的命令名称为Update
,并且 Gridview 正在劫持你的命令名称并触发一个RowUpdating
事件而不是你的RowEditing
事件。将按钮命令名称更改为Edit
,并使用RowEditing
事件。
如果使用Row_Command
事件进行编辑,则不要将按钮命令名称"Update"用于"更新",将"Delete"用于删除。相反,请分别使用 Edit 或 UP 和 Del。