Use form and querystring collection

xiaoxiao2021-03-06  66

When the user fills in the page

content, or the value after the browser address bar is entered in the URL, and the FORM and QueryString collection are used to the ASP script. This is a simple way to access the value in the ASP code.

1. Access the general technology of the ASP collection

Most ASP collections are similar to those of the general set seen in VB. In fact, they are arrays of the value, but can be accessed by using a text string key (for size is not sensitive) and a integer index. Therefore, if the containing the client web page is as follows:

Firstname:

Lastname:

You can access the values ​​within its control by accessing the ASP's FORM collection:

StrfirstName = Request.form ("firstname")

StrlastName = Request.form ("Lastname")

You can also use the integer index of the control in the form, and the range of index starts from the first defined control in the HTML and then sorted in the defined order:

StrfirstName = Request.form (1)

StrlastName = Request, form (2)

However, this technique is not recommended in the technology that is integrated into indexes, because once there is a change in HTML, or insert a new control, the ASP code will get the wrong value. Further, for those who read the code, it is easy to confuse.

1) All values ​​for access collection

You can turn a series of values ​​on the entire Form into a single character variable by reference collection, without providing a key or index.

StrallFormContent = Request.form

If the text box contains value Priscilla and Descartes, the request.form statement will return the following character:

Firstname = Priscilla & LastName = Descartes

Note that the supplied value is in the form of a name / value (ie, the control name = control value), and each pair of name / values ​​are separated by symbol "&". If it is intended to deliver the content in the form, it is useful to perform an executable application or DLL of the standard format that wants to get the value. However, generally, through the contents of the collection by using the name of the control in the form.

2) Traversing an ASP collection

There are two ways to traverse all members in an ASP collection, and the way is basically the same as the normal VB collection. Each collection provides a count property that returns the number of entries in the collection. You can use the count attribute by using a integer index.

For intloop = 1 to request.form.count

Response.write Request.form & "
"

NEXT

If the previous form contains two text boxes of Priscilla and Descartes values, they will get the following results:

Priscilla

Descartes

However, a better way is to use for Each ... NEXT structure.

For Each Objitem in Request.form

Response.write objitem & "=" & required.form (objitem) & "
" Next

This has the advantage that it can access the name of the control and access it. The above code will result in the following results:

Firstname = Priscilla

Lastname = descartes

Note that some browser returns to the value of the ASP may be the same as the order displayed on the page.

3) Multi-value

In some cases, each member in the ASP collection may more than one value, which occurs when there are several controls in the HTML definitions have the same NAME properties. E.g:

In the Form collection, an entry will be created for "OtherHobby". However, it will include the values ​​obtained from the three text boxes. If the user left one or more when submitted, the returned value is an empty string. If the user enters Gardening and Mountaineering, the second text box is empty, and the REQUEST.FORM ("OtherHobBy") is accessed in our ASP code, and the string will return:

Gardening, Mountaineering

In this case, access a single value, you can use complex code:

For Each Objitem in Request.form

If Request.form (Objitem) .count> 1 Then 'More Than One Value In 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 & "=" & required.form (objitem) & "
"

END IF

NEXT

This will return to the previous form instance containing three OtherHobby controls.

Otherhobby:

Subkey 1 value = gardening

Subkey 2 Value =

Subkey 3 value = mountaineering

However, this technique is rarely used because of the same name, the same name very little.

a) radio or page button control in HTML

In HTML, the case where the same Name property that needs to be given to several controls is a radio (or option) button, for example:

I Live in:

American
europe

asia

Because users can only select one of multiple items (this is why the same name is given), it will only be returned, and the browser can only send the value of the selected control. Therefore, if the user of this form has chosen "Europez", this entry will be obtained by traversing the Form set:

Country = EU

Since each control provides a different Value property, 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", so you will get:

Country = on

This is not often used, so the VALUE property is usually used for radio controls using the same name.

b) HTML checkbox control

When an HTML source code contains a check box control, the only name is generally given, for example:

I Enjoy:

Reading

Eating

Sleeping

In this case, when the form is submitted, if only the first and third check boxes are selected (marked), the following values ​​are obtained when traversing the FORM collection:

Reading = on

Sleeping = on

However, if a value is provided for each check box, the value is sent to the server instead of the string "ON". For example, the form is as follows:

If except the third check box, all submits, in the Request.form collection, the following results are generated:

Hobby = hobby025, hobby003, hobby010

If you write more complex collection traversal code, as previously described (separately displaying each subkey), you get the result:

It is necessary to pay attention to both cases, and no selected controls are not returned at all. In the first case, there is no spoofing comma, and there is no null value. This is not the same as the results of the above text boxes. When using the text box, each text box returns a value, even an empty string. This is the result of this browser causes this. Therefore, when accessing a collection in the ASP code, pay attention to this problem.

The above situation A tricky negative effect is when using the check box, the index of the check box value is not contacted in the position of the control in the original HTML, and the number of sub-keys of the fourth check box in the above example is 3 Since the second control is not selected when the form is submitted. c) HTML list control

The , , , ,