2011-12-09 46 views
0

我創建了一個GridView,並設置其標題:如何將行刪除按鈕連接到我的GridView_RowDeleted事件?

<asp:GridView ID="ProductsGridView" 
    DataSourceID="ProductsDataSource" 
    AllowPaging="True" 
    AutoGenerateColumns="False" 
    runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" 
    AutoGenerateDeleteButton="True" 
    AutoGenerateEditButton="True" 
    OnRowCancelingEdit="GridView1_RowCancelingEdit" 
    OnRowUpdating="GridView1_RowUpdating" 
    OnRowEditing="GridView1_RowEditing" 
    OnRowDeleted="ProductsGridView_RowDeleted" 
    onselectedindexchanged="ProductsGridView_SelectedIndexChanged"> 

我可以看到在網格中的刪除按鈕,但是當我上雙擊按鈕時,Visual Studio帶我到:

protected void ProductsGridView_SelectedIndexChanged(object sender, EventArgs e) 

代替:

protected void ProductsGridView_RowDeleted(object sender, GridViewDeletedEventArgs e). 

這個事件沒有對發送行信息。我錯過了什麼?

+0

我想你要走了一些代碼從你上面的例子說的是造成SelectedIndexChanged事件火災前/而不是刪除,然後刪除事件。上面的代碼應該按照規定工作。 –

+0

這是我想了解 - 它一直髮送給我 – aralele

回答

0

我通常做到以下幾點:

<asp:GridView ID="ProductsGridView" 
    DataSourceID="ProductsDataSource" 
    AllowPaging="True" 
    AutoGenerateColumns="False" 
    runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" 
    AutoGenerateDeleteButton="True" 
    AutoGenerateEditButton="True" 
    OnRowCancelingEdit="GridView1_RowCancelingEdit" 
    OnRowUpdating="GridView1_RowUpdating" 
    OnRowEditing="GridView1_RowEditing" 
    onrowdeleting="GridView1_RowDeleting" 

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 
{ 

} 
0

我寧願建議你一個簡單的方法,如果您擔心刪除而已,沒有價值觀的內聯編輯在網格本身。

<asp:GridView ID="ProductsGridView" DataSourceID="ProductsDataSource" 
       AllowPaging="True" AutoGenerateColumns="False" 
       runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" 
       AutoGenerateEditButton="True" OnRowCommand="Gridview1_RowCommand"> 
    <columns> 
     <table> 
      <tr> 
      <td> col1val1 </td> 
      <td> col1val1 </td> 
      <td> col1val1 </td> 
      <td> col1val1 </td> 
      <td> col1val1 </td> 
      <td> <asp:Button Id="btnDelete" CommandName="DeleteRow" 
        CommandArgument='<%#Eval("PrimaryKeyFromTheDataSource") %>' 
       Text="Delete" Tooltip="DeleteCurrentRow" 
       onclientclick='return confirm("Are you certain to delete?");'/> 
      </td> 

      </tr> 
     </table> 
    <columns> 
</asp:Gridview>` 

C# - 行命令事件

int i = Convert.ToInt32(e.CommandArgument); 
if(e.commandname.equals("deleterow")) 
{ 
    DeleteItemById(i); 

} 
0
Protected void CustomersGridView_RowDeleting(Object sender, GridViewDeleteEventArgs e) 
{ 
    TableCell cell = CustomersGridView.Rows[e.RowIndex].Cells[2]; 
    if (cell.Text == "Beaver") 
    { 
     e.Cancel = true; 
     Message.Text = "You cannot delete customer Beaver."; 
    } 
    else 
    { 
     Message.Text = ""; 
    } 
} 
<asp:GridView ID="CustomersGridView" runat="server" 
    DataSourceID="CustomersSqlDataSource" 
    AutoGenerateColumns="False" 
    AutoGenerateDeleteButton="True" 
    OnRowDeleting="CustomersGridView_RowDeleting" 
    DataKeyNames="CustomerID,AddressID"> 
    <Columns> 
     <asp:BoundField DataField="FirstName" 
      HeaderText="FirstName" SortExpression="FirstName" /> 
     <asp:BoundField DataField="LastName" HeaderText="LastName" 
      SortExpression="LastName" /> 
     <asp:BoundField DataField="City" HeaderText="City" 
      SortExpression="City" /> 
     <asp:BoundField DataField="StateProvince" HeaderText="State" 
      SortExpression="StateProvince" /> 
    </Columns> 
</asp:GridView> 
+0

謝謝,我可以看到你在這裏dpoing,但仍然有這個問題 - 因爲引發事件(在你的代碼)是SelectedIndexChanged 而不是「CustomersGridView_RowDeleting 「事件。 艾米的想法? – aralele

+0

void ItemsGridView_SelectedIndexChanged(Object sender,EventArgs e) { \t GridViewRow row = ItemsGridView.SelectedRow; \t Message.Text =「您選擇了」+ row.Cells [2] .Text +「。」; } –

+0

你可以用此刪除: '>刪除 後面 保護無效lbtndelete_click(對象發件人,EventArgs的){ INT ID = Convert.toint32(((LinkBut​​ton的)發送器).CommandArgument)頁上; } –

相關問題