为什么我的rowcommand事件处理程序在这里不起作用?

时间:2017-09-07 02:12:47

标签: c# asp.net gridview event-handling buttonfield

我正在尝试处理按下按钮字段中的按钮的事件,然后从网站和数据库中删除该行。我这样做但在我的.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>

1 个答案:

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