简体   繁体   English

jQuery对话框在每个页面回发上弹出

[英]jQuery dialog pop up on every page postback

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. 现在,当页面加载时,我需要做一些服务器端处理,然后再决定是否要启动jQuery对话框。 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: OpenDialog()在标记中定义如下,带有标题内容占位符:

<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. 任何帮助,将不胜感激。

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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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