Home

Requesting and Presenting Values

 

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(s) to you. You get that value on the server.

To assist you with getting a value from a browser, the .NET Framework provides an object (in reality, it is a class; we will study classes later on) 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, HttpUtility is equipped with 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="VB" %>
<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="exercise.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>

Here is an example of running the web page:

Request

 
 
 
 

Data Reading

 

Introduction

When interacting with a web page, if it contains a web form that requests some values, the user may be asked to enter them in the available controls. Consider the following web page:

 Browser

In this case, a visitor is expected to provide the length and the height of a rectangle. The visitor would then submit those values to you so you can calculate the perimeter and the area.

Number Request

On a control of a web page, everything the user types is primarily a string. If it represents another type of value, it must be analyzed first and possibly be converted. In Lesson 6, wee saw some of the techniques used to converted a value to the appropriate type. Still, in the .NET Framework, each data type provides a mechanism called Parse. To use Parse, type the data type, followed by a period, followed by Parse, and followed by parentheses. In the parentheses of Parse, type the string that you requested from the user. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Pages As String
    Dim NumberOfPages As Integer

    Pages = "846"
    NumberOfPages = Integer.Parse(Pages)
    Response.Write("This book contains " & NumberOfPages & " pages.")
%>

</body>
</html>

Remember that you can create different delimiting sections. Each subsequent section will continue the previous one. Here are examples:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Pages As String
    Dim NumberOfPages As Integer
%>

<%
    Pages = "1025"
    NumberOfPages = Integer.Parse(Pages)
    Response.Write("This book contains " & NumberOfPages & " pages.")
%>

</body>
</html>

An advanced but faster way to do this is to type the string value in the parentheses of Parse. This has the same effect.

Requesting Dates and Times

Like a regular number, a date can be requested from a control on a web page. After the user has entered or selected a value, to convert it to a Date value, type Date or DateTime, followed by a period, and followed by Parse(). In the parentheses of Parse(), enter the value you got from the control. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Arrival As String
    Dim DateHired As Date

    Arrival = "08/14/1982"
    DateHired = Date.Parse(Arrival)

    Response.Write("Date Hired: " & DateHired)
%>

</body>
</html>

Of course, the date or time value the user entered (or selected) must be valid; otherwise, you would produce an error. Because dates and times follow some rules for their formats, you should strive to let the user know how you expect the value to be entered.

By default, if you request only a date from the user and the user enters a valid date, the compiler would add the midnight value to the date. If you request only the time from the user and the user enters a valid time, the compiler would add the current date to the value.

Formatting Data Display

 

Conversion To String

Once a value is ready, you can convert it to a string and display it on a web page. We mentioned earlier that everything the user types using the keyboard is primarily a string and it's your job to convert it to the appropriate type. In reverse, if you have a value that is not a string, you can convert it to a string. To support this, each .NET Framework data type provides a mechanism called ToString.

To convert a value of a primitive data type to a string, type the name of the variable, followed by a period, followed by ToString().  Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim UnitPrice As Double
%>

<% 
    UnitPrice = 248.95
    Response.Write("Unit Price: " & UnitPrice.ToString())
%>

</body>
</html>

Number Value Formatting

To properly display a number in a friendly and most familiar way, you can format it. Formatting specifies what kind of value you are using and how you want it to be displayed to the user. As it happens, you can display a natural number as a common value or, depending on the circumstance, you may prefer to show it as a hexadecimal value. When it comes to double-precision numbers, you may want to display a distance with three values on the right side of the decimal separator and in some cases, you may want to display a salary with only 2 decimal places.

The System namespace provides a specific letter that you can use in the parentheses of ToString() for each category of data to display. To format a value, in the parentheses, between the double-quotes, type the appropriate letter from the following table:

  Character Used For
  c C Currency values
  d D Decimal numbers
  e E Scientific numeric display such as 1.45e5
  f F Fixed decimal numbers
  g G General and most common type of numbers
  n N Natural numbers
  r R Roundtrip formatting
  x X Hexadecimal formatting
  p P Percentages

Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Number As Integer

    Number = 903746
%>

<% 
    Response.Write("Hexadecimal Format: " & Number.ToString("X"))
%>

</body>
</html>

If you leave the parentheses of ToString() empty, the compiler would use a default formatting to display the value.

Data and Time Formatting

As mentioned earlier, when the user enters a date value for a Date variable, the compiler adds a time part to the value. Fortunately, if you want to consider only the date or only the time part, you can specify this to the compiler. To support this, the Date data type provides a series of letters you can use to format how its value should be displayed to the user. The character is entered in the placeholder of the Date variable after the 0 or the incremental numeric value.

In US English, to express a date value, you can use one of the following formats:

  • MM-dd-yy
  • MM-dd-yyyy
  • dd-MMM-yy
  • dd MMM yy
  • dd MMMM yy
  • MMMM dd, yy
  • dd-MMM-yyyy
  • dd MMM yyyy
  • dd-MMMM-yyyy
  • dd MMMM yyyy
  • MMM dd, yy
  • MMM dd, yyyy
  • MMMM dd, yy
  • MMMM dd, yyyy

In this cases:

  • MM represents a month with 2 digits
  • MMM represents the short name of a month
  • MMMM represents the complete name of a month
  • dd represent a day with 2 digits
  • yy represent a year with 2 digits
  • yyyy represent a year with 4 digits

As you may see, you can start a date with the month or the day. Here is an example:

<%@ Page Language="VB" %>

<html>
<head>

<title>Exercise</title>

</head>
<body>

<%
    Dim Arrival As String
    Dim DateHired As Date

    Arrival = "08/14/1982"
    DateHired = Date.Parse(Arrival)
%>

<% 
    Response.Write("Date Hired: " & DateHired.ToString("MMMM dd, yyyy"))
%>

</body>
</html>
 
 
   
 

Previous Copyright 2009 FunctionX Home