Requesting and Presenting Values
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…
Casale Media: 728x90 (Banner) DO NOT MODIFY
Requesting and Presenting Values#
A Request From a Form#
Introduction#
Once a user has finished entering information into a form, those values can be sent to you. To enable the retrieval of these values, the .NET Framework offers the HttpRequest class, which is accessible through an object named Request. This object allows you to identify a specific item and obtain its value. To perform this action, use the name of the Request object followed by square brackets. Inside the brackets, provide the name of the control as a string.
Request.Form(Object).OptionalIndex.CountThe 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:
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.CountThe 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#
While most web pages are designed solely to show information, others—particularly interactive ones—allow users to provide data. When a visitor submits a value through a web form, that value must be processed by the server to be useful. This often involves taking the submitted information and either storing it in a database or displaying it back to the user.
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:
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:
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#
After a value is prepared, you can transform it into a string format for display on a web page. As we discussed previously, anything a user enters via the keyboard is initially treated as a string. However, if the value needs to represent a different type, such as a number or a date, it must be converted before it can be used for calculations or specific logic.
<%@ 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.45e 5 | |
| 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>Casale Media: 336x280, 300x250 (Rectangle) DO NOT MODIFY Casale Media: 160x600, 120x600 (Tower) DO NOT MODIFY Previous Copyright © 2009 FunctionX Home DynamiContext AdLinks DynamiContext AdLinks

