Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17


"Redirect" with form POST data possible?

Posted on 2004-08-25
Medium Priority
Last Modified: 2011-08-18
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.

Question by:jhance
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +5

Expert Comment

ID: 11894028
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.
LVL 16

Accepted Solution

Walter Ritzel earned 1000 total points
ID: 11894428
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.


LVL 18

Expert Comment

ID: 11894605
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 =)
Learn how to optimize MySQL for your business need

With the increasing importance of apps & networks in both business & personal interconnections, perfor. has become one of the key metrics of successful communication. This ebook is a hands-on business-case-driven guide to understanding MySQL query parameter tuning & database perf


Expert Comment

ID: 11895081
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.


Expert Comment

ID: 11895277
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


Expert Comment

ID: 11896088
well all of the above solutions should work in
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
LVL 32

Author Comment

ID: 11897975
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.
LVL 32

Author Comment

ID: 11898000
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...
LVL 16

Expert Comment

by:Walter Ritzel
ID: 11898081
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).

LVL 32

Author Comment

ID: 11905351

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.
LVL 16

Expert Comment

by:Walter Ritzel
ID: 11905414
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();

LVL 32

Author Comment

ID: 11908673

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...

Expert Comment

ID: 13151471
Is the same thing possible for classic asp?
Fahd Murtaza

Expert Comment

ID: 13342866
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)

Expert Comment

ID: 13342922
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.


Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
This video teaches users how to migrate an existing Wordpress website to a new domain.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question