简体   繁体   中英

Output cache for page that varies by several DropDownList controls

Is there a way to use Output cache for the page that varies by several DropDownList controls?

I thought about using Cache variables to cache several elements of the page and read them based on the values that I get from the DDLs from codebehind. But the problem is that I don't want the data to be cache "Forerver", and I don't want to change the default Cache duration for the entire site.

I know that for outputcache I get decide the timeout of the caching for a specific page, so I think it's the best solution, but I have a problem implementing it with several DDLs.

This question talks about using VaryByParam , but it's not what I need.

In ASP.NET, the first time a page is loaded ( IsPostBack false; HTTP method = GET) it will set initial default values for your DDLs.

When the user changes DDL values and submits the page, it is submitted with the HTTP POST method, and IsPostBack is true. Requests using the HTTP POST method are not cached.

One way you may be able to achieve what you want is the following. This is for a single DDL but can be extended to multiple DDLs.

  • Set AutoPostBack = true on your DDL

  • Handle the SelectedIndexChanged event, and redirect ( Response.Redirect ) to the same page after modifying the URL to add/update a querystring parameter (eg ...&MyDDLIndex=4... ).

  • When your page is first loaded ( IsPostBack = false), initialize the DDL's SelectedIndex property based on your custom querystring value, with a suitable default if the querystring value is missing or invalid.

By doing this, you will get a new URL for each combination of DDL settings you want. Eg with three DDLs, you might have a URL that looks like:

`...MyPage.aspx?MyDDL1Index=2&MyDDL2Index=4&MyDDL3Index=5...`

You now have unique URLs for each combination of DDL values, and can use VaryByParam to cache versions of this page based on these querystring values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM