Interacting With Get and Post Methods in ASP.NET
2012-06-19 10:58
603 查看
Interacting With Get and Post Methods in ASP.NET
Posted by triaslama on May 1, 2008There 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.
相关文章推荐
- Working with Data in ASP.NET 2.0 :: Master Pages and Site Navigation
- ASP.NET 中 POST 数据并跳转页面(译自 Redirect and POST in ASP.NET)
- Upload files in ASP.NET MVC with JavaScript and C#
- ASP.NET form method "post" and "get"
- Upload Files In ASP.NET Core 1.0 (Form POST And JQuery Ajax)
- Redirect and POST in ASP.NET
- Redirect and Post JSON object in ASP.NET MVC
- [转]Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application (3 of 10)
- how to get all controller attribute action and parameter in asp.net mvc
- save cookies and get cookies in system using asp.net
- Customize web.config sections and register your own configuration then get values in asp.net
- securely implement request processing, filtering and content redirection with HTTP pipeline in ASP.NET
- Active Directory Authentication in ASP.NET MVC 5 with Forms Authentication and Group-Based Authorization
- using Silverlight 4 in an ASP.NET MVC 3 application and accessing data with JSON
- ASP.NET 中 POST 数据并跳转页面(译自 Redirect and POST in ASP.NET)
- ASP.NET MVC - Redirect-and-Post-JSON-Object-in-ASP-NET-MVC
- Sorting, Filtering, and Paging with the Entity Framework in an ASP.NET MVC Application
- Methods GET and POST in HTML forms - what's the difference?
- How to get URL and QueryString value in an ASP.NET page
- Multiple Files Upload in ASP.NET and ASP.NET with jQuery