jQuery dialog pop up on every page postback

641 Views Asked by At

I have a web page that has a master file. The master file has an update panel that contains a timer, used for showing adds/images every so many seconds. However the page's content place holder is not contained within the update panel. This is the basic markup of the master page:

<head id="hdMain" runat="server">
    <title>Main Master Page</title>
    <link href="~/Style/custom-theme/jquery-ui-1.10.1.custom.min.css" rel="stylesheet" />
    <asp:ContentPlaceHolder
        ID="cpHead"
        runat="server">
    </asp:ContentPlaceHolder>
</head>
<body style="margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px" scroll="no">
    <form id="frmMain" runat="server">
        <asp:ScriptManager
            ID="smMain"
            runat="server">
        </asp:ScriptManager>
        <div style="height: 671px; width: 1024px; z-index: 0;">
            <asp:ContentPlaceHolder
                ID="cpMain"
                runat="server">
            </asp:ContentPlaceHolder>
        </div>
        <div style="background-color: #192646; height: 97px; width: 1024px">
            <asp:UpdatePanel
                ID="upMain"
                runat="server"
                UpdateMode="Conditional">
                <Triggers>
                    <asp:AsyncPostBackTrigger ControlID="tmrMain" />
                </Triggers>
                <ContentTemplate>
                    <asp:Timer
                        ID="tmrMain"
                        runat="server"
                        Interval="10000"
                        OnTick="RotateImage">
                    </asp:Timer>
                    <img
                        id="imgMain"
                        runat="server"
                        height="97"
                        width="1024"
                        alt=""
                        src="" />
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>

Now, when my page loads, I need to do some server side processing before deciding if I want to fire up a jQuery dialog. I am using the code below in my code behind to do this:

if (NeedToRunStartupScript())
{
    ClientScript.RegisterStartupScript(this.GetType(), "RedirectScript", "Sys.Application.add_load(function() { OpenDialog(); });", true);
}

The OpenDialog() is defined as following in the markup, withing the header content place holder:

<script type="text/javascript" src="../Scripts/JQuery/jquery-1.9.1.js"></script>
<script type="text/javascript" src="../Scripts/JQuery/jquery-ui-1.10.1.custom.min.js"></script>
<script type="text/javascript">
     function OpenDialog() {
         $(function () {
             var redirectSomewhere = false;
             var newDialog = $('<div title="Dialog">\
                                <p>Do you wish to redirect?</p>\
                               </div>');

             newDialog.dialog({
                 height: 250,
                 width: 300,
                 modal: true,
                 buttons: {
                     Yes: function () {
                         redirectSomewhere = true;
                         $(this).dialog("close");
                     },
                     No: function () {
                         $(this).dialog("close");
                     }
                 },
                 close: function () {
                     if (redirectSomewhere) {
                         window.location.href = "SomePage.aspx";
                     }
                 }
             });
         });
     };

Pretty standard stuff really. And this code works. However, each time a timer clicks and updates the image, the dialog window shows up again, so if I let it just sit there for several minutes, I will have tens of dialog windows on top of one another. I basically need my dialog to be registered only once, and not be reinitialized on every partial postback. Any help would be appreciated.

1

There are 1 best solutions below

1
On

Have you tried checking for async postback like this

bool isAsyncPostback = ScriptManager.GetCurrent(this).IsInAsyncPostBack;
if (NeedToRunStartupScript() && !isAsyncPostback)
{
    ClientScript.RegisterStartupScript(this.GetType(), "RedirectScript", "Sys.Application.add_load(function() { OpenDialog(); });", true);
}