Read, With the Name of Your Lord Who Created

Interacting With Get and Post Methods in ASP.NET

Posted by triaslama on May 1, 2008

There are two common ways to pass data from one page to another, using http Get and Post methods. In Get method, data passed to server using URL encoding. So with Get method data added in URL as query string. For more information on how to retrieve query string values, you can read my previous post here. When we use Post method the URL is still intact, because data passed in HTTP body.

In correlation with ASP.NET, retrieving data passed through HTTP Get and Post methods is quite simple. If data passed with Get method we need the following code to retrieve the data:

Page.Request.QueryString[<param>];

If data passed with Post method we need the following code to retrieve the data:

Page.Request.Form[<param>];

Maybe the problem will more suitable addressed with hand in code practice, so let’s try a simple code! I named the HTML file as methods_source.htm:

    <html>
    <head>
    <title>Using Http Get And Post</title>
    </head>
    <body>
    <form id=”frm_get” action=”get_recv.aspx” target=”_blank” method=”GET” >
    <table>
    <tr>
    <td>Name: </td> <td><input type=”text” id=”txtName” name=”name” /></td>
    </tr>
    <tr>
    <td>Address: </td> <td><input type=”text” id=”txtAddr” name=”addr” /></td>
    </tr>
    <tr>
    <td></td> <td><input type=”submit” value=”Send Using Get” /></td>
    </tr>
    </table>
    </form>
    <p />
    <form id=”frm_post” action=”post_recv.aspx” target=”_blank” method=”POST” >
    <table>
    <tr>
    <td>Name 2: </td>
    <td><input type=”text” id=”txtName2″ name=”name2″ /> </td>
    </tr>
    <tr>
    <td>Address 2: </td>
    <td><input type=”text” id=”txtAddr2″ name=”addr2″ /> </td>
    </tr>
    <tr>
    <td></td>
    <td><input type=”submit” value=”Send Using Post” /> </td>
    </tr>
    </table>
    </form>
    </body>
    </html>

Methods_source.htm consists of two HTML forms. First form will pass data within it using GET method, and second form will pass data within it using POST method.
When we click submit button of first form, we will be redirected to get_recv.aspx (as stated in action attribute) in a new window. Click the submit button of second form will bring us to post_recv.aspx (opened in new window too). This is screenshots of our page:

Methods_source.htm

The name and address text boxes of first form will be sent as parameter of query string with the field name are name and addr respectively. The name of query string fields automatically taken from name attribute of each HTML element, so don’t forget to specify the name attribute to make it works! Inside get_recv.aspx we retrieve the query string values using Page.Request.QueryString["name"] and Page.Request.QueryString["addr"]. Here get_recv.aspx:

    <%@ page language=”C#” %>

    <html>
    <head>
    <title>Get: Receiver</title>
    </head>
    <body>
    <table border=”2″ cellpadding=”7″ cellspacing=”2″ >
    <tr>
    <th>Name</th> <th>Address</th>
    </tr>
    <tr>
    <td><% Response.Write(Page.Request.QueryString["name"]); %></td>
    <td><% Response.Write(Page.Request.QueryString["addr"]); %></td>
    </tr>
    </table>
    </body>
    </html>

When I fill name & address textboxes with ‘Muadz’ and ‘Senayan City, Jakarta’ respectively and click the ‘Send Using Get’ button I get the following URL: http:…/getpost/get_recv.aspx?name=Muadz&addr=Senayan+City%2C+Jakarta. The data from methods_source.htm will be showed in tabular format, so this is what we get:

Get_recv.aspx, name and address values taken from query string.

Values of second form sent using POST method therefore the URL still intact, we retrieve name 2 and address 2 using Page.Request.Form[<param>], <param> value taken from the name each of HTML element of second form (Once again don’t forget specify name attribute for name 2 and address 2 textboxes to make it works!). When ‘Send Using Post’ button clicked, values of second form passed to post_recv.aspx. This is code for post_recv.aspx:

    <%@ page language=”C#” %>

    <html>
    <head>
    <title>Post: Receiver</title>
    </head>
    <body>
    <table border=”2″ cellpadding=”7″ cellspacing=”2″ >
    <tr>
    <th>Name</th> <th>Address</th>
    </tr>
    <tr>
    <td><% Response.Write(Page.Request.Form["name2"]); %></td>
    <td><% Response.Write(Page.Request.Form["addr2"]); %></td>
    </tr>
    </table>
    </body>
    </html>

This is screenshots of post_recv.aspx when I fill name 2 and address 2 with ‘Ali’ and ‘Wirogunan, Yogyakarta’ respectively:

Data sent using POST method received by post_recv.aspx and showed in tabular format.

4 Responses to “Interacting With Get and Post Methods in ASP.NET”

  1. M.S. Babaei Says:

    thanks

    it’s totally clear…

  2. julio Says:

    no funciona

  3. george Says:

    thanks man,

    best regards!

  4. Kamalesh Says:

    worthful !!

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>