Sitecore: Turning on HTML Caching blocks postback behavior

我的未来我决定 提交于 2019-12-01 19:00:23

问题


I have a sitecore page with an ASP dropdownlist, and the data on the form is populated from the selected value of the dropdown. When the selected item of the dropdownlist is changed, a postback is fired. In the postback, the new selected item is added to the querystring and the user is redirected (for linkability).

I recently enabled HTML caching (for all sublayouts, "Vary by querystring"), and now suddenly, this mechanism no longer works. What seems to happen is I select a new dropdown item, the page appears to post back (though if I'm debugging, none of my breakpoints get hit). After that, if I change the selected item again, I can see in Firebug the message "__doPostBack is not defined", which appears to mean the ASP-generated JavaScript is not being added to the page.


回答1:


Enabling caching for a sublayout means you are bypassing the code entirely and Sitecore is just serving up the same HTML it generated previously. So it's behaving as designed. In other words, this does not seem to be a scenario where you can take advantage of sublayout caching.




回答2:


As posted previously, this is expected behavior precisely because the page is being fetched from the cache. You can still support caching for non-postback loads, but the easiest way I've found is to sense the postback with code in Global.asax and switch accordingly, as in the sample below.



    public override string GetVaryByCustomString(HttpContext context, string custom)
    {
        if (context.Request.RequestType.Equals("POST"))
        {
            context.Response.Cache.SetNoServerCaching();
            return "POST " + DateTime.Now.Ticks + " " + context.Request.RawUrl;
        }

        switch (custom)
        {
            case "RAWURL":
                return context.Request.RawUrl;
            default:
                return "";
        }
    }

Then you can hook this to output cache directives in your controls:

<%@outputcache duration="3600" varybyparam="none" varybycustom="RAWURL" %>

Note that if you do it this way, you lose the easy ability to vary by a control's data source.



来源:https://stackoverflow.com/questions/3628185/sitecore-turning-on-html-caching-blocks-postback-behavior

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!