弹出不去发布在asp.net - c#

我有一个叫做sales的按钮,当我单击cancel it backback并插入表单中的值时,它有一个JavaScript弹出窗口,但是当我单击ok时,它不会回发,并且表单中的值也不会进入数据库( JavaScript按钮实际上是打印调用,当单击该按钮时,它会在打印对话框打开时要求打印,它不会回发并且数据库中未插入数据)

这是JavaScript代码

function confirmAction(printable) {
    var r = confirm("You want to Print Invoice?");
    if (r == true) {
        var printContents = document.getElementById(printable).innerHTML;
        var originalContents = document.body.innerHTML;

        document.body.innerHTML = printContents;

        window.print();

        document.body.innerHTML = originalContents;

        __doPostBack();
    }
    else {
        __doPostBack();
    }
}

这是按钮单击的代码

 <asp:Button ID="btnaddsale" runat="server" Text="Sale" OnClick="btnaddsale_Click"  OnClientClick="javascript:confirmAction('printable')"/>

参考方案

好,给您一些注意事项:

无论哪种情况,都需要回发。
您的<asp:Button>将自动以任何一种方式进行回发,因此在这种情况下您无需调用__doPoskBack();
这里的主要问题是,如果要回发,它将在函数退出时立即发生,实际上过早地取消了打印对话框。为了避免这种情况,我们将使用JavaScript技巧来检查document是否具有focus,并且只有当它具有(当用户在浏览器中退出打印对话框时)才会返回并允许回发。

要解决此问题,

首先:在用户取消时设置功能return true;,然后等待focus,然后如果用户要打印,请先return true

function confirmAction(printable) {
    var r = confirm("You want to Print Invoice?");
    if (r == true) {
        var printContents = document.getElementById(printable).innerHTML;
        var originalContents = document.body.innerHTML;

        document.body.innerHTML = printContents;

        window.print();

        document.body.innerHTML = originalContents;

        // Check focus after user exits print dialog and then return true for the postback
        var document_focus = false;
        $(document).focus(function () { document_focus = true; });
        setInterval(function () { if (document_focus === true) { return true; } }, 500);
    }
    else {
        return true;
    }
}

然后,将JavaScript代码更改为在return事件中使用OnClientClick语句:

<asp:Button ID="btnaddsale" runat="server" Text="Sale" 
    OnClick="btnaddsale_Click"  
    OnClientClick="javascript:return confirmAction('printable')"/>

根据评论和您更改的要求进行更新:

这是使脚本在回发后弹出的摘要。因此,您将值插入数据库,然后使用Page.ClientScript.RegisterStartupScript()添加打印脚本/页面加载时确认对话框

注意我不建议将该脚本嵌入到您的C#代码中,因此建议您使用confirmAction()函数并将其(如果尚未放置)放入单独的“ yourScripts.js”文件中,然后调用该函数使用jQuery加载页面时的名称。这是一个例子:

在母版页或页眉中:此文件应包含confirmAction()函数

<script type="text/javascript src="path/to/yourScriptsFile.js">

然后,在代码背后:

protected void Page_Load(object sender, EventArgs e)
{
    // Only display script on PostBack, not initial page load
    if (IsPostBack)
    {
        Page.ClientScript.RegisterStartupScript(
            this.GetType(), 
            "confirmAction", 
            @"<script type=""Text/Javascript"">$(document).ready(function() { confirmAction('printable'); });</script>");
    }
}

还要注意,由于您现在不希望回发,因此confirmAction函数不再应该return true;或使用我上面发布的技巧代码,而只是return false

function confirmAction(printable) {
    var r = confirm("You want to Print Invoice?");
    if (r == true) {
        var printContents = document.getElementById(printable).innerHTML;
        var originalContents = document.body.innerHTML;

        document.body.innerHTML = printContents;

        window.print();

        document.body.innerHTML = originalContents;
    }
    return false;
}

在JavaScript中运行方法C# - javascript

打扰一下,我有这种C#asp方法。受保护的无效btnSave_Click(对象发送者,EventArgs e)有谁知道我该如何发送脚本给您?可以办到?。 javascript大神给出的解决方案 是的,那可以做到。为此,您在.aspx.cs页中创建了函数,然后单击保存按钮上的代码将其复制到函数中,然后执行以下步骤。//Call cs method from J…

调试捆绑和版本化的javascript文件 - javascript

我已经使用Bundleconfig.cs将我的JavaScript文件打包如下:bundles.Add(new ScriptBundle("~/bundles/resultscripts").Include( "~/Scripts/spectrum.js", "~/Scripts/notify.js"…

在ASP.NET WebForms中在服务器端初始化bootsrap datatimepicker - javascript

我有这个HTML<div class='datepicker input-group date' id='datetimepickerStart'> <input type='text' class="form-control" /> <span c…

JavaScript getElementbyId只能运行一次,但不能两次? - c#

我试图获得一个表格单元格,当单击它时它可以变成文本框,以便我们可以填充它,然后将文本添加到该单元格中。有我的JavaScript函数:$(".cellConsigne").click(function () { var numId = this.id.substring(24); document.getElementById("…

Visual Studio,ASP.Net(.Net Framework),并在项目中包含NuGet安装的软件包 - javascript

我在Visual Studio 2017中有一个主要由JavaScript组成的ASP.Net(.Net Framework)MVC Web应用程序。我已经使用NuGet安装了一个软件包(特别是marker-animate-unobtrusive软件包。)文档继续说我应该在我的HTML页面中包含JavaScript,但是在仔细检查了NuGet的内容后,我不确…