asp:ButtonField 单击之前的 Javascript [英] Javascript before asp:ButtonField click
问题描述
我在 Asp.net 应用程序中有一个 GridView
控件,它有一个 <asp:buttonField>
的 type="image"
> 和 CommandName="Delete"
.
I have a GridView
control in an Asp.net application, that has a <asp:buttonField>
of type="image"
and CommandName="Delete"
.
有没有办法在到达OnRowDelete
事件之前执行一段javascript?
Is there any way to execute a piece of javascript before reaching the OnRowDelete
event?
在删除该行之前,我只需要一个简单的确认.
I want just a simple confirm before deleting the row.
谢谢!
EDIT:请注意,
标记没有OnClientClick
属性.
EDIT: Please Note that <asp:ButtonField>
tag does not have an OnClientClick
attribute.
推荐答案
我会改用 TemplateField,并根据需要使用常规 asp:Button 或 asp:ImageButton 填充 ItemTemplate.然后,您可以执行 RowCommand 事件在拦截 Delete 命令时将要执行的相同逻辑.
I would use a TemplateField instead, and populate the ItemTemplate with a regular asp:Button or asp:ImageButton, depending one what is needed. You can then execute the same logic that the RowCommand event was going to do when it intercepted the Delete command.
在这两个按钮中的任何一个上,我将使用 OnClientClick 属性在此之前执行 JavaScript 确认对话框.
On either of those buttons I would then use the OnClientClick property to execute the JavaScript confirm dialog prior to this.
<script type="text/javascript">
function confirmDelete()
{
return confirm("Are you sure you want to delete this?");
}
</script>
...
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="DeleteButton" runat="server"
ImageUrl="..." AlternateText="Delete" ToolTip="Delete"
CommandName="Delete" CommandArgument='<%# Eval("ID") %>'
OnClientClick="return confirmDelete();" />
</ItemTemplate>
</asp:TemplateField>
这篇关于asp:ButtonField 单击之前的 Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!