GridView”触发了未处理的事件RowUpdating。 asp.net背后的C#代码
Stackoverflow和其他网站上也有类似的问题,但我似乎错过了一些东西。
我有一个GridView绑定到DataTable,它来自数据库。 我的目标是使用一个按钮来更新当前的一行,该按钮位于调用以下方法的同一行中。
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); }
它是从这里调用的:
在我第一次尝试时,我在后面的代码中使用了GridViewCommandEventArgs的OnRowCommand。
但它仍然会抛出相同的exception,虽然我已经阅读了几个网站,将其更改为OnRowEditing和GridViewEditEventArgs将解决问题。
提前致谢,
蒂姆A.
ASP.NET中的GridView有一些内置命令 – Deleted
, Update
等。发生的事情是你的按钮的命令名为Update
,而Gridview正在劫持你并发出RowUpdating
事件而不是你的RowEditing
事件。 将按钮命令名称更改为Edit
,并使用RowEditing
事件。
如果使用Row_Command
事件进行编辑,则不要使用按钮命令名称“Update”进行更新,使用“Delete”进行删除。 而是分别使用Edit或UP和Del。