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 に答える