Cómo hacer que JavaScript confirme el cuadro de ASP: Buttonfield?
-
11-12-2019 - |
Pregunta
Estoy teniendo dificultades para obtener un cuadro de confirmación de Javascript de ASP: ButtonField:
Este es el código original de la GridView, sin embargo, ButtonField no parece aceptar "ONCLIENTCLICK"
<asp:GridView ID="gvNavios" runat="server" onrowcommand="gvNavios_RowCommand">
<Columns>
<asp:ButtonField runat="server" ButtonType="Button" Text="delete" CommandName="Eliminar" />
</Columns>
</asp:GridView>
Así que probé ASP: LinkButton en su lugar:
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="eliminar" CommandName="delete" runat="server" Text="delete"/>
</ItemTemplate>
</asp:TemplateField>
Sin embargo, de esta manera, no puedo hacer que la fila se hizo clic en e.comMandargument no se llena
El código C # detrás:
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);
}
}
Aprecio uno de los siguientes: Inserte JavaScript en el botón ASP: o obtenga el número de fila de LinkButton.
Solución
Debe agregar el GRANDARGUMGENTE al BETBOTONTON y proporcione algún índice (la identificación del objeto será la mejor).
<asp:LinkButton ID="eliminar" CommandArgument='<%# Eval("ID") %>' CommandName="delete" runat="server" Text="delete"/>
Otros consejos
PORQUE NO USAS UN TEMPLINEFIELF?, CREO ES MAS FACIL
¿Por qué no usas un campo de plantilla? ¿
Dentro del RowDatabound :
Primero cree un campo de plantilla, luego dentro del campo de plantilla Inserte un ImageButton, llame a ITG_BORAR (nombre de comandos).
Luego, en el RowCommand, encuentra, configure el índice (comandargument), establece algo de estilo y la comfirmación de Java:
If e.Row.RowType = DataControlRowType.DataRow Then
Dim boton_borrar As ImageButton = CType(e.Row.Cells(1).FindControl("img_borrar"), ImageButton)
boton_borrar .CommandArgument = e.Row.RowIndex.ToString
boton_borrar .Style("cursor") = "hand"
boton_borrar .Attributes.Add("onClick", "return window.confirm(' ¿Desea borrar este registro? ');")
End If