0

给定以下 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 事件处理程序触发?

4

2 回答 2

3

您可以使用OnClientClick属性LinkButton而不是添加属性rowdatabound吗?

此外,您似乎还没有在网格视图上实际设置 onrowcommand 属性。

例如:

<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 在链接按钮的回发中起着重要作用,因此OnClick在行数据绑定中添加我会影响回发 javascript。

另请查看这篇文章,以了解一种稍微“更高级”的方法,而不是使用默认的 javascript 确认: http: //mattberseth.com/blog/2007/07/confirm_gridview_deletes_with.html

于 2013-07-08T23:37:34.237 回答
1

尝试将按钮的命令名称更改为“删除”

更新:

代替

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;}"
于 2013-07-08T23:21:01.793 回答