The value that is provided when the user fills out the page <FORM> content, or after the URL is entered in the browser's address bar, through the FORM formand querystring collections are used by ASP scripts. This is an easy way to access values in ASP code.
1, access to ASP collection of general technology
Most of the ASP sets are not much different from the common collections seen in VB. In fact, they are arrays of values, but can be accessed by using a text string key (insensitive to size) and an integer index. So, if the client Web page contains the following <FORM>:
<form action= "show_request.asp" method= "POST"
Firstname:<input type= "TEXT" name= "FirstName"
Lastname:<input type= "TEXT" name= "LastName"
<input type= "SUBMIT" value= "Send"
</FORM>
You can access the values within the control by visiting the ASP's form collection:
strFirstName = Request.Form ("FirstName")
strLastName = Request.Form ("LastName")
You can also use the integer index of a control in a form, which starts with the first defined control in HTML and then sorts the order by definition:
strFirstName = Request.Form (1)
strLastName = Request.Form (2)
However, this type of integer-indexed technique is deprecated, because once a control in HTML has changed or a new control is inserted, the ASP code gets the wrong value. Further, it is very easy to confuse people who read the code.
1 access to the full value of the collection
You can use a reference collection to change a series of values on a form to a single character variable without providing a key or index.
Strallformcontent = Request.Form
If the text box contains values Priscilla and Descartes, the Request.Form statement returns the following characters:
Firstname=priscilla&lastname=descartes
Note that the supplied value is in the form of a name/value pair (that is, the control name = control value), and each pair of name/value is delimited by the symbol "&". This technique is useful if you intend to pass content from a form to an executable application or DLL in a standard format that you want to value. Generally, however, the contents of the collection are accessed by using the name of the control in the form as the text key.
2) traverse an ASP collection
There are two ways to traverse all members of an ASP collection in the same way as a common VB collection. Each collection provides a Count property that returns the number of entries in the collection. You can use the Count property to traverse by using an integer index.
For Intloop=1 to Request.Form.Count
Response.Write Request.Form (intloop) & "<BR>"
Next
If the previous form contains two text boxes for Priscilla and Descartes values, you will get the following result:
Priscilla
Descartes
However, a better approach is to use the for each ... Next structure.
For each objitem in Request.Form
Response.Write Objitem & "=" & Request.Form (objitem) & "<BR>"
Next
The benefit is that you can access both the name of the control and its value. The above code will get the following results:
FirstName = Priscilla
LastName = Descartes
Note that some browsers may return to the ASP's <FORM> values in a different order than they appear on the page.
3) The multivalued nature of the members of the set
In some cases, the individual members of the ASP collection may have more than one value, which occurs when several controls in the HTML definition have the same name property. For example:
<form action= "show_request.asp" method= "POST"
<input type= "TEXT" name= "Otherhobby"
<input type= "TEXT" name= "Otherhobby"
<input type= "TEXT" name= "Otherhobby"
<input type= "SUBMIT" value= "Send"
</FORM>
In the Form collection, an entry is created for "Otherhobby". However, it will include the values obtained from the three text boxes. If the user leaves one or more blank at the time of submission, the returned value is an empty string. If the user enters gardening and mountaineering separately in the first and third text boxes, and the second text box is empty, access to Request.Form ("Otherhobby") in our ASP code will return the string:
Gardening, mountaineering.
In order to be able to access a single value in this case, you can use more complex code:
For each objitem in Request.Form
If Request.Form (objitem). Count >1 Then ' more than one value on this item Response.Write objitem & ":
"
For intloop = 1 to Request.Form (objitem). Count
Response.Write "Subkey" & Intloop & "value =" & Request.Form (Objitem) (intloop) & "
"
Next
Else
Response.Write Objitem & "=" & Request.Form (objitem) & "
"
End If
Next
For the previous form instance that contains three Otherhobby controls, this returns:
Otherhobby:
subkey 1 value = Gardening
Subkey 2 value =
Subkey 3 value = Mountaineering
However, this technique is rarely used because it rarely gives the same name to multiple text boxes.
A The radio or select Page button control in HTML
In HTML, a radio (or option) button is the case for the same name property that you want to give several controls, such as:
<form action= "show_request.asp" method= "POST"
I Live in:
<input type= "RADIO" name= "Country" value= "AM" >america <BR>
<input type= "RADIO" name= "Country" value= "EU" >europe <BR>
<input type= "RADIO" name= "Country" value= "as" >asia <BR>
<input type= "SUBMIT" value= "Send"
</FORM>
Because users can only select one of multiple items (which is why they give them the same name), they will only get a return value, and the browser can only send the value of the selected control. Therefore, if the user of the form has chosen "Europez", it will get the entry and get its value by traversing the form set:
Country = EU
Because a different value property is provided for each control, the name of the country or region corresponding to each entry is reflected. If the Value property is omitted, the browser will return the value "on" and therefore will get:
Country = On
This is not often used, so the Value property is generally used for a radio control that uses the same name.
b) HTML check box control
When the HTML source code in a form contains a check box control, it is generally given a unique name, for example:
I Enjoy:
Reading
eating
sleeping
In this case, when you submit a form, if only the first and third check boxes are selected (tagged), the following values are obtained when you traverse the form collection:
Reading = On
Sleeping = On
However, if you provide a value for each check box, send this value to the server instead of the string "on". For example, the form is as follows:
I Enjoy:
_
Swimming
_
Reading
eating
sleeping