Thursday, December 27, 2007

Passing values using HTTP POST in Asp.Net

By doing HTTP POST to another page

A lot of Asp.Net developers might not be aware of this, but one of the ways of passing values from one page to another is a normal HTTP POST. Before we dive into it, let me ask if you know exactly what this means : "An Asp.Net webform posts back to itself".

Basically, when you click on a button to submit a webform, you are sending that form back to the server. Here, webform means whatever falls between the <form></form> tags on your page. In technologies such as ASP, you can have multiple <form></form> elements on the same web page. When the user posts back the form, the [action] attribute of the form decides to which page the form gets submitted.

For example, suppose you have a form that looks like this :

<form action="PostPage.aspx" id="SimplePageForm" method="Post">
    Please enter text here that you want to see on the next page : 
                <input type="text" name="myText" id="theTxt" />
    <hr />
    <input type="submit" name="btn" id="btnSubmit" />

We basically want to create a form that posts to some other page. For e.g., our current form has action="PostPage.aspx". This means that when you submit the current page, it shall post back to PostPage.aspx page, and not to itself. The current FORM's elements shall be available on this other ASPX page, and can be accessed using the .net Request object.

Let me dwell a little bit more on this. On the current form, you have a textbox with the [name] property set to "myText". When you post this page, the entire form's data posts back to the server as a name-value collection. Meaning, a name-value collection will be sent back to the server, where one entry has name = "myText", and value = whatever value you entered in the textbox. There will be similar name-value pair entries for other controls on the page. As an aside, note that HTML handles the POSTed values differently for different controls. For e.g., for a textbox, the value is set to whatever text you enter in the textbox, whereas for a dropdown list, it will be set to the value that has been selected at the time of postback.

Bear in mind that the form is posted to a different page, i.e., PostPage.aspx, so the name-value collection is accessible on the PostPage.aspx. Instead of providing you with the raw posted values, Asp.Net wraps this information in the Request object. You can access the posted value using Request.Form["myText"]. Again, as an aside ;), if you are using <input type="file"> on your form, i.e., if you are using a file upload control, the story is slightly different. In this case, you can access the path of the files to be uploaded using Request.Files.

Finally, all we need to do in the PostPage.aspx is to access the posted values using the Request.Form collection. Take a look at the source code below, before we move to the final "prescriptive guidance" for this type of solution.

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class A_2_Default : System.Web.UI.Page
    protected void Page_Load(object sender, EventArgs e)
    Response.Write("You typed in : " + Request.Form["myText"]);

As is evident, we just get the posted value for the control named "myText" using the Request.Form collection, and print it out.

The above solution is not feasible if you are trying to pass values between ASPX pages. The simple reason for this being that Asp.Net does not allow <form></form> elements to post to any other form other than itself. However, you can have multiple HTML forms on your webpage (ones without runat="server"). The advantage of doing this is that it allows you to show one/more forms to the user at the same time. Depending upon the user action, one of the form is posted back. This way, each form can have its own "processing page" to which it posts back on each submit. I'm not saying this is the best practice solution, but just that even simple HTTP POST can be handy at times.

No comments: