0
<script type="text/javascript">
    jQuery(document).ready(function () {
        // note: the `Delete` is case sensitive and is the title of the button
        var DeleteButtons = jQuery('#<%= gvShoppingCart.ClientID %> :button[value=Delete]');
       DeleteButtons.each(function () {
           var onclick = jQuery(this).attr('onclick');
           jQuery(this).attr('onclick', null).click(function () {
               if (confirm("Are you sure you want to delete item from shopping cart?")) {
                   return onclick();
               }
               else {
                   return false;
               }
           });
       });
   });
</script>

如果为true(而不是onclick()),我如何更改上面的代码以返回下面的方法

 protected void gvShoppingCart_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //code here
    }

编辑:下面的新代码 -

 <script type="text/javascript">
     jQuery(document).ready(function () {
         $('.deleteStyle').click(function () {
             return confirm("Are you sure you want to delete");
         });
     });
</script>
<style type="text/css">
    .deleteStyle
    {
    }
</style>

网格视图删除:

<asp:CommandField ControlStyle-CssClass="deleteStyle" ShowDeleteButton="True" ButtonType="Button"   />

gvRoles_RowDeleting(注意我现在在 Roles Gridview 上测试它!)

protected void gvRoles_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        BusinessLayer.Roles rls = new BusinessLayer.Roles();
        rls.deleteRole(rls.getRole(gvRoles.DataKeys[e.RowIndex].Value.ToString()));
        this.BindRoles();
    }
4

1 回答 1

1

如果你决定使用 jQuery,这里有一个简单的例子:

示例 1:

请注意,对于此示例,您应该ButtonType="Button"从 CommandField中删除

    <script type="text/javascript">
        jQuery(document).ready(function () {
            $('.deleteStyle').click(function () {
                return confirm("Are you sure you want to delete item from shopping cart?");
            });
        });
    </script>
    <style type="text/css">
        .deleteStyle
        {
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="gvShoppingCart" runat="server" OnRowDeleting="gvShoppingCart_RowDeleting">
            <Columns>
                <asp:CommandField ControlStyle-CssClass="deleteStyle" DeleteText="Delete" ShowDeleteButton="true" />
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>

示例 2:

此示例向您展示如何使用以下方法实现相同的功能ButtonType="Button"

只需将网格视图 ID 更改为所需的值;

<script type="text/javascript">
    jQuery(document).ready(function () {
        jQuery('#<%= gvShoppingCart.ClientID %> :button[value=Delete]').each(function () {
            var onclick = jQuery(this).attr('onclick');
            jQuery(this).attr('onclick', null).click(function () {
                var gridViewName = "gvShoppingCart";
                if (confirm("Are you sure you want to delete item from shopping cart?")) {
                    var deleteItem = onclick.replace("javascript:__doPostBack('gvShoppingCart','", "");
                    deleteItem = deleteItem.replace("')", "");
                    __doPostBack('gvShoppingCart', deleteItem);
                }
                else {
                    return false;
                }
            });
        });
    });
</script>

<asp:CommandField ShowDeleteButton="True" ButtonType="Button" />
于 2013-01-05T12:42:32.693 回答