如何从asp:buttonfield制作javascript确认框?
-
11-12-2019 - |
题
我很难从ASP中获取JavaScript确认框:Buttonfield:
这是GridView的原始代码,但是Buttonfield似乎没有接受“OnClientClick”
<asp:GridView ID="gvNavios" runat="server" onrowcommand="gvNavios_RowCommand">
<Columns>
<asp:ButtonField runat="server" ButtonType="Button" Text="delete" CommandName="Eliminar" />
</Columns>
</asp:GridView>
.
所以我尝试了asp:linkbutton:
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:LinkButton ID="eliminar" CommandName="delete" runat="server" Text="delete"/>
</ItemTemplate>
</asp:TemplateField>
.
但是,这种方式我无法获得哪一行被点击,因为没有填充e.commandargument
c#代码背后:
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);
}
.
}
我很欣赏以下之一:在ASP中插入JavaScript:按钮,或从LinkButton获取行号。
解决方案
需要将Commandargument添加到LinkButton并提供一些索引(对象ID将是最好的)。
<asp:LinkButton ID="eliminar" CommandArgument='<%# Eval("ID") %>' CommandName="delete" runat="server" Text="delete"/>
. 其他提示
Porque没有USAS UN Templatefield?,Creo ES MAS Facile
为什么不使用templatefield?¿
rowdatabound :
首先创建一个模板,然后在templatefield内插入一个图像布顿,调用它img_borrar(commandname)。
然后在RowCommand中,找到它,设置索引(Commandargument),设置一些样式,java comfimation:
.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