在C#函数之前等待弹出响应

Wait popup response before C# function

本文关键字:响应 等待 函数      更新时间:2023-09-26

我试图用弹出窗口调用一个JavaScript函数,如果用户点击"Ok",它就会调用一个C#函数。但页面总是在我加载JavaScript函数的同时PostBack。

我的HTML ASP:按钮:

<asp:Button ID="PrchBtn" runat="server" class="PrchBtn" Text="<%$ Resources:Resource, WebEDI_Save %>" OnClick="PrchBtn_Click" OnClientClick = "Confirm();" />

OnClientClick,它调用这个JS函数:

function Confirm() {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function (e) {
        if (e) {
            confirm_value.value = "Yes";
        } else {
            confirm_value.value = "No";
        }
        document.forms[0].appendChild(confirm_value);
    });
}

然后我的C#函数:

public void PrchBtn_Click(object sender, EventArgs e)
    {
        //Code here...
    }

它使用一个简单的"确认"对话框。但我想自定义弹出窗口,这就是我使用"Alertify"库的原因。

谢谢你的帮助。

更新(参见注释#3):通过以下链接(从JavaScript(而不是AJAX!)调用代码隐藏函数)这是我的实际代码:

<asp:Button ID="PrchBtn" runat="server" class="PrchBtn" Text="<%$ Resources:Resource, WebEDI_Save %>" OnClientClick="Confirm(); return false;" />
<asp:Button runat="server" ID="PrchBtnHidden" ClientIDMode="Static" OnClick="PrchBtn_Click" style="display:none;" />
function Confirm() {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function (e) {
        if (e) {
            confirm_value.value = "Yes";
            document.forms[0].appendChild(confirm_value);
            __doPostBack('<%= PrchBtnHidden.UniqueID %>');
        } else {
            confirm_value.value = "No";
            document.forms[0].appendChild(confirm_value);
            __doPostBack('<%= PrchBtnHidden.UniqueID %>');
        }
    });
}

但问题是一样的,JS和C#同时在做一些事情。

更新(奖励错误):我不知道为什么,但我的警觉性被窃听了。提示:

alertify.prompt("Message", function (e, str) {
        // str is the input text
        if (e) {
            Console.Log("Ok");
        } else {
            Console.Log("No");
        }
    }, "Default Value");

当我点击"确定"或"否"时,没有任何东西启动。提示的TextBox内容为:

function(e,str){//str是输入文本if(e){Console.Log("Ok");}else{Console.Log("No");}}

还有一个警报。确认

alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function (e) {
        if (e) {
            confirm_value.value = "Yes";
            document.forms[0].appendChild(confirm_value);
            __doPostBack('<%= PrchBtnHidden.UniqueID %>');
        } else {
            confirm_value.value = "No";
            document.forms[0].appendChild(confirm_value);
            __doPostBack('<%= PrchBtnHidden.UniqueID %>');
        }
    });

只有"Ok"在开火。取消按钮没有任何作用。

解决方案:再次获得警觉性。js(http://alertifyjs.com/)这是我的JS函数:

alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function () {
        confirm_value.value = "Yes";
        document.forms[0].appendChild(confirm_value);
        __doPostBack('<%= PrchBtnHidden.UniqueID %>');
    },
      function () {
          confirm_value.value = "No";
          document.forms[0].appendChild(confirm_value);
          __doPostBack('<%= PrchBtnHidden.UniqueID %>');
      }).set('labels', { ok: 'Ok', cancel: 'No' }); 

它起作用了!

解决方案: 创建2个HTML按钮,一个可见链接到JavaScript函数,另一个不可见链接到C#方法:

<asp:Button ID="PrchBtn" runat="server" class="PrchBtn" Text="<%$ Resources:Resource, WebEDI_Save %>" OnClientClick="Confirm(); return false;" />
<asp:Button runat="server" ID="PrchBtnHidden" ClientIDMode="Static" OnClick="PrchBtn_Click" style="display:none;" />

JS:

function Confirm() {
    var confirm_value = document.createElement("INPUT");
    confirm_value.type = "hidden";
    confirm_value.name = "confirm_value";
    alertify.confirm('<%= GetGlobalResourceObject("Resource","WebEDI_PDF_MsgBox") %>', function () {
        confirm_value.value = "Yes";
        document.forms[0].appendChild(confirm_value);
        __doPostBack('<%= PrchBtnHidden.UniqueID %>');
    },
      function () {
          confirm_value.value = "No";
          document.forms[0].appendChild(confirm_value);
          __doPostBack('<%= PrchBtnHidden.UniqueID %>');
      }).set('labels', { ok: 'Ok', cancel: 'No' });
}

当你点击第一个按钮时,它会调用JS。然后JS会调用第二个按钮的PostBack。我对Alertify有问题,所以我使用了另一个来源:http://alertifyjs.com/

您可以触发隐藏按钮上的点击,在隐藏按钮上可以在代码后面附加点击处理程序。

服务器:

protected void Page_Load(object sender, EventArgs e)
        {
            PrchBtnHiddenNo.Click += PrchBtnHiddenNo_Click;
            PrchBtnHiddenYes.Click += PrchBtnHiddenYes_Click;
        }
        void PrchBtnHiddenYes_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }
        void PrchBtnHiddenNo_Click(object sender, EventArgs e)
        {
            throw new NotImplementedException();
        }

客户:

<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
    <script src="Scripts/alertify.js"></script>
    <link href="Content/alertify.default.css" rel="stylesheet" />
    <link href="Content/alertify.core.css" rel="stylesheet" />
    <hgroup class="title">
        <h1>Test alertify</h1>
    </hgroup>

    <asp:Button ID="PrchBtn" runat="server" class="PrchBtn" Text="Click Here" OnClientClick="Confirm(); return false;" />
    <asp:Button runat="server" ID="PrchBtnHiddenYes" ClientIDMode="Static" Style="display: none;" />
    <asp:Button runat="server" ID="PrchBtnHiddenNo" ClientIDMode="Static" Style="display: none;" />
    <script>
        function Confirm() {
            var confirm_value = document.createElement("INPUT");
            confirm_value.type = "hidden";
            confirm_value.name = "confirm_value";
            alertify.confirm('Do it', function (e) {
                if (e) {
                    $('#PrchBtnHiddenYes').click();
                    confirm_value.value = "Yes";
                } else {
                    $('#PrchBtnHiddenNo').click();
                    confirm_value.value = "No";                    
                }
            });
        }
    </script>    
</asp:Content>

JS源代码和css下载自:http://fabien-d.github.io/alertify.js/

重要提示:如果你试图触发点击文件上传输入酸痛IE不会让你这么做。

您可以使用纯java脚本无jquery:

document.getElementById("PrchBtnHiddenYes").click();