views:

32

answers:

3

I have, e.g. a listing page with filter inputs, from which the user can navigate to a capture page, where they might spend some time capturing, before returning to the listing page. When they return to the listing page, I would like their previous filter to be be automatically applied again.

This is no rocket science problem, so I'm sure already has a myriad solutions, but I hope to get some answers here that enlighten me as to commonly used patterns for solving this. My immediate idea is for a session object for page 'pre-sets', with a dictionary per page. This could also be extended to longer term pre-sets if stored somewhere longer term than session.

+1  A: 

Session is a good place to store this kind of information. I don't see any reason why using Session would not work for you in this case.

Andrew Hare
+1  A: 

yes you can store filter for each page in a dictionary object which itself can be put into session like:

IDictionary<string,string> pageFilters;
if(Session["filters"]==null){
    pageFilters = new Dictionary<string,string>();
    Session["filters"]=pageFilters;
}else{
   pageFilters=(IDictionary)Session["filters"];
}

if(pageFilters.ContainsKey(CURRENT_PAGE_NAME OR KEY))
{
   pageFilters[CURRENT_PAGE_NAME OR KEY]=/*FILTER FOR PAGE*/;
}else{
   pageFilters[CURRENT_PAGE_NAME OR KEY]=/*FILTER FOR PAGE*/;
}
A: 

I have some helper functions in a utility class where I pass in the controls I want to save or load to the session. Typically I will call the load on the initial page load, and call the save functions after the user executes a search or whenever else I want to save.

I create a new overload for each kind of control I want to save to the session. Here is one for a TextBox. I am not sure if this is very good code or not, but it seems to work reasonably well.

public void SaveToSession(TextBox control, HttpSessionState session, string SectionKey)
{
    session[SectionKey + control.ID] = control.Text;
}

public string LoadFromSession(TextBox control, HttpSessionState session, string SectionKey, string DefaultValue)
{
    string ReturnValue = "";
    if (session[SectionKey + control.ID] != null)
    {
     control.Text = session[SectionKey + control.ID].ToString();
     ReturnValue = session[SectionKey + control.ID].ToString();
    }
    else
    {
     control.Text = DefaultValue;
     ReturnValue = DefaultValue;
    }
    return ReturnValue;

}
Chris Mullins