I am using Output Caching and want to implement GetVaryByCustomString () version of it. However I've gotten into a problem when the cache is based on a value set in the PageLoad() of a page.

When Default.aspx load, the version of the USER CONTROL I would like to show is based on the URL which needs to go through a database query. will load and in the code behind of the page, I make a query to the database, retrieve the values I want.

Would it be possible to use output caching, or would I need to implement my own variation of it.

Would it be possible to querying the Database earlier in the page lifecycle such that when GetVaryByCustomString is executed, the variable will be already set?

+1  A: 

I moved the code of assigning the variable into the Page_PreInit method which seems to be executing before the GetVaryByCustomString().