我在我的 GridView 中添加了一个删除命令按钮,但是每当我点击按钮时,我都会收到一个错误,即:
- [HttpException (0x80004005): GridView 'GridView1' 触发了未处理的事件 RowDeleting。]
- System.Web.UI.WebControls.GridView.OnRowDeleting(GridViewDeleteEventArgs e) +1463321
但我不知道我应该使用什么事件以及删除和编辑什么,这是我的代码:
<asp:TemplateField HeaderText="">
<ItemTemplate>
<asp:Button ID="lkDelte" runat="server" OnClientClick="return confirm('Are you sure you want to delete?')"
CommandName="Delete" CommandArgument='<%# ((GridViewRow) Container).RowIndex %>'></asp:Button>
</ItemTemplate>
</asp:TemplateField>
protected void gdCourse_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = GridView1.Rows[index];
string con_str = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(con_str);
SqlCommand com = new SqlCommand("dbo.delete_documents", con);
com.CommandType = CommandType.StoredProcedure;
SqlParameter pDocument_ID = new SqlParameter("@document_id", row);
com.Parameters.Add(pDocument_ID);
con.Open();
com.ExecuteNonQuery();
con.Close();
}
else
{
Label2.Text = "Unable to delete";
}
}