Can you maintain session state when using Server.Transfer() ?

D

David Berman

It seems that my site is losing session information when using
Server.Transfer. I have a page called PictureGallery.aspx. It takes an
argument which is an index id, so it would look like
PictureGallery.aspx?id=30 to display gallery 30. In this way I have a
database driven picture gallery. To improve indexing, I put code in
Global.asax to allow me to get to the same page with a url like this:
Pictures_30.aspx. There is no Pictures_30.aspx, the
Application_BeginRequest parses the url, captures it, and calls
Server.Transfer("PictureGallery.aspx?id=30");

However, when I try to access a session variable on the page, I get the
error:
HttpException (0x80004005): Session state can only be used when
enableSessionState is set to true, either in a configuration file or in
the Page directive]

If I go to the page directly, instead of using the Server.Transfer, the
page works fine. Why is this happening? Is there a workaround?

I am using this redirect system to make it as easy as possible to be
indexed by search engines or for people to type in a url to email a
friend to view a picture gallery.

Thank you very much.

David



Meet people for friendship, contacts,
or romance using free instant messaging software! See a picture you
like? Click once for a private conversation with that person!

http://www.SocialNetwork.com
 
S

Steve C. Orr [MVP, MCSD]

Here's a nice, simple way to pass values from one page to another:
(VB.NET code)

'Add data to the context object before transferring
Context.Items("myParameter") = x
Server.Transfer("WebForm2.aspx")

Then, in WebForm2.aspx:

'Grab data from the context property
Dim x as Integer = CType(Context.Items("myParameter"),Integer)

Of course there are a number of ways to pass values from one page to
another, such as using the querystring, cookies, session,
context, saving to a temporary table in the database between each page, etc.
You'll have to decide which technique is best for your application.
Here are several good articles on the subject to help you decide.
http://msdn.microsoft.com/msdnmag/issues/03/04/ASPNETUserState/default.aspx

http://www.aspalliance.com/kenc/passval.aspx

http://www.dotnetbips.com/displayarticle.aspx?id=79
 
D

David Berman

Thanks for your reply. I can see how you can use this mechanism to pass
arguments from one page to another. However, the state information is
missing when I do a transfer. Suppose i use this method to put the
state object into a container as you illustrated, and pass it, can I
then modify the code of the page to assign the context.session variable
to point to the object in the container I passed?

Basically, I know that the page works fine when I execute it as
Page.aspx?argument=xxx but it doesn't work when I do a server transfer
to it, because it says I don't have session state information in that
context.



Meet people for friendship, contacts,
or romance using free instant messaging software! See a picture you
like? Click once for a private conversation with that person!

www.SEN.us
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top