JS不使用asp.net更新面板运行

时间:2013-05-24 20:25:26

标签: javascript asp.net updatepanel

以下代码在没有更新面板的情况下工作,但在有更新面板时不运行。其目的是从后面或前面的浏览器按下清除页面条目上的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>

1 个答案:

答案 0 :(得分:1)

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

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

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