0

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>
Sohee
  • 23
  • 5
  • In codehind it's `GridView1_RowCommand` then at the aspx the GridView called `ID="gridViewStudent"`? Plus there is no onrowcommand=`GridView1_RowCommand` on it. – Circle Hsiao Sep 07 '17 at 02:21
  • Change your event name to `gridViewStudent_RowCommand` and add proper event handler on the control: `OnRowCommand="gridViewStudent_RowCommand"`. – Tetsuya Yamamoto Sep 07 '17 at 02:26
  • It says that OnRowCommand is not a valid attribute of element ButtonField – Sohee Sep 07 '17 at 02:31
  • For what reason that you're adding `OnRowCommand` to `ButtonField`? Try to place the handler at parent `GridView` instead. – Tetsuya Yamamoto Sep 07 '17 at 02:33
  • Share us the exception message threw when you click. – Circle Hsiao Sep 07 '17 at 02:38
  • After all of the changes it still goes into an error and says that the event is not being handled. – Sohee Sep 07 '17 at 02:38
  • System.Web.HttpException: The GridView 'gridViewStudent' fired event RowDeleting which wasn't handled. – Sohee Sep 07 '17 at 02:38
  • 1
    @Sohee Try adding `OnRowDeleting="gridViewStudent_RowDeleting"` event handler to your `GridView` and ensure this added to code behind: `protected void gridViewStudent_RowDeleting(Object sender, GridViewDeleteEventArgs e)`. – Tetsuya Yamamoto Sep 07 '17 at 02:41
  • Possible duplicate of [The GridView 'PendingRecordsGridview' fired event RowDeleting which wasn't handled](https://stackoverflow.com/questions/14301815/the-gridview-pendingrecordsgridview-fired-event-rowdeleting-which-wasnt-handl) – Circle Hsiao Sep 07 '17 at 02:47
  • thank you for the help, it will now go into the event successfully – Sohee Sep 07 '17 at 02:50

1 Answers1

0

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
}
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61