9.1. Gathering Data from the User

Literally every web site on the Internet has to deal with input from the user. Generally, this input can be sent to the web server with two different techniques, GET and POST. In Chapter 4 you briefly saw the difference between these two methods and saw that GET data is appended to the actual address of the page being requested whereas with the POST method the data is sent in the body of the request for the page.

With the GET method, data is added to the query string of a page. You can retrieve it using the QueryString property of the Request object as discussed in Chapter 7. Imagine you are requesting the following page:

http://www.PlanetWrox.com/Reviews/ViewDetails.aspx?id=34&catId=3

With this example, the query string is id=34&catId=3. The question mark is used to separate the query string from the rest of the address. To access individual items in the query string, you can use the Get method of the QueryString collection:

VB.NET

Dim id As Integer = Convert.ToInt32(Request.QueryString.Get("id"))      ' id is now 34
Dim catId As Integer = Convert.ToInt32(Request.QueryString.Get("catId")) ' catId is now 3

C#

int id = Convert.ToInt32(Request.QueryString.Get("id"));                 // id is now 34
int catId = Convert.ToInt32(Request.QueryString.Get("catId"));           // catId is now 3

The POST method on the other hand gets its data from a form with controls that have been submitted to the server. Imagine you have a form with two controls: a TextBox called txtAge to hold the ...

Get Beginning ASP.NET 3.5: In C# and VB now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.