Summary
For
Expressions make use of operators, which are the symbols (or
Control-flow
statements, such as branching statements and
|
Chapter 3. Request ObjectsIn this chapter
The first two chapters
In this chapter, you'll see how to use the ASP.NET
Request
object. This important object runs on the server and you use
As you've already seen, VB, C#, and JScript inside ASP.NET pages enable the server to modify content that is seen in the Web browser. This enables you to collect and display values such as the current date and time and the values of various HTTP
Altering HTML content based on things such as date and time is dynamic, but not active. Visitors to a Web site that simply displays dynamic data based on the date, time, or other variables are passive receptors of information. Their only input to the information
Traditionally, Web browsers have communicated with servers through forms, where information was sent to a server CGI program. The program could then examine this information and produce a new HTML document on the fly to be sent back to the requesting client.
Alternatively, in some situations, the page might have contained
In both of these cases, ASP.NET enables you to collect information that you can use in your code. |
FormsThe information stored in the Request object collections originates from the client, and is passed to the server as part of the HTTP document request. The server decodes all this information, and makes it available to ASP.NET through the collections, which are part of the Request object's interface. Apart from the normal information contained in the HTTP header portions of the request, one way that the browser can send specific information to the server is with a form. This can be seen in the referring HTML page with a <FORM> tag. Using a TextBox in a Form
The following example asks users to enter their
<form method="POST" action="TextBoxResult.aspx"> Name: <input type="text" name="Name" size="20"><br> Address: <input type="text" name="Address" size="20"><br> <input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"> </form>
Note This code is part of the page that you can find at www.UsingASP.net, selecting Chapter Examples, Chapter 3, then TextBox . You can also see the page in Figure 3.1. Figure 3.1. This form enables users to enter two fields.
Notice that the <FORM> tag has an action attribute that specifies a URL. The browser goes to this URL when the Submit button is clicked. The form information arrives as part of the HTTP header, and is easily accessible by the Request object. To get information from a form field, simply declare a string variable into which the information will be placed, and retrieve the string data with a Request.Form() method call. The following code shows how to obtain the string contained in the Name field of the form: Dim strName as String strName = Request.Form( "Name" )
The entire code that gets the name and address string data, and displays it in the outgoing document, can be seen in the code fragment that
<%=strName%> you will know that it is taking the contents of the strName variable and sending it out in the HTML document. <% Dim strName as String Dim strAddress as String strName = Request.Form( "Name" ) strAddress = Request.Form( "Address" ) %> The name that was submitted was: <font color="red"> <%=strName%></font><br> The address that was submitted was: <font color="red"> <%=strAddress%></font><br>
Note This code is part of the page that is invoked by filling out the form shown in Figure 3.1 and clicking on the Submit button. You can see the rendered page in Figure 3.2. Figure 3.2. Here, you can see the data obtained from Request.Form() .
Using a <SELECT> Tag in a Form
Many times, rather than offer users an editable text box, you need to give them a list of predetermined selections. A
<SELECT>
tag indicates the HTML construct that does this. Within the
<SELECT>
tag are the choices,
<form method="POST" action="SelectResult.aspx">
Flavors:
<select name="Flavors">
<option value="Chocolate">Chocolate</option>
<option value="Vanilla">Vanilla</option>
<option value="Strawberry">Strawberry</option>
</select><br>
Containers:
<select name="Containers">
<option value="Cone">Cone</option>
<option value="Cup">Cup</option>
<option value="WaffleCone">Waffle Cone</option>
</select>
<input type="submit" value="Submit" name="B1">
<input type="reset" value="Reset" name="B2">
</form>
This code has two
<SELECT>
tags, each with a different name. One is Flavors and the other Containers. The page that receives this form uses these
Note This code is part of the page that you can find at www.UsingASP.net, selecting Chapter Examples, Chapter 3, then Select. You can see the page in Figure 3.3. Figure 3.3. Two Select objects offer users their choices.
Retrieving the data from the named form select items is the same as retrieving data from a TextBox . Just declare a string variable, and get the data using a Request.Form() method call. The following code shows the code that gets the two selections and sends them out in the HTML document: <% Dim strFlavor as String Dim strContainer as String strFlavor = Request.Form( "Flavors" ) strContainer = Request.Form( "Containers" ) %> The flavor that was submitted was: <font color="red"> <%=strFlavor%></font><br> The container that was submitted was: <font color="red"> <%=strContainer%></font><br>
Note This code is part of the page that is invoked by filling out the form shown in Figure 3.3 and clicking on the Submit button. You can see the rendered page in Figure 3.4. Figure 3.4. The data obtained from Request.Form() .
Using a Check Box in a FormTo offer users a choice that's either on or off, a check box is a good tool. An <INPUT TYPE="CHECKBOX"> tag indicates a choice in HTML code. This tag has attributes with which you can specify its name and its value. When they are selected and submitted, they contain the value that's specified in the Value attribute. For example, the following check box: <input type="checkbox" name="Cup" value="ON">Cup<br> if selected, will contain the string data "ON" when retrieved with the Request.Form() method as follows: Dim strStringData as String strStringData = Request.Form( "Cup" ) ' If selected, strStringData will contain the text "ON"
The following example offers users two categories, each of which contains three choices. Because the choices are
<form method="POST" action="CheckBoxResult.aspx"> Check the flavors that you enjoy:<br> <input type="checkbox" name="Chocolate" value="ON">Chocolate<br> <input type="checkbox" name="Vanilla" value="ON">Vanilla<br> <input type="checkbox" name="Strawberry" value="ON">Strawberry<br> Check the containers that you enjoy:<br> <input type="checkbox" name="Cone" value="ON">Cone<br> <input type="checkbox" name="Cup" value="ON">Cup<br> <input type="checkbox" name="WaffleCone" value="ON">Waffle Cone<br> <input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"> </form>
Note This code is part of the page that you can find at www.UsingASP.net, selecting Chapter Examples, Chapter 3, then Check Boxes. You can see the page in Figure 3.5. Figure 3.5. Two sets of check boxes offer more flexible choices.
The page that receives and acts on the form data is shown
<% If Request.Form( "Chocolate" ) = "ON" Then %> You like chocolate<br> <% End If If Request.Form( "Vanilla" ) = "ON" Then %> You like vanilla<br> <% End If If Request.Form( "Strawberry" ) = "ON" Then %> You like strawberry<br> <% End If %> <hr> <% If Request.Form( "Cone" ) = "ON" Then %> You like cones<br> <% End If If Request.Form( "Cup" ) = "ON" Then %> You like cups<br> <% End If If Request.Form( "WaffleCone" ) = "ON" Then %> You like waffle cones<br> <% End If %>
Note This code is part of the page that is invoked by filling out the form in the example shown in Figure 3.5 and clicking on the Submit button. You can see the rendered page in Figure 3.6. Figure 3.6. Here are the results of the user selection.
Using Radio Buttons in a Form
It's often necessary to create forms that contain multiple groups of
Take, for example, the following HTML code: <input type="Radio" Name="Gender" Value="Female"> Female <input type="Radio" Name="Gender" Value="Male"> Male Now use the following code to retrieve the data in the page to which the submitted form goes: Dim strStringData as String StrStringData = Request.Form( "Gender" )
The
strStringData
variable contains
"Female"
if the Female radio button was selected, and
"Male"
if the Male radio button was selected. Note that radio buttons are grouped together by the
Name
attribute. Within the
The following HTML code shows two groups of radio buttons, each of which offers three choices: <form method="POST" action="RadioButtonResult.aspx"> <p>Flavors:<br> <input type="radio" value="Chocolate" checked name="R1">Chocolate<br> <input type="radio" value="Vanilla" name="R1">Vanilla<br> <input type="radio" value="Strawberry" name="R1">Strawberry</p> <p>Containers:<br> <input type="radio" value="Cone" checked name="R2">Cone<br> <input type="radio" value="Cup" name="R2">Cup<br> <input type="radio" value="WaffleCone" name="R2">Waffle Cone<br> <input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"> </form>
Note This code (which has had the formatting tags removed) is part of the page that you can find at www.UsingASP.net, selecting Chapter Examples, Chapter 3, then Radio Buttons. You can see the page in Figure 3.7.
Figure 3.7. Radio buttons offer
|