簡體   English   中英

如何將行刪除按鈕連接到GridView_RowDeleted事件?

[英]How can I hook up a row delete button to my GridView_RowDeleted event?

我創建了一個GridView並設置其標題:

<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).

此事件沒有有關發送行的信息。 我想念什么?

我通常會執行以下操作:

<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)
{

}

如果您只關心刪除而不是對網格本身中的值進行內聯編輯,我寧願建議您采用一種簡便的方法。

    <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);

}
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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM