0

ページ内の一部のデータを削除し、ユーザーに警告し(YES / NOのメッセージボックスを表示)、ユーザーが[YES]をクリックするとデータを削除したい

ASPでMessageBoxを実装することは可能ですか?はいの場合、どのようにですか?

4

3 に答える 3

1
<asp:Button ID="Button1" runat="server" Text="Button"
 OnClientClick="return confirm('Are you sure you want delete this?');" />
于 2012-10-09T15:22:18.233 に答える
1

それはconfirm次のように簡単です:

<input type="submit" value="delete" onclick="javascript:confirm('Are you sure?')"/>

ASP.NETの場合、サーバー側でこれを行うことができます。

btnDelete.Attributes.Add("onclick", "return confirm('Are you sure?')");

または、マークアップについて:

<asp:Button ID="btnDelete" runat="server" Text="Delete" OnClientClick="return confirm('Are you sure?');" />

編集: を使用して、サーバー側のコードで次GridViewのようなことを行うことができます。

public partial class _Default : System.Web.UI.Page {

    Dictionary<string, string> collection = null;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            collection = new Dictionary<string, string>();
            collection.Add("Microwave", "$299");
            collection.Add("Coffee maker", "$59");
            collection.Add("Arm chair", "$89");
        }
        GridView1.DataSource = collection;
        GridView1.DataBind();
    }

    protected void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
    {
        if (DataControlRowType.DataRow == e.Row.RowType)
        {
            ((LinkButton)e.Row.FindControl("lnkDelete"))
                .Attributes.Add("onclick", "return confirm('are you sure?')");

        }
    }

    protected void GridView1_RowCommand(Object sender, GridViewCommandEventArgs e) {
        if (e.CommandName.ToLower() == "delete")
        {
            // this code should be executed only when the user clicks "ok"
            // in the confirm message that appears on the browser

            // your implementation goes here
        }
    }
}

また、マークアップは次のように行うことができます。テンプレート列については、Visual Studioでコマンド列を作成してからテンプレート列に変換するのが簡単なので、実際にリンク削除ボタン(またはボタン)のIDを取得して、e.Row.FindControl上記のように見つけることができます。

<asp:GridView ID="GridView1" runat="server" OnRowCommand="GridView1_RowCommand"
    OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="Key" HeaderText="Product" />
        <asp:BoundField DataField="Value" HeaderText="$$$" />
        <asp:TemplateField ShowHeader="False">
            <ItemTemplate>
                <asp:LinkButton ID="lnkDelete" runat="server" CausesValidation="False" 
                    CommandName="Delete" Text="Remove"></asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
于 2012-10-09T15:25:26.207 に答える
0

jQueryを使用してそれを行うことができます!

<!doctype html>

<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>jQuery UI Dialog - Modal confirmation</title>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.9.0/themes/base/jquery-ui.css" />
    <script src="http://code.jquery.com/jquery-1.8.2.js"></script>

    <script src="http://code.jquery.com/ui/1.9.0/jquery-ui.js"></script>
        <script>
    $(function() {
        $( "#dialog-confirm" ).dialog({
            resizable: false,
            height:140,
            modal: true,
            buttons: {
                "Delete all items": function() {
                  alert('deleted');
                  $( this ).dialog( "close" );
                },
                Cancel: function() {
                  alert('cancel')
                    $( this ).dialog( "close" );
                }
            }
        });
    });
    </script>
</head>
<body>

<div id="dialog-confirm" title="Empty the recycle bin?">
    <p><span class="ui-icon ui-icon-alert" style="float: left; margin: 0 7px 20px 0;"></span>These items will be permanently deleted and cannot be recovered. Are you sure?</p>
</div>


</body>
</html>

参照: http: //jqueryui.com/dialog/#modal-confirmation

于 2012-10-09T15:26:59.460 に答える