Javascript确认窗口无回发

Javascript confirm window no postback

本文关键字:窗口 确认 Javascript      更新时间:2023-09-26

我在一个页面上创建了Javascript弹出窗口,可以在回发时提供确认脚本,例如,我在页面加载中有这个

DotNetNuke.Framework.jQuery.RequestDnnPluginsRegistration();
string eventTarget = (this.Request["__EVENTTARGET"] == null) ? string.Empty : this.Request["__EVENTTARGET"];
string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];
cancelSelected = btnCancel_Click_Confirmed(eventTarget, eventArgument);

这种方法要求取消选定的

        protected const string cancelScriptKey = "CancelScript";
        protected void Cancel_Click(object sender, EventArgs e)
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), cancelScriptKey, CoachTeamRegister.BuildConfirmScript("Are you sure you want to cancel?'nAll unsaved changes would be lost?", cancelScriptKey), true);
        }

在这种情况下,这是有效的,因为我可以使用PageLoad 中的变量cancelSelected完成操作

不过,在代码的后面,我需要在一个方法中进行内部处理,直到方法完成,才返回。有没有一种方法可以创建一个确认对话框,并从所选内容中接收布尔值而不返回?

我能找到的最接近的是这样的问题:我可以通过使用javascripts-confirm来停止回发吗?它讨论了页面上我已经知道如何使用的静态按钮,客户想要一个特定情况的对话框

我知道的阻止执行的唯一方法是使用默认的confirm,它将阻止执行,直到从对话框中收到结果。当使用像JQuery这样的自定义对话框时,情况并非如此。实现这一点的一个变通方法是,根据对话框中的结果,使用__DoPostBackdocument.getElementById('btnId').click(); ,通过JS执行服务器端方法

假设您在标记中有此按钮

<asp:button id="btnSave" runat="server" OnClick="btnSave_Click"/>

根据结果调用服务器端活动。

     <script>
      $(function() {
        $( "#dialog-confirm" ).dialog({
          resizable: false,
          height:140,
          modal: true,
          buttons: {
            "Save": function() {
              $( this ).dialog( "close" );
                 //Save was clicked. Call the server side event.
                 document.getElementById('btnSave clientId').click(); 
                 //Or alternatively __DoPostBack('btnSave clientId','');
            },
            Cancel: function() {
              $( this ).dialog( "close" );
                //Show message for the user if needed.
            }
      }
    });
  });
  </script>