Passing values between pages

ASP.NET 1.1

There are three widely used methods of passing values from one page to another in ASP.NET 1.1.

1. Using Query String

We usually pass value through query string of the page and then this value is pulled from Request object in another page.

FirstForm.aspx.cs
Response.Redirect("SecondForm.aspx?Parameter=" + TextBox1.Text);

SecondForm.aspx.cs
TextBox1.Text = Request. QueryString["Parameter"].ToString();

It's pretty straightforward! Only problem with this method is that we need to pass value under the constraint of maximum characters (2083 characters) allowed in the browser URL. If you want to pass string larger then that your application will throw exception. This is reliable only when you are passing values like integer or short string. If parameter is larger than maximum URL length or if you are not sure about its length, then this is not preferred method to pass value.

Another problem with this method is that user can actually see these passing parameters and its value in address bar. This will further require to validate each parameters after pulling it from Request object.

You also have to take care of any special characters in the value while passing through query string. To address that, you should also encode the value before passing it to next page. And likewise, you also have to decode the value after pulling from Request object. Our code snippet of pulling passed value will be something like this:

FirstForm.aspx.cs
Response.Redirect("SecondForm.aspx?Parameter=" + Server.UrlEncode(TextBox1.Text));
SecondForm.aspx.cs
TextBox1.Text = Server.UrlDecode(Request.QueryString["Parameter"].ToString());

This approach is very handy if you have to pass some integer like values, for example like employee id, order id etc.

2. Using Context Object

Passing value through context object is another widely used method.

FirstForm.aspx.cs 
TextBox1.Text = this.Context.Items["Parameter"].ToString();

SecondForm.aspx.cs 
this.Context.Items["Parameter"] = TextBox1.Text; Server.Transfer("SecondForm.aspx", true);

Note that we are navigating to another page using Server.Transfer instead of Response.Redirect.

One of the major benefits of using Context object for passing values is that passed value will not visible to the user through the address bar. This means developer needs not to worry about validating parameter values after pulling from Context object. There is also no limitation of size of parameter passed using Context object as it method does not depend upon URL of page.

Some developers also use Session object to pass values which I never prefer. In Session based method, value is store in Session object and then later pulled out from Session object in Second page.

3. Posting form to another page instead of PostBack

Third method of passing value is by posting page to another form, like what we used to do in old ASP age. Here is the example of that:

FirstForm.aspx.cs 
private void Page_Load(object sender, System.EventArgs e)
{
    buttonPassValue.Attributes.Add("onclick", "return PostPage();");
}

And we create add a Javascript function to post the form.

FirstForm.aspx
<code><script language=javascript>
function PostPage()
{
    document.Form1.action = "SecondForm.aspx";
    document.Form1.method = "POST";
    document.Form1.submit();
}
</script>

SecondForm.aspx.cs
TextBox1.Text = Request.Form["TextBox1"].ToString();

In above example, we are posting the form to another page. And then pulling value of controls of previous page through Request object. NOTE: You might get error "The viewstate is invalid for this page and might be corrupted." in second page using this method. Workaround to handle this error is to put EnableViewStateMac=false in Page declaration of form

SecondForm.aspx 
<%@ Page language="c#" Codebehind="SecondForm.aspx.cs" AutoEventWireup="false" Inherits="WebApplication17.SecondForm" EnableViewStateMac=false%>

ASP.NET 2.0

In ASP.NET 2.0, Microsoft has solved this problem by adding PostBackURL property of the control for cross page post back. Implementation is just a a matter of setting a property of control and that's all.

FirstForm.aspx
<asp:Button id="buttonPassValue" style="Z-INDEX: 102;" runat="server" Text="Button" PostBackUrl="~/SecondForm.aspx"></asp:Button>

SecondForm.aspx.cs 
TextBox1.Text = Request.Form["TextBox1"].ToString();

In above example, we are assigning PostBackUrl property of the button and we provide Url of the target page. In target page, we will have access of all controls of previous page using Request object.

You can also use PreviousPage object to access previous page controls instead of using classic Request object.

SecondForm.aspx.cs 
TextBox textBoxTemp = (TextBox) PreviousPage.FindControl("TextBox1"); TextBox1.Text = textBoxTemp.Text;

This is much simpler and clear implementation to pass values between pages.

Passing values between pages is common but sometime tricky implementation in web application. As we have discussed many of the mechanisms above but I always prefer and recommend to use posting form to another page than other methods in in ASP.NET 1.1 based applicationFor ASP.NET 2.0, I prefer to using PostBackURL for its clean implementation.