Session variables problem

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I am having issues with the right way to architecture the following (using c#
asp.net):-

The question I have is how best pass the collected data from one web page
for use in another.

The question I have is how best pass the search criteria from the first page
to the second. The search criteria exists as a structure and is filled when
the user presses the 'Search' button on the first page.

1. Present an asp page for collecting user search criteria
2. Show grid with results of search after querying SQL



If I use the session to store it, then this causes problems as the session
variable is overwritten on a second search by the user, but a couple of
presses on the back button of the browser will get you to a page expecting
different criteria in the session from earlier which is no longer there.

ViewState seems no good as it only works on postback and the above issue
would still cause a problem.

I do not want to pass the search criteria in the address line as this is
messy and allows the user to change and view it.

I beleive you can also use Server.Transfer but I think this would have a
similiar issue with the data being available long-term.

Any pointers as to the correct approach to this would be most welcome.
 
A good technique for pages like this is to use the query string. That
way the data is all contained in the URL and users can bookmark
favorite searches. The URL does have a size limitation though (2048
characters, I think?)
 
One of the alternative architechture you can consider is - exposing public [shared] properties on the First page (where the search button located) and call them from the second page. In ASP.NET every form assocaited with a class and it can be called from any other classes. This way we don't have to use query string like we had to use in asp applications.

Take a look at this link - http://authors.aspalliance.com/aspxtreme/webapps/passingcontrolvaluesbetweenpages.aspx

Problem with this approach: when you want to come back to search page what criteria do you bring back from second page to first page? One way to accomplish this is - exposing public [shared] properties on second page and call them from the first page.

Microsoft also has documentation on this but I could not find it find handy. Hope this help.

Prodip Saha
 
Thanks for the quick reply scott. I was hoping to avoid using the URL query
string so as not to expose it to the user. I think my problem is coming from
a C++/C# background I haven't fully grapsed this stateless approach as yet !
 
you can store the query data in session in a keyed collection. just store
the key in the url or viewstate

-- bruce (sqlwork.com)


| I am having issues with the right way to architecture the following (using
c#
| asp.net):-
|
| The question I have is how best pass the collected data from one web page
| for use in another.
|
| The question I have is how best pass the search criteria from the first
page
| to the second. The search criteria exists as a structure and is filled
when
| the user presses the 'Search' button on the first page.
|
| 1. Present an asp page for collecting user search criteria
| 2. Show grid with results of search after querying SQL
|
|
|
| If I use the session to store it, then this causes problems as the session
| variable is overwritten on a second search by the user, but a couple of
| presses on the back button of the browser will get you to a page expecting
| different criteria in the session from earlier which is no longer there.
|
| ViewState seems no good as it only works on postback and the above issue
| would still cause a problem.
|
| I do not want to pass the search criteria in the address line as this is
| messy and allows the user to change and view it.
|
| I beleive you can also use Server.Transfer but I think this would have a
| similiar issue with the data being available long-term.
|
| Any pointers as to the correct approach to this would be most welcome.
 
Prodip,

Thanks for the quick reply. I will look at giving this a try but I think
that it may still have the same problem when users return to an earlier page,
would the first page class still be available at this point, if not I will
get the same problem I have now.

Simon.

Prodip Saha said:
One of the alternative architechture you can consider is - exposing public [shared] properties on the First page (where the search button located) and call them from the second page. In ASP.NET every form assocaited with a class and it can be called from any other classes. This way we don't have to use query string like we had to use in asp applications.

Take a look at this link - http://authors.aspalliance.com/aspxtreme/webapps/passingcontrolvaluesbetweenpages.aspx

Problem with this approach: when you want to come back to search page what criteria do you bring back from second page to first page? One way to accomplish this is - exposing public [shared] properties on second page and call them from the first page.

Microsoft also has documentation on this but I could not find it find handy. Hope this help.

Prodip Saha


Simon Matthews said:
I am having issues with the right way to architecture the following (using c#
asp.net):-

The question I have is how best pass the collected data from one web page
for use in another.

The question I have is how best pass the search criteria from the first page
to the second. The search criteria exists as a structure and is filled when
the user presses the 'Search' button on the first page.

1. Present an asp page for collecting user search criteria
2. Show grid with results of search after querying SQL



If I use the session to store it, then this causes problems as the session
variable is overwritten on a second search by the user, but a couple of
presses on the back button of the browser will get you to a page expecting
different criteria in the session from earlier which is no longer there.

ViewState seems no good as it only works on postback and the above issue
would still cause a problem.

I do not want to pass the search criteria in the address line as this is
messy and allows the user to change and view it.

I beleive you can also use Server.Transfer but I think this would have a
similiar issue with the data being available long-term.

Any pointers as to the correct approach to this would be most welcome
 
Patrice,

thanks I found the info on the URL not changing when you do a transfer.

I looked at keeping everything on one page, but I want to keep it seperate
for simplicity for the unlucky person who gets to maintain it !

Simon.
 
Bruce,

Thanks, we came up with this about 30mins ago and will most likely go this
route. If I key it into the session I suppose I would need to get rid of them
over time otherwise you could build up a lot of redundant data on long
sessions ?

I'm not sure I understand the viewstate properly, it only seems to get set
on a postback, so on the second page the first time it loads the vieewstate
would not save the details until the user did something ? but if they simply
looked at the results and then came back to the page later the viewstate
would be null ?

Simon.
 
Simon,
When the you returns to first page you would call the public properties
(which are populated from the first page to start with) on the second page.
Here is some code snippet. I am not using try/catch block for simplicity.

In the page load event of first page--
if (!Page.IsPostBack)
{
if (Context.Handler.ToString()=='secondpage_aspx')
{
_varSecondPageClass =(SecondPageClass)Context.Handler;
txtCriteria2.Text=_varSecondPageClass.Criteria2; //public
property on the second page
txtCriteria3.Text=_varSecondPageClass.Criteria3; //public
property on the second page
//do somthing
}
//do something
}

In the page load event of second page--
if (!Page.IsPostBack)
{
if (Context.Handler.ToString()=='firstpage_aspx')
{
_varFirstPageClass =(FirstPageClass)Context.Handler;
txtCriteria2.Text =_varSecondPageClass.Criteria2
=_varFirstPageClass.Criteria2; //public property on the second page
txtCriteria3.Text =_varSecondPageClass.Criteria3
=_varFirstPageClass.Criteria3; //public property on the second page
//do something
}
//do something
}

Things to remember: when you click the search button or return button, you
must reassign all the shared properties.

Thanks,
Prodip Saha

Simon Matthews said:
Prodip,

Thanks for the quick reply. I will look at giving this a try but I think
that it may still have the same problem when users return to an earlier page,
would the first page class still be available at this point, if not I will
get the same problem I have now.

Simon.
public [shared] properties on the First page (where the search button
located) and call them from the second page. In ASP.NET every form
assocaited with a class and it can be called from any other classes. This
way we don't have to use query string like we had to use in asp
applications.what criteria do you bring back from second page to first page? One way to
accomplish this is - exposing public [shared] properties on second page and
call them from the first page.
 
I have implemented a query string but went for the advice from bruce on using
it to save a key to a session storing the string. When I go from one page I
save the criteria to a session variable based on a random (or other) number.
So I have:-

http://www.mysite.com?Search=27611

I then have :-

SearchData = (SearchDataStruct) Session["27611"];

in the second page to read out the data.

Thanks for the help.
 
Simon,
I use Session to store my search criteria classes all the time.
Then on the results page I pull the criteria out of session, and use it to
build the SQL command to get the results which are displayed in the grid.

If the user retunrs to the Search page, I pull the crtieria class out of
session and populate the search controls with the data they last entered.
They can then change some settings or push the Clear Form button to re-set
all the values to their defaults.

This sounds exactly like what you are trying to do (except you use a
Structure instead of a class which is not very different.)

I guess I don't see what the problem is that you are having. This technique
works well for me.
Can you please elaborate a bit?
 
Joe,

I was doing this but storing the data in a 'known' session variable. The
problem I got was when a user completes two searches of the same type (ie.
same asp pages) but different criteria. As I was storing the criteria in the
same variable it caused problems if the user pressed the back button and
returned to an earlier page.

However with advice from Bruce & Scott I have stored the criteria structure
in the session but referenced with a key which is passed in the query string
rather than a set variable name.

So I have:-

http://www.mysite.com?Search=27611

I then have in the second page to read out the data.
:-

SearchData = (SearchDataStruct) Session["27611"];

This seems to work okay but i now have a concern over building up a lot of
redundant data over time in the session. As this is a intranet application
sessions could be quite long and large.

Simon.
 
Back
Top