实施消息框以警告用户有关删除数据的信息

时间:2012-10-09 15:15:34

标签: asp.net

我想删除页面中的某些数据并想要警告用户(显示带有YES / NO的消息框),如果用户点击YES删除数据

是否有可能在ASP中实现MessageBox?若是,如何?

3 个答案:

答案 0 :(得分:1)

<asp:Button ID="Button1" runat="server" Text="Button"
 OnClientClick="return confirm('Are you sure you want delete this?');" />

答案 1 :(得分: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,并通过{{1}找到它如上所示。

e.Row.FindControl

答案 2 :(得分: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