2

マスター ファイルを含む Web ページがあります。マスター ファイルには、数秒ごとに追加/画像を表示するために使用されるタイマーを含む更新パネルがあります。ただし、ページのコンテンツ プレースホルダーは更新パネルに含まれていません。これは、マスター ページの基本的なマークアップです。

<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>

ここで、ページが読み込まれると、jQuery ダイアログを起動するかどうかを決定する前に、サーバー側の処理を行う必要があります。これを行うために、コードビハインドで以下のコードを使用しています。

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

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

かなり標準的なものです。そして、このコードは機能します。ただし、タイマーがクリックして画像を更新するたびに、ダイアログ ウィンドウが再び表示されるため、そのまま数分間そのままにしておくと、何十ものダイアログ ウィンドウが重なって表示されます。基本的に、ダイアログを一度だけ登録し、部分的なポストバックごとに再初期化する必要はありません。どんな助けでも大歓迎です。

4

1 に答える 1

0

このような非同期ポストバックをチェックしてみましたか

bool isAsyncPostback = ScriptManager.GetCurrent(this).IsInAsyncPostBack;
if (NeedToRunStartupScript() && !isAsyncPostback)
{
    ClientScript.RegisterStartupScript(this.GetType(), "RedirectScript", "Sys.Application.add_load(function() { OpenDialog(); });", true);
}
于 2013-12-09T16:16:12.577 に答える