简体   繁体   中英

Closing jQuery Modal dialog after ASP.NET postback opens it again

I have an HTML link that opens a jQuery modal dialog which contains an iFrame. This iFrame contains a form that should close dialog after postback ( Ok or Cancel ).

In my postback I add a Javascript function to iFrame that closes the dialog and it works fine. However problem is, after user presses Ok or Cancel button, dialog closes and opens again! What am I doing wrong?

Here is my code:

Calling HTML page:

<html>
<head>
    <link rel="stylesheet" type="text/css" href="theme/south-street.css" media="screen" id="themeCSS" />

    <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $("#dlgConfirm").dialog({
                autoOpen: false,
                resizable: false,
                modal: true,
                width: 550,
                height: 300,
                close: function (event, ui) { alert('closed') }
            })
        });


        function fConfirm(tId) {
            alert('ddd');
            $('#dlgConfirm').children().attr('src', 'ConfirmTracking.aspx?tId=' + tId)
            .load(function () {
                $("#dlgConfirm").dialog('open');
            });
            return false;
        }
    </script>
</head>
<body>
    <div style="display: none">
        <div id="dlgConfirm" title="Confirm Tracking" style="padding: 0px">
            <iframe style="margin: 0px; padding: 0px" marginheight="0" marginwidth="0" src="" width="100%" height="100%" frameborder="0"></iframe>
        </div>
    </div>

    <a href="#" onclick="return fConfirm('109')">Click me</a>
</body>
</html>

iFrame Page

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ConfirmTracking.aspx.cs" Inherits="Chicken.ConfirmTracking" %>
<html>
<head runat="server">
    <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui-1.8.16.custom.min.js"></script>
    <script type="text/javascript">
        function CloseThis() {
            parent.$('#dlgConfirm').dialog('close');
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <br />
        <asp:TextBox ID="edtTrackingNo" runat="server"></asp:TextBox>
        <br /><br />
    <asp:Button ID="btnOk" runat="server" Text="Ok" />
    <asp:Button ID="btnCancel" runat="server" onclick="Cancel_Click" Text="Cancel" />
    </form>
</body>
</html>

iFrame Codebehind:

using System;

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

        }

        protected void Cancel_Click(object sender, EventArgs e) {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "none", "<script>$(function(){CloseThis()});</script>");
        }
    }
}

I managed to trace your problem, the load in function fConfirm(tId) triggers again after closing your dialog. It seems to be a bug. A messy way to fix this is adding a global variable to your page and set it to true to show that dialog is opened via function then before opening your dialog if the variable is true open the dialog. don't foget to set your variable to false again so you can re-open dialog if it is needed.

your function will look like below:

    var loadedByFunction = false;

    function fConfirm(tId) {
        loadedByFunction = true;
        $('#dlgConfirm').children().attr('src', 'ConfirmTracking.aspx?tId=' + tId)
        .load(function () {
            if (loadedByFunction) {
                loadedByFunction = false;
                $("#dlgConfirm").dialog('open');
            }
        });
        return false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM