2012-10-23 81 views
0

我加了删除命令按钮,我的GridView但每当我的小鸡按钮,我得到一个错误是: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"; 
    } 
} 

回答

1

代码没有错,但忘记了处理网格视图的RowDeleting事件。

您需要处理GridView控件RowDeleting方法:

protected void gvLineItems_RowDeleting(object sender, GridViewDeleteEventArgs e)

在这种情况下,你可以重新绑定在这个方法中或离开该方法空,但增加它的签名,以便事件能正确触发。

添加到您的程序:

protected void NameOfYourGridView_RowDeleting(object sender, GridViewDeleteEventArgs e) 
{ 
    //bind your grid view here... 
} 
+0

抱歉,但你的意思是我应该删除 '保护无效gdCourse_RowCommand(对象发件人,GridViewCommandEventArgs E)' 并添加 '保护无效gvLineItems_RowDeleting(对象发件人,GridViewDeleteEventArgs e)' 包括里面的代码? – user1744446

+0

不,你不应该删除任何东西,你需要RowCommand来触发行动的类型,在你的情况是“删除”。你需要RowCommand和RowDeleting。 RowCommand被称为删除,但您需要为删除的行处理RowDeleting。这可以是一个空函数,但编译器需要签名,所以只需添加签名即可。 – JonH

+0

现在大错误的心不是展示,但问题是,它不是删除记录 我应该做点什么标签,,,,我的意思是,指定我的意思是这事件 我应该添加OnRowCommand =“” OnRowDeleting =“ “在GridView标记 – user1744446