I would probably do it this way:
- Create an object to store the state information you want to be page specific. If different pages need different information, create multiple classes.
- Store this object in a single session key: Session["PageSpecific"]; for example.
- Create a class which inherits from System.Web.UI.Page.
- In the OnLoad event of the base class, clear the session key if the the page is not performing a postback.
- Create and call an overloadable method to populate the session object.
- Instead of inheriting from System.Web.UI.Page in each of your pages, inherit from your new base class.
Something like this (warning: air code. May contain syntax errors):
public class PageBase
: System.Web.UI.Page
{
protected overrides OnInit(System.EventArgs e) {
base.OnInit(e);
if(!this.IsPostBack) {
Guid requestToken = System.Guid.NewGuid();
ViewState["RequestToken"] = requestToken;
Session["PageSpecific" & requestToken.ToString()] = InitializePageSpecificState();
}
}
protected virtual object InitializePageSpecificState() {
return new GenericPageState();
}
//You can use generics to strongly type this, if you want to.
protected object PageSpecificState {
get {
return Session["PageSpecific" & ViewState["RequestToken"].ToString()];
}
}
}