1

以下代码在没有更新面板的情况下工作,但在有更新面板时不会运行。其目的是清除来自向后或向前浏览器按下的页面条目上的 bfcache。任何人都知道当用户返回/前进并且页面有更新面板时如何让 js 运行?

window.onpageshow = function (event) { if (event.persisted) { window.location.reload() } };

带有更新面板的完整页面:

<body>
<form id="form1" runat="server">
    <ajaxToolkit:ToolkitScriptManager runat="server" ID="ToolkitScriptManager" />
     <asp:UpdatePanel ID="updatePanel1" UpdateMode="Conditional" runat="server">
        <ContentTemplate>
            <script type="text/javascript">
                var js = "window.onpageshow = function (event) { if (event.persisted) { window.location.reload() } };";

                Sys.Application.add_load(js);

                var prm = Sys.WebForms.PageRequestManager.getInstance();

                prm.add_endRequest(js);
                prm.add_beginRequest(js);
            </script>

            <a href='Default.aspx'>Default</a>
            <a href='Default2.aspx'>Default2</a>
            <a href='Default3.aspx'>Default3</a>
            <a href='Default4.aspx'>Default4</a>
            <br />
            <div>
                <asp:TextBox ID="tbText" runat="server"></asp:TextBox>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</form>

尝试#2

<body>
<script type="text/javascript">
    window.onpageshow = function (event) { if (event.persisted) { window.location.reload() } };
    </script>
<form id="form1" runat="server">
    <ajaxToolkit:ToolkitScriptManager runat="server" ID="ToolkitScriptManager" />
     <asp:UpdatePanel ID="updatePanel1" UpdateMode="Conditional" runat="server">
        <ContentTemplate>
            <a href='Default.aspx'>Default</a>
            <a href='Default2.aspx'>Default2</a>
            <a href='Default3.aspx'>Default3</a>
            <a href='Default4.aspx'>Default4</a>
            <br />
            <div>
                <asp:TextBox ID="tbText" runat="server"></asp:TextBox>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</form>

尝试#3

<body>
<form id="form1" runat="server">
    <ajaxToolkit:ToolkitScriptManager runat="server" ID="ToolkitScriptManager" />
     <asp:UpdatePanel ID="updatePanel1" UpdateMode="Conditional" runat="server">
        <ContentTemplate>
            <script type="text/javascript">
                var js = window.onpageshow = function (event) { if (event.persisted) { window.location.reload() } };;

                Sys.Application.add_load(js);

                var prm = Sys.WebForms.PageRequestManager.getInstance();

                prm.add_endRequest(js);
                prm.add_beginRequest(js);
            </script>

            <a href='Default.aspx'>Default</a>
            <a href='Default2.aspx'>Default2</a>
            <a href='Default3.aspx'>Default3</a>
            <a href='Default4.aspx'>Default4</a>
            <br />
            <div>
                <asp:TextBox ID="tbText" runat="server"></asp:TextBox>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</form>

4

1 回答 1

1

您的错误是您已将字符串作为必须运行的代码部分。"从此行中删除:

var js = 
 window.onpageshow = 
    function (event) { if (event.persisted) { window.location.reload() } };

还有一个,您可以从更新面板中删除该部分 stript 并将其放置在之前。

于 2013-05-24T20:29:55.320 回答