如何将行删除按钮连接到GridView RowUpdated事件?

时间:2011-12-09 23:12:13

标签: c# asp.net

我创建了一个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).

此事件没有关于发送行的信息。我错过了什么?

3 个答案:

答案 0 :(得分: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)
{

}

答案 1 :(得分: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);

}

答案 2 :(得分: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>