"Redirect" with form POST data possible?

I'm not sure if this is possible...


I have a FORM with POST data that I'm passing on to an ASPX script for further processing.  I'd like to have the following sequence:

FormPage.htm --- POST--->WorkerPage.aspx --- "redirect" ---> DisplayInfo.aspx

The user inputs some data in the FormPage and presses submit.  This posts data to the WorkerPage where some aspx processing takes place and some data is stored in the DB (or whatever) from SOME of the form data.  Then it needs to automatically go to the DisplayInfo page where some stuff is displayed to the user for print or whatever.

The problem is the "redirect".  I can get to the DisplayInfo page but none of the POST data from the form exists any longer.  I don't know how to automatically fall into DisplayInfo from WorkerPage without a Redirect() call.

If I'm running down a rathole, please feel free to offer alternate suggestions.  I want to do it this way (rather than in the FormPage) since there are multiple "FormPages" feeding into the WorkerPage.  All need the same processing but I don't want multiple copies of the processing code in each of the FormPages.

LVL 32
Who is Participating?

Improve company productivity with a Business Account.Sign Up

Walter RitzelConnect With a Mentor Senior Software EngineerCommented:
If you want to pass the values received from a form to another page, three ways are possible:
- passing trhough session variables: in WorkerPage.aspx, pass the values to session variables (e.g: Session("Cod") = Request("Cod") and so on) and then call the response.redirect;

- passing through querystring, as sugested in comment above

- using HttpContext: this is the best way, I think. HttpContext is similar to session, but it exists only during the first call of the target page. Example:

HttpContext.Current.Items.Add("Cod", Request("Cod"))

Note that I'm not using response.redirect, I'm using Server.Transfer.


When you put your form data into the database, get the identity field of the row you inserted (using the @@identity in SQL).

Then, in your redirect to the DisplayInfo.aspx page, include the @@identity field in the url (DisplayInfo.aspx?id=623 or whatever).

On the DisplayInfo.aspx, use the id from the URL to retrieve the data you need from the database.
Similar to what wpcortes posted, but slightly different...
In PHP, I use code like this:

header("Location: /otherpage.php");

and in otherpage.php:

Or in short, encode the entire set of posted information into the session, do the redirect, and decode the post variables out of the session.  Unfortunately I have no idea if ASP is even capable of something like this.  If so, that should be inspiration for you though =)
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

In your WorkerPage, try this:

Response.Redirect "DisplayInfo.asp?value1=" & Request("value1") & "&value2=" & Request("value2") & "&value3=" & Request("value3")

That will take whatever data is sent via Post to WorkerPage, and send it along to the DisplayInfo page.

Hope this helps.

Here's a further example:

In my HTML form, I have the following fields text-boxes:

In the WorkerPage.aspx, when it comes time to redirect, it would look like this:

Response.Redirect "DisplayInfo.aspx?UserName=" & Request("UserName") & "&Age=" & Request("Age") & "&City=" & Request("City")

Now, the POST info sent to WorkerPage.aspx will be redirected to DisplayInfo.aspx

That example was based on ASP 3.0, but the concept is the same for ASP.NET

well all of the above solutions should work in asp.net
and also u can use data caching and then purge the cache
so mainly u have 5 solutions
1-take the variables and send them as a request variables not request.form
2-put the data in a session variable(better join them with some unique character sequence use one variable to store split in the last page and destroy the session variable)
3-save in db and either send or put in session the id
4-cache the data requested
5-save the data in an xml file on the server hard and make that file name session.sessionid.xml

:D hope this did help
jhanceAuthor Commented:
Thanks all for your suggestions.  I'm still stuck so let me explain what I've tried and what didn't work:

1) Using the DB would be my first thought but it can't be done here.  The DB is essentially "write-only" from the web server.

2) Using "GET" parameters on the redirect URL is pretty unmanagable.  There are about 50 data items to be passed and one is quite long.  This makes for an impossibly long URL.

3) I tried the HttpContext method suggested but didn't have any success.  (BTW, the Microsoft docs on that are downright cryptic!!)  Anyway, I did:

System.Web.HttpContext.Current.Items.Add("TESTVAR", "ABCDEFG")

in my first page to create the data item.  Then I have:

Dim strTest As String
strTest = System.Web.HttpContext.Current.Items["TESTVAR"]

in my destination page.

When run in the browser I get:
Compiler Error Message: BC30311: Value of type 'System.Collections.IDictionary' cannot be converted to 'String'.

Line 283:<%
Line 284:      Dim strTest As String
Line 285:      strTest = System.Web.HttpContext.Current.Items["TESTVAR"]

So maybe this doesn't work or maybe (more likely) I'm just not understanding how to access the data in the HttpContext class.  An example might help here..

4) webtrans suggests "data caching" but I have no clue here how that applies to this situation.  If this is a serious suggestion, please elaborate.
jhanceAuthor Commented:
By the way, the suggestion to use:

Session("XXX") = "DATA"

etc.  does work, so unless someone has a better approach or can debunk this one as a bad idea, I'll probably accept it...
Walter RitzelSenior Software EngineerCommented:
To work the HttpContext solution,
just do this:

strTest = System.Web.HttpContext.Current.Items["TESTVAR"].ToString();

BTW, .ToString() works on C#, but in VB.NET must be a way to do this.



PS: HttpContext is probably the best solution to do this because security issues and memory issues (Session will consume more of server memory, because you must to clear the session to liberate memory, and HttpContext is managed by .NET).

jhanceAuthor Commented:

I'm not getting any success even adding the ".ToString()" to the end.  Error message is:

Object reference not set to an instance of an object.
Walter RitzelSenior Software EngineerCommented:
You must use a server.transfer to change to the second page, and the Context will be available just the first time you access the second page. Any postback in the second page will erase the context.


First Page: A button will transfer to another page:
      private void btnAdd_Click(object sender, System.EventArgs e)

Second Page:
protected override void PageLoad(object sender, EventArgs e)
if (!IsPostBack)
string lScreenMode = HttpContext.Current.Items["ScreenMode"].ToString();

jhanceAuthor Commented:

I was using Server.Transfer() but the error remains.  I'm baffled!  The Session() vars worked just fine so I accepted your answer.  I'll see if I can workout the HttpContext issue some other time...
Is the same thing possible for classic asp?
Fahd Murtaza
I've found the accepted answer is correct but I found fourth way that seems to do it faster but
it involves javascript and more coding.  
give the body of your aspx form an id like body in my case .
<body MS_POSITIONING="GridLayout" id="body">
put "protected System.Web.UI.HtmlControls.HtmlGenericControl body;" in on the .cs page above page_load fuction.
Then you can do something like
body.controls.add(new literalcontrol("<form action='someaspx.aspx' method='post' id='someform' name='someform'>)
body.controls.add(new literalcontrol(<input type=hidden id='someid' value='somevariable'>))
body.controls.add(new literalcontrol(</form>))
I've tried both ways and this way flies while the accepted answer was way slower.
as for fahdmurtaza question I got this idea from classic asp.
I wondering how come the servertransfer takes longer? I'd perfer the servertransfer method
(less coding)
Think i know why it take longer.
server.transfer has to go to server who then transfers to the page.
but in the above way it goes directly to the target page with the info.
jhance check to see the spelling on target page is the same.
unlike request.params[] if the param is not there it returns empty string.
HttpContext.Current.Items[] returns error if it does not find the item you are looking for.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.