2012-10-23 147 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