1

I am having trouble attaching a click event onto an image that I have stored within a grid view. Basically it is a delete button that will allow the user to delete a specific row depending on where the button is. I have the code in c# ready for it, however, I cannot seem to attach a click event to it.

This is the markup code of the button

  <asp:TemplateField HeaderText="Remove" ItemStyle-HorizontalAlign="Center">
                        <ItemTemplate>
                            <asp:ImageButton ID="imgbDeleteP" runat="server" BORDER="0" CausesValidation="false" ImageUrl="~/img/Del.png" Height="25px" ImageAlign="Middle"
                               onClick ="gv_Quals_RowCommand" CommandArgument="<%#Container.DataItemIndex%>" CommandName="Remove" />
                        </ItemTemplate>

onClick ="gv_Quals_RowCommand"

Here is the code in c# for the click event

protected void gv_Quals_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if ((e.CommandName == "Remove"))
        {
            int index = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = gv_Quals.Rows[index];
            DataTable dtCurrentTable = (DataTable)Session["CurrentTable"];
            dtCurrentTable.Rows[index].Delete();
            if ((dtCurrentTable.Rows.Count < 0))
            {

            }
            else if ((row.Cells[0].Text != "*New*"))
            {
                int appId = 5000;
                //int appId = 1;
                string insProg = ("delete from projectunitassignment where UnitId =" + int.Parse(row.Cells[0].Text));
                SqlCommand cmd = new SqlCommand(insProg, conn);
                cmd.Connection.Close();
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
                RebindCat(appId);
            }
        }
    }

This is the compilation error that I keep getting

CS0123: No overload for 'gv_Quals_RowCommand' matches delegate 'ImageClickEventHandler'

I cannot set the click event through the properties as it is stored within the grid view so I cannot access it through there. Also the click event does not run as I have tested with debugging

Jack Henry
  • 302
  • 2
  • 20
  • I think that is because of `GridViewCommandEventArgs` which commonly used for `RowCommand` , change it to `EventArgs` – Aria Apr 30 '18 at 11:29

3 Answers3

2

The problem is with GridViewCommandEventArgs should be just EventArgs

public void imgbDeleteP_Click(object sender, EventArgs e)

Edit:

I see that in your code you use the Command Argument, so if you want to use that you should see this post

Basically use onCommand instead of onClick or cast the sender to button to get the command argument, something like:

var argument = ((ImageButton)sender).CommandArgument;
Karthikcbe
  • 295
  • 2
  • 12
0

Did you try to associate the click event for that grid during page load ?

Dilip
  • 474
  • 2
  • 8
  • 18
0

I think that is because of GridViewCommandEventArgs which commonly used for RowCommand , change it to EventArgs, so that event should be something like this:

protected void gv_Quals_RowCommand(object sender, EventArgs e)
{
     ImageButton btn = (ImageButton)sender;
     string cmName= btn.CommandName;
     string cmArgument= btn.CommandArgument;
     if ((cmName == "Remove"))
     {
        ..... 
     }
}

or to get row index:

GridViewRow gvRow = (GridViewRow)(sender as Control).Parent.Parent;
int index = gvRow.RowIndex;

The first parent is the GridView Cell and the second parent of the GridView Cell is the GridView Row.

Aria
  • 3,724
  • 1
  • 20
  • 51