单击“浏览器”后退按钮导致“文档过期问题”

时间:2016-11-18 11:14:05

标签: javascript jquery asp.net

我有一个aspx页面,Usercontrol在其上调用了以下代码 脚本: -

<script type="text/javascript">

    $(document).ready(function () {
        $('.error-load').hide();
        $('#imgLoading').hide();
        $("#BtnLoadMore").click(function () {
            $(this).hide();
            var lastArticleId = $('#hdnLastArticle').val();
            var sortOrder = $('#<%= hdnSortValue.ClientID  %>').val();
            $('#imgLoading').show();
            $.ajax({
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: '{"RowId":"' + lastArticleId + '", "SortOrder":"' + sortOrder + '"}',
                url: "http://blogs.sample.com/themes/blogs/vitalvotes/LoadService.aspx/GetNextData",
                dataType: "json",
                success: RenderPagedData,
                error: function (response) {
                    $('.error-load').show();
                    $('#imgLoading').hide();
                }
            });

        });

    });

    function RenderPagedData(data) {

        var result = JSON.parse(data.d);
        var htmlString = null;
        for (i = 0; i < result.length; i++) {
            htmlString = "<tr><td><div class='current-articles'><div class='current-thumb'><a ";
            htmlString += "href='" + result[i].ArticleLink + "'";
            htmlString += "rel='nofollow'><img src='" + result[i].ThumbnailImage + "'style='height:135px;width:186px;border-width:0px;'>";
            htmlString += "</a></div><div class='current-list'><h2>";
            htmlString += "<a ";
            htmlString += "href='" + result[i].ArticleLink + "'>";
            htmlString += "" + result[i].Subject + "</a></h2><div class='views-comments'><h5>";
            htmlString += "<span style='color:Gray;'>" + result[i].NewsletterDate + "</span>";
            htmlString += "<span class='gray'>|</span> <span><span>";
            htmlString += "" + result[i].ViewCount + " Views </span></a></span></h5></div>";
            htmlString += "<span>" + result[i].Body + "</span>";
            htmlString += "</div><div class='clear'></div></div><div class='hr'></div></td></tr>";
            $('#ctl00_ctl00_ctl00_bcr_bcr_bcr_BlogsPostForDefault_gvBlogsPost tr:last').after(htmlString);
            if (i == result.length - 1) {
                $('#hdnLastArticle').val(result[i].RowId);
            }
            $('#imgLoading').hide();
            $('#lblLoadError').hide();
            $("#BtnLoadMore").show();
        }


    }
</script>

设计: -

<radAjax:RadAjaxPanel ID="Panel" runat="server LoadingPanelID="Loadingpanel6">
    <div>
        <asp:HiddenField ID="hdnSortValue" runat="server" />
    </div>
    <div class="current-btns" style="float: left !important;">
        <div id="divFitnessSortBy" runat="server" class="fit-sort">
            <table>
                <tr>
                    <td>
                        <b>Sort by :</b>
                    </td>
                    <td>
                        <asp:Button ID="btnMostRecent" Text="Most Recent" class="button action sortcomments"
                            runat="server" OnCommand="btnMostRecent_Click" CommandArgument="date" />
                    </td>
                    <td>
                        <asp:Button ID="btnMostViewed" Text="Most Viewed" class="Comments" runat="server"
                            OnCommand="btnMostViewed_Click" CommandArgument="views" />
                    </td>
                </tr>
            </table>
        </div>
        <div class="clear">
        </div>
    </div>
    <div class="hr">
    </div>
    <div class="BlogsPostHolderDiv" id="divPagination">
        <asp:GridView ID="gvBlogsPost" runat="server" DataKeyNames="PostID" AutoGenerateColumns="False"
            AllowPaging="false" Width="100%" OnRowDataBound="gvBlogsPost_Bound" GridLines="None">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <div class="current-articles">
                            <div class="current-thumb">
                                <a id="lnkThumbnail" runat="server" rel="nofollow">
                                    <asp:Image ID="imgThumbnail" runat="server" Height="135px" Width="186px" />
                                </a>
                            </div>
                            <div class="current-list">
                                <h2>
                                    <a id="lnkArticle" runat="server">
                                       <%#DataBinder.Eval(Container.DataItem,"Subject")%></a>
                                </h2>
                                <div class="views-comments">
                                    <h5>
                                        <asp:Label runat="server" ID="lblNewsletterDate" ForeColor="#808080"><%# DataBinder.Eval(Container.DataItem, "PostDate")%></asp:Label>
                                        <span class="gray">|</span> <span id="spnViews" runat="server"><span>
                                            <%#Convert.ToInt64(DataBinder.Eval(Container.DataItem, "TotalViews")).ToString("#,#")%>
                                            Views </span></span>
                                    </h5>
                                </div>
                                <asp:Label ID="lblShortDescription" runat="server"><%#DataBinder.Eval (Container.DataItem, "Body")%> </asp:Label>
                            </div>
                            <div class="clear">
                            </div>
                        </div>
                        <div class="hr">
                        </div>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <div id="divLoadMore" style="text-align: center; margin: 5px auto;">
            <input type="button" id="BtnLoadMore" class="button action" style="float: none;"
                value="Load more Vitalvotes articles" /></div>
        <div class="error-load">
            Unable to load more articles</div>
        <div align="center">
            <img id="imgLoading" visible="false" alt="Loading" src="http://media.sample.com/Assets/images/text.gif" /></div>
    </div>
    <input type="hidden" id="hdnLastArticle" value="10" />
    <radAjax:AjaxLoadingPanel ID="Loadingpanel6" runat="server">
        <asp:Image ID="Image1" runat="server" AlternateText="Loading" BorderWidth="0px" ImageUrl="/themes/article/images/loading6.gif" />
    </radAjax:AjaxLoadingPanel>
</radAjax:RadAjaxPanel>`

当页面最初加载时,数据被加载。使用默认按钮btnMostRecent,如果我打开任何文章并单击浏览器的后退按钮,它可以正常工作。但是,当我点击按钮'btnMostRecentit将加载文章,如果我打开任何文章和点击后退按钮它显示文件到期问题。。请告诉我如何解决它?

1 个答案:

答案 0 :(得分:0)

这种情况正在发生,因为当您单击按钮时,它实际上是将页面发回,而不是进行ajax调用。

当页面从客户端发布到服务器时,如果您从结果页面导航到其他页面,然后点击后退按钮,浏览器需要再次发送帖子以重新生成该页面,因此它显示“文档已过期”消息。不同的浏览器处理方式不同有些人会问您是否要再次发布表格。无论哪种方式,它通常都是不受欢迎的,因为它会使网站访问者感到困惑。

无论如何,解决这个问题的“行业标准解决方案”就是所谓的“后重定向获取”模式。在这种方法中,当您单击向服务器发送http帖子的按钮时,您有服务器端代码执行响应重定向到同一页面(导致http get),通常使用查询字符串中传递的参数。

由于页面最终是基于http get而不是http帖子呈现的,如果您通过链接从页面导航然后使用后退按钮返回它,浏览器可以再次获取页面,没有'文件过期问题'或提示重新发布表格。