Home

Requesting a Value

   

A Request From a Form

 

Introduction

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 provides an object called Request. When a form is created on a web page, that form becomes a property of the Request object.

The objects that are part of the form constitute a collection and each object of the form becomes a member of this collection. To access an object of this collection, you can use Request.Form() (this can be referred to as an indexed property because each object can be accessed using its index). For example, you can enter the name of a control between double-quotes in the parentheses of Request.Form().

If you use Request.Form() to access a control, the information should be collected using the POST value of the METHOD attribute of the form. Based on this, to access any control on the form, you would type:

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:

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

Besides, or as opposed to, collecting values using the controls on a form, you can request a string. To support this, the Request object is equipped with QueryString, which also is a collection. This allows you to get values either from a control or directly from the address bar of the browser. Remember, the GET value of the METHOD attribute of a form causes its information to be displayed in the address bar of the browser when that information is being sent:

Web Form

Notice that the section on the right side of the question mark includes parts using 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:

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:

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

Although most web pages are meant only to display values, some others, mostly interactive pages, allow a visitor to submit values. To do this, a visitor may type in a text box, select a radio button, or click a check box. These actions create a value on the browser. The browser is referred to as a client because this is where the visitor creates a value. After formulating a value, a user would click a button to send the value to you. You get that value on the server.

To assist you with getting a value from a browser, the .NET Framework provides a class named HttpUtility.

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:

<%@ 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