0

我在我的 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";
    }
}  
4

1 回答 1

1

您的代码没有问题,但您忘记处理RowDeleting网格视图的事件。

您需要处理 gridviewRowDeleting方法:

protected void gvLineItems_RowDeleting(object sender, GridViewDeleteEventArgs e)

在这种情况下,您可以在此方法中重新绑定或将方法留空,但添加它的签名,以便事件可以正确触发。

将此添加到您的程序中:

protected void NameOfYourGridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
 {
  //bind your grid view here...
 }
于 2012-10-23T14:57:16.323 回答