ASP.NET Redirect With Form Values

Tags: Technology, ASP.NET, Response, Redirect, Form Values

As a web developer, every once in a while the need arises to redirect a user to a page as well as have the destination page know something about what happened on the previous page.

In the world of ASP.NET Web Forms, this can be difficult due to most everything relying on the "Post Back Model".

So what are the ways you can redirect a user to a page, and let that page know a little more about the user?

1) Cookie - You can set a cookie, and expire it on the destination page. I don't like this because cookies are messy, and unless you're going to persist that cookie for more than one request, it's not really worth having cookie management for just that one redirect.

2) Session - You could use built-in ASP.NET session, but like cookies, session is meant for values that are longer lived (the whole session), and you would have to manage that session variable as well.

3) Query String - You could just tack on "?status=pending" to your url, but chances are you don't want the user to copy that url and paste it to his friends, therefore tricking the destination page to think that everyone has that same "status".

4) Post values - You can't redirect AND send form values from your server code to your destination page. It just doesn't work like that. Response.Redirect() only sends a redirect message to the client that the page they are asking for is temporarily found at another destination.

So how do you pass form values to a destination page that you want to redirect your user to?

Like this:


public void RedirectWithFormValues(string redirectURL, Dictionary<string, string> formValues)
{
	//note: The weird use of string concatenation here is mostly for readability

	string redirectFormHTML = @"
<html>
<head></head>
<body onload=""document.getElementById('redirectForm').submit()"">
	<form id=""redirectForm"" action=""{0}""  method=""post"">{1}</form>
</body>
</html>";

	System.Text.StringBuilder sbFormValues = new System.Text.StringBuilder();

	foreach (KeyValuePair<string, string> formValue in formValues)
	{
		sbFormValues.Append(String.Format("<input type=\"hidden\" name=\"{0}\" id=\"{0}\" value=\"{1}\" />", formValue.Key, formValue.Value));
	}

	Response.Clear();
	Response.Write(String.Format(redirectFormHTML, redirectURL, sbFormValues.ToString()));
	Response.End();
}

This method allows you to send form values to a page that you want to redirect the user to. It works much like Response.Redirect(), in that the user's browser has to ask twice. But in this way, instead of sending a redirect response, it sends a normal response on it with nothing but a form, some values, and some javascript that automatically submits the form to the desired page.

Usage of the above method looks like this:


Dictionary<string, string> formValues = new Dictionary<string,string>();
formValues.Add("test", "I passed this value along!");
this.RedirectWithFormValues("destination.aspx", formValues);

Disadvantages:

1) There is a little bit of a client delay on the users browser. In cases of slow internet connections or client computers, this delay can be a little bit longer than desired for a simple redirect.

2) Back button sadness. If you try to use the back button, you will just be redirected back to the destination page.

3) Refresh button sadness. If you refresh the destination page, it will warn you that your refresh will resubmit the form, which can be confusing to users who may not have filled out a form prior to the redirect.

As with any complicated, rule bending fix... There definitely are better ways to accomplish this. Those usually involve rethinking your user experience to work better with your tool set, and be more straight forward with the user. But as all of you developers know, time is always short, and sometimes these solutions are necessary. We can always go back and refactor, right? :P

Add a Comment