ASP.NET

Requesting a Value

After a user has finished preparing values in a form, he or she can send them to you. To allow you to get values from the user, the IIS's own library…

3 min read560 words5 code samples

ASP.NET Tutorials: Value Request

Requesting a Value#

A Request From a Form#

Introduction#

Users can submit data to you after they have entered values into a form. The Request object, part of the IIS library, is used to capture these user inputs. Any form on a web page is treated as a property of this Request object. The individual elements within the form are part of a collection where each item is an accessible member. You can retrieve a specific object from this collection using the indexed Request.Form() property. For instance, you can put the control's name in quotes inside the parentheses. When using Request.Form(), the data must be sent via the POST method. To access a form control, you would use this syntax:

C#
Request.Form(Object).OptionalIndex.Count

The object you need to access can be entered as the Object part. For example, you can enter the name of a form's control as "Object". This can be done as follows:

C#
Request.Form("txtFirstName")

In our lessons about arrays and collections, we will review what roles the OptionalIndex factor and the Count value play. For now, we will ignore them.

A String Request#

In addition to, or instead of, gathering data through form controls, you can also request specific strings. The Request object provides the QueryString collection for this purpose, allowing you to retrieve values from controls or directly from the browser's address bar. Keep in mind that using the GET method for a form causes its data to be visible in the address bar during transmission:

Notice that the section on the right side of the question mark includes parts with the formula Name=Value. This indicates that the section on the right side of the question mark is in fact a collection of strings. Each string in this collection can be produced to you by Request.QueryString. The syntax of Request.QueryString is:

C#
Request.QueryString(Variable).OptionalIndex.Count

The Variable parameter is the name of the string that you want to retrieve. It can be the name of a control passed as argument. Here is an example:

C#
Request.QueryString("txtFirstName")

It can also be the name of a variable. In our lessons about arrays and collections, we will review what roles the OptionalIndex factor and the Count value play. This also means that, as opposed to the Request.Form collection that uses the POST value of the method attribute of a form, when using the Request.QueryString property, you should send values using GET.

HTML Encoding#

Introduction#

While most web pages focus on displaying information, interactive sites enable visitors to submit their own data. This is typically done by entering text, choosing radio buttons, or selecting checkboxes, which creates values on the client side in the browser. Once the user submits these values, you can receive them on the server. To facilitate this process, the .NET Framework offers the HttpUtility class.

Encoding a Value#

When a web page visitor types a value or selects from a control, the value must be analyzed and made into a readable type. To make this possible, the value must be encoded. To support this operation, the HttpUtility class is equipped with a method named HtmlEncode. The value must be passed in the parentheses of HttpUtility.HtmlEncode(). This value would be the one you get from a call to Request.QueryString(ControlName). This means that you can pass the Request.QueryString(ControlName) expression to the HttpUtility.HtmlEncode() call. This would be done as follows:

C#
<%@ Page Language="C#" %>
<html>
<head>

<style>

body {
    font-family: Verdana, Helvetica, Arial, 'Sans Serif';
    color: #000000;	
    font-size: 10pt;
    background-color: #FFFFFF }

hr { color=#000080 }

.toptitle {
  color: #000080;
  font-family: 'Times New Roman', Garamond, Georgia, Serif;
  text-align: center;
  font-size: 24pt;
  font-weight: bold;
  text-decoration: none }

.housetitle {
  color: #0000FF;
  font-family: Georgia, Times New Roman, Courier New;
  font-size: 16pt;
  font-weight: bold;
  text-decoration: none }

.names {
  font-family: Verdana;
  font-size: 10pt }

</style>

<title>Grier Summer Camp</title>
</head>
<body>

<p class='toptitle'>Grier Summer Camp</p>

<p class='housetitle'>Registration</p>

<p>Please use this form to register.</p>

<form action="index.aspx" method="get">
 <table border="0" width="320">
    <tr>
      <td width="100" class="names">First Name:</td>
      <td>
	<input type="text" name="txtFirstName" size="10"
          value="<%=HttpUtility.HtmlEncode(Request.QueryString["txtFirstName"])%>">
      </td>
    </tr>
    <tr>
      <td width="100" class="names">Last Name:</td>
      <td>
	<input type="text" name="txtLastName" size="10"
          value="<%=HttpUtility.HtmlEncode(Request.QueryString["txtLastName"])%>">
      </td>
    </tr>
    <tr>
      <td width="100" valign="top" class="names">Full Name:</td>
      <td>
	<input type="text"
	       name="txtFullName"
               size="21"
	  value="<%=HttpUtility.HtmlEncode(Request.QueryString["txtFirstName"]) +
	    " " + 
	    HttpUtility.HtmlEncode(Request.QueryString["txtLastName"])%>">
        <input type="submit" value="Submit it">
      </td>
    </tr>
  </table>
</form>

<table border="0" width="100%" cellspacing="0" cellpadding="0">
  <tr>
    <td width="100%">
      <hr color="#FF0000">
    </td>
  </tr>
  <tr>
    <td width="100%"
        align="center"
        style="font-family: Verdana; font-size: 10pt">
		Copyright © 2009 Grier Summer Camp
    </td>
  </tr>
</table>

</body>
</html>

Home Copyright © 2009 C# Key