我已经返回一个 Javascript 函数,它向用户询问确认消息。 JavaScript 函数是
function ConfirmOnDelete() {
if (confirm("Are you sure to delete?"))
return true;
else
return false;
GridView 如下所示:
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
这里我想在用户单击 GridView 命令字段中的删除时调用 JavaScript 函数。 这个怎么称呼?
最佳答案
假设您想继续使用 CommandField
,您可以使用 GridView 的 OnRowDataBound
事件以编程方式完成此操作。
在 GridView
声明中指定 RowDataBound 事件的事件处理程序:
<asp:GridView ID="gv" runat="server" OnRowDataBound="gv_RowDataBound"....
然后在您的事件处理程序(代码隐藏)中找到您的按钮(这里我假设是 ImageButton
,但这取决于您的 中的
) 并将 JavaScript 添加到其 ButtonType
属性CommandFieldOnClientClick
属性。
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
((ImageButton)e.Row.Cells[cell].Controls[ctrl]).OnClientClick = "return confirm('Are you sure you want to delete?');"; // add any JS you want here
}
}
在上面的示例中,cell 指的是 CommandField
的列索引,ctrl 指的是其中的控件索引(删除按钮)您引用的单元格。
关于c# - 如何在 GridView 命令字段中调用 JavaScript 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14397309/