Go Premium for a chance to win a PS4. Enter to Win


"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
  • 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 =)
Industry Leaders: 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!


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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
CTAs encourage people to do something specific to show interest in your company, product or service. Keep reading to learn why CTAs should always be thought of as extremely important, albeit small, sections of websites.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

885 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