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.
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.
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.
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>
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:
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.
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:
In this cases:
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> |
Requesting and Presenting Values
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment