分享

asp.net (C#) 这样的弹出确认框?

 行走在理想边缘 2020-05-15

楼主这个问题的确和普通的Confirm处理有所不同,但也是可以处理的。建议将你的逻辑分离一下,点击了你的按钮的时候,首先执行之前的代码,并给出提示。
通过JS来判断用户选择的是确定还是取消,如果是选择的确定,再执行后面的代码,这中间我通过一个隐藏的Button1来处理这个工作。应该能达到楼主想要的效果。
下面是一个刚写的可以运行的例子

后端代码

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class UseConfirmSample : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void BT_Submit_Click(object sender, EventArgs e)
    {
        BeforeConfirmExecuteCode();

        string script = @"var b = confirm('是否继续');
            if(b)
                document.getElementById('Button1').click();
            else
                alert('选择的是取消')";

        Page.ClientScript.RegisterStartupScript(this.GetType(), "", script, true);
    }

    /// <summary>
    /// 点击接钮之后,在弹出提示之前需要执行的代码
    /// </summary>
    private void BeforeConfirmExecuteCode()
    {
        Response.Write("这是在弹出确认/取消对话框之前执行的代码");
    }

    /// <summary>
    /// 这是点击确定后继续执行的代码
    /// </summary>
    protected void OnSelectedConfirmExecuteCode()
    {
        Response.Write("你选择了确定,继续执行!");
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        OnSelectedConfirmExecuteCode();
    }
}


前端代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UseConfirmSample.aspx.cs" Inherits="UseConfirmSample" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www./TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www./1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Button ID="BT_Submit" runat="server" OnClick="BT_Submit_Click" Text="首先点击这个按钮" />
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" style="display:none" Text="Button" /></div>
    </form>
</body>
</html>

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多