0

我正在尝试处理按下按钮字段中的按钮并随后从网站和数据库中删除该行的事件。我这样做,但在我的 .aspx.cs 文件中使用以下代码:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            System.Diagnostics.Debug.WriteLine("testing buttons");
        }
    } 

但是,每当我单击按钮字段中的按钮时,它都会引发错误,指出该事件未处理。下面的代码是整个数据表的aspx。

<asp:GridView ID="gridViewStudent" runat="server" CellPadding="4" ForeColor="#333333"
      emptydatatext="There are no data to display">
      <AlternatingRowStyle BackColor="White" />
      <Columns>
          <asp:ButtonField ButtonType="Button" CommandName="Delete" HeaderText="Edit" ShowHeader="True" Text="Remove" />
      </Columns>
      <EditRowStyle BackColor="#2461BF" />
      <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
      <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
      <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
      <RowStyle BackColor="#EFF3FB" />
      <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
      <SortedAscendingCellStyle BackColor="#F5F7FB" />
      <SortedAscendingHeaderStyle BackColor="#6D95E1" />
      <SortedDescendingCellStyle BackColor="#E9EBEF" />
      <SortedDescendingHeaderStyle BackColor="#4870BE" />



</asp:GridView>
4

1 回答 1

0

据我所知,您并没有将所有必需的事件处理程序声明为GridView. 为相应的操作使用适当的事件处理程序和事件处理程序方法:

页面标记 (ASPX)

<asp:GridView ID="gridViewStudent" runat="server" ...
      OnRowCommand="gridViewStudent_RowCommand"
      OnRowDeleting="gridViewStudent_RowDeleting">

</asp:GridView>

隐藏代码 (ASPX.CS)

// Row command event
protected void gridViewStudent_RowCommand(object sender, GridViewCommandEventArgs e)
{
    // do something
}

// Row deleting event
protected void gridViewStudent_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    // do something
}
于 2017-09-07T02:52:16.833 回答