给出以下GridView代码:
<asp:GridView ID="gvReq" runat="server" DataSourceID="objdsReq" >
<Columns>
<asp:TemplateField HeaderText="Control">
<ItemTemplate>
<asp:LinkButton ID="lbdelete" runat="server" CommandArgument='<%# Container.DataItemIndex %>' ForeColor="Red" CommandName="DeleteReq">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:ObjectDataSource ID="objdsReq" runat="server" SelectMethod="GetDataTable" >
<%-- parameter list --%>
</asp:ObjectDataSource>
在RowDataBound事件中,添加了JavaScript代码:
Protected Sub gvReq_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvReq.RowDataBound
If (e.Row.RowType = DataControlRowType.DataRow) Then
Dim lbdelete As LinkButton = e.Row.Cells(DELETE_CELL).Controls.Item(1)
lbdelete.Attributes.Add("onclick", "javascript:if(confirm('Are you sure you want to delete?')){return true}else{return false}")
JavaScript会触发,但RowCommand事件永远不会触发 - 我猜是因为它只是由JavaScript处理:
Protected Sub gvReq_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs) Handles gvReq.RowCommand
Dim dataItemIndex As Integer = Convert.ToInt32(e.CommandArgument)
Dim reqID As Integer = Convert.ToInt32(gvReq.DataKeys(dataItemIndex).Values(0))
If e.CommandName = "DeleteReq" Then
管理层要求将JavaScript确认对话框放在那里。
现在,如果有人点击OK
到JavaScript确认框,我该如何触发RowCommand事件处理程序?
答案 0 :(得分:3)
您可以使用OnClientClick
的{{1}}属性,而不是在LinkButton
添加属性吗?
此外,您似乎还没有在网格视图上实际设置onrowcommand属性。
E.g:
rowdatabound
我相信javascript在回发链接按钮中扮演了重要角色,因此在行数据绑定中添加<asp:GridView ID="gvReq" runat="server" DataSourceID="objdsReq" OnRowCommand="gvReq_RowCommand">
<Columns>
<asp:TemplateField HeaderText="Control">
<ItemTemplate>
<asp:LinkButton ID="lbdelete" runat="server"
CommandArgument='<%# Container.DataItemIndex %>'
ForeColor="Red"
CommandName="DeleteReq"
OnClientClick="return confirm('Are you sure you want to delete?');"
>Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
会影响回发javascript。
另外,请查看这篇文章,了解一下这种方法,而不是使用默认的javascript确认:http://mattberseth.com/blog/2007/07/confirm_gridview_deletes_with.html
答案 1 :(得分:1)
尝试将Button的CommandName更改为“Delete”
更新:
而不是
lbdelete.Attributes.Add("onclick", "javascript:if(confirm('Are you sure you want to delete?')){return true}else{return false}")
试
lbdelete.OnClientClick = "javascript:if(confirm('Are you sure you want to delete?')){return true;}else{return false;}"