Javascript 如何在asp.net中调用浏览器关闭或返回按钮上的模式窗口?

Javascript 如何在asp.net中调用浏览器关闭或返回按钮上的模式窗口?,javascript,c#,asp.net,Javascript,C#,Asp.net,我的页面上有一个返回按钮。单击该返回按钮执行以下代码 PageInformation InfoView = new PageInformation (); InfoView = (PageInformation )ViewState["PBLSave"]; if (txtValidFPGTE18MQ1.Text != InfoView.FPGTE18.Q1.ToString() || txtValidFPGTE18MQ2.Text != Inf

我的页面上有一个
返回
按钮。单击该
返回
按钮
执行以下代码

        PageInformation InfoView = new PageInformation ();

        InfoView = (PageInformation )ViewState["PBLSave"];

        if (txtValidFPGTE18MQ1.Text != InfoView.FPGTE18.Q1.ToString() || txtValidFPGTE18MQ2.Text != InfoView.FPGTE18.Q2.ToString()
            || txtValidFPGTE18MQ3.Text != InfoView.FPGTE18.Q3.ToString() || txtValidFPGTE18MQ4.Text != InfoView.FPGTE18.Q4.ToString()
            || txtValidFPGTE18MQ5.Text != InfoView.FPGTE18.Q5.ToString() || txtValidFPGTE18MQ6.Text != InfoView.FPGTE18.Q6.ToString())
        {
            mdlPopupExtender.Show();
        }
        else
        {
            redirectUrl = "../UI/Summary_Page.aspx";
            Response.Redirect(redirectUrl);
        }

这里的
mdlPopupExtender
是我定制的模式窗口。我想执行整个代码,并在浏览器的
单击
关闭[X]
和浏览器后退按钮的
上显示此模式窗口。我该怎么做

假设函数名为RunOnClose

在CS文件中

[System.Web.Services.WebMethod()]
public static void RunOnClose()
        {
            PageInformation InfoView = new PageInformation ();

        InfoView = (PageInformation )ViewState["PBLSave"];

        if (txtValidFPGTE18MQ1.Text != InfoView.FPGTE18.Q1.ToString() || txtValidFPGTE18MQ2.Text != InfoView.FPGTE18.Q2.ToString()
            || txtValidFPGTE18MQ3.Text != InfoView.FPGTE18.Q3.ToString() || txtValidFPGTE18MQ4.Text != InfoView.FPGTE18.Q4.ToString()
            || txtValidFPGTE18MQ5.Text != InfoView.FPGTE18.Q5.ToString() || txtValidFPGTE18MQ6.Text != InfoView.FPGTE18.Q6.ToString())
        {
            mdlPopupExtender.Show();
        }
        else
        {
            redirectUrl = "../UI/Summary_Page.aspx";
            Response.Redirect(redirectUrl);
        }
        }
<script type="text/javascript">
                     window.onbeforeunload = function () {
                         $.ajax({

                             type: "POST",
                             url: "WebForm1.aspx/RunOnClose",
                             data: "{}",
                             contentType: "application/json; charset=utf-8",
                             dataType: "json",
                             success: function () {
                                 alert('Funtion called!');

                             },
                             error: function () {
                                 alert('Funtion not called');
                             }
                         });
                     };
    </script>
在aspx文件中

[System.Web.Services.WebMethod()]
public static void RunOnClose()
        {
            PageInformation InfoView = new PageInformation ();

        InfoView = (PageInformation )ViewState["PBLSave"];

        if (txtValidFPGTE18MQ1.Text != InfoView.FPGTE18.Q1.ToString() || txtValidFPGTE18MQ2.Text != InfoView.FPGTE18.Q2.ToString()
            || txtValidFPGTE18MQ3.Text != InfoView.FPGTE18.Q3.ToString() || txtValidFPGTE18MQ4.Text != InfoView.FPGTE18.Q4.ToString()
            || txtValidFPGTE18MQ5.Text != InfoView.FPGTE18.Q5.ToString() || txtValidFPGTE18MQ6.Text != InfoView.FPGTE18.Q6.ToString())
        {
            mdlPopupExtender.Show();
        }
        else
        {
            redirectUrl = "../UI/Summary_Page.aspx";
            Response.Redirect(redirectUrl);
        }
        }
<script type="text/javascript">
                     window.onbeforeunload = function () {
                         $.ajax({

                             type: "POST",
                             url: "WebForm1.aspx/RunOnClose",
                             data: "{}",
                             contentType: "application/json; charset=utf-8",
                             dataType: "json",
                             success: function () {
                                 alert('Funtion called!');

                             },
                             error: function () {
                                 alert('Funtion not called');
                             }
                         });
                     };
    </script>

window.onbeforeunload=函数(){
$.ajax({
类型:“POST”,
url:“WebForm1.aspx/RunOnClose”,
数据:“{}”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:函数(){
警报('调用函数!');
},
错误:函数(){
警报(“未调用函数”);
}
});
};

假设函数名为RunOnClose

在CS文件中

[System.Web.Services.WebMethod()]
public static void RunOnClose()
        {
            PageInformation InfoView = new PageInformation ();

        InfoView = (PageInformation )ViewState["PBLSave"];

        if (txtValidFPGTE18MQ1.Text != InfoView.FPGTE18.Q1.ToString() || txtValidFPGTE18MQ2.Text != InfoView.FPGTE18.Q2.ToString()
            || txtValidFPGTE18MQ3.Text != InfoView.FPGTE18.Q3.ToString() || txtValidFPGTE18MQ4.Text != InfoView.FPGTE18.Q4.ToString()
            || txtValidFPGTE18MQ5.Text != InfoView.FPGTE18.Q5.ToString() || txtValidFPGTE18MQ6.Text != InfoView.FPGTE18.Q6.ToString())
        {
            mdlPopupExtender.Show();
        }
        else
        {
            redirectUrl = "../UI/Summary_Page.aspx";
            Response.Redirect(redirectUrl);
        }
        }
<script type="text/javascript">
                     window.onbeforeunload = function () {
                         $.ajax({

                             type: "POST",
                             url: "WebForm1.aspx/RunOnClose",
                             data: "{}",
                             contentType: "application/json; charset=utf-8",
                             dataType: "json",
                             success: function () {
                                 alert('Funtion called!');

                             },
                             error: function () {
                                 alert('Funtion not called');
                             }
                         });
                     };
    </script>
在aspx文件中

[System.Web.Services.WebMethod()]
public static void RunOnClose()
        {
            PageInformation InfoView = new PageInformation ();

        InfoView = (PageInformation )ViewState["PBLSave"];

        if (txtValidFPGTE18MQ1.Text != InfoView.FPGTE18.Q1.ToString() || txtValidFPGTE18MQ2.Text != InfoView.FPGTE18.Q2.ToString()
            || txtValidFPGTE18MQ3.Text != InfoView.FPGTE18.Q3.ToString() || txtValidFPGTE18MQ4.Text != InfoView.FPGTE18.Q4.ToString()
            || txtValidFPGTE18MQ5.Text != InfoView.FPGTE18.Q5.ToString() || txtValidFPGTE18MQ6.Text != InfoView.FPGTE18.Q6.ToString())
        {
            mdlPopupExtender.Show();
        }
        else
        {
            redirectUrl = "../UI/Summary_Page.aspx";
            Response.Redirect(redirectUrl);
        }
        }
<script type="text/javascript">
                     window.onbeforeunload = function () {
                         $.ajax({

                             type: "POST",
                             url: "WebForm1.aspx/RunOnClose",
                             data: "{}",
                             contentType: "application/json; charset=utf-8",
                             dataType: "json",
                             success: function () {
                                 alert('Funtion called!');

                             },
                             error: function () {
                                 alert('Funtion not called');
                             }
                         });
                     };
    </script>

window.onbeforeunload=函数(){
$.ajax({
类型:“POST”,
url:“WebForm1.aspx/RunOnClose”,
数据:“{}”,
contentType:“应用程序/json;字符集=utf-8”,
数据类型:“json”,
成功:函数(){
警报('调用函数!');
},
错误:函数(){
警报(“未调用函数”);
}
});
};

似乎不起作用:(javascript函数没有被调用。@NiroshaDKalinga您使用的是哪种浏览器?我使用的是IE 11。在chrome中也不起作用。页面上的所有其他按钮都会被调用错误函数,其中对于“浏览器后退”按钮和“关闭”[x],什么都不会发生。@Hamzaanis似乎不起作用:(javascript函数未被调用。@NiroshaDKalinga您使用的是哪种浏览器?我使用的是IE 11。在chrome中也不起作用。错误函数被调用用于页面上的所有其他按钮,其中对于浏览器后退按钮和关闭[x],什么都不会发生。@HamzaAnis