javascriptasp.netbuttonfield

How to make javascript confirm box from asp:buttonfield?


I'm having a hard time getting a javascript confirm box from asp:buttonfield:

This is the original code of the Gridview, however buttonfield does not seem to accept "onClientClick"

<asp:GridView ID="gvNavios" runat="server"  onrowcommand="gvNavios_RowCommand">
<Columns>
    <asp:ButtonField runat="server" ButtonType="Button" Text="delete" CommandName="Eliminar" />
</Columns>
</asp:GridView>

So I tried asp:Linkbutton instead:

<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
     <asp:LinkButton ID="eliminar" CommandName="delete" runat="server" Text="delete"/>
</ItemTemplate>
</asp:TemplateField>

However this way I cannot get which row was clicked as e.commandargument is not filled

The C# code-behind:

 protected void gvNavios_RowCommand(object sender, GridViewCommandEventArgs e)
{

    string currentCommand = e.CommandName;
    int currentRowIndex = Int32.Parse(e.CommandArgument.ToString());
    string imo = gvNavios.Rows[currentRowIndex].Cells[3].Text;

    if (currentCommand.Equals("delete"))
    {
        eliminarNavio(imo);
        Response.Redirect(Request.RawUrl);
    }

}

I appreciate one of the following: Insert javascript in asp:button, or get Row number from linkbutton.


Solution

  • You need to add the CommandArgument to the LinkButton and provide some index (the object ID will be the best).

    <asp:LinkButton ID="eliminar" CommandArgument='<%# Eval("ID") %>' CommandName="delete" runat="server" Text="delete"/>