Why does my rowcommand event handler not work here?

468 views Asked by At

I am trying to handle the event in which a button in my button field is pressed and subsequently removes the row from the website and from the database. and I do that but using the following code in my .aspx.cs file:

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

However, whenever I go to click a button in the button field, it throws the error saying that the event is not handled. The following code is the aspx for the entire data table.

<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

There are 1 answers

0
Tetsuya Yamamoto On BEST ANSWER

From what I see, you're not declaring all required event handlers to the GridView. Use proper event handlers and event handler methods for respective actions:

Page Markup (ASPX)

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

</asp:GridView>

Code behind (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
}