If you submit all except the third check box, the following results are generated in the Request.Form collection:
Hobby = Hobby025, Hobby003, Hobby010
If you write a more complex collection traversal code, as described earlier (displaying each subkey individually), you get this result:
Hobby:
subkey 1 value = Hobby025
subkey 2 value = Hobby003
Subkey 3 value = Hobby010
You need to be aware of both cases where the unchecked control does not return any values at all. In the first case, there is no deceptive comma and the second case has no null value. This is not the same as the result of the equivalent test using the text box above. When you use a text box, each text box returns a value, even if it is an empty string. This is the result of the browser. Therefore, you should be aware of this problem when accessing collections in ASP code.
This is a tricky negative effect when using a check box, the index of the check box value has no connection to the position of the control in the original HTML, in the example above, the number of subkeys for the fourth check box is 3 because the second control is not selected when the form is submitted.
c) HTML List control
The tag in HTML is used to produce a standard drop-down list box whose values are represented in an interesting mixture. The following forms are created with 5 values available for user selection, and because the multiple property is included, you can select not only one entry but the shift or CTRL key when you select it.
programming
swimming
reading
eating
sleeping
This particular case returns a single entry in the form collection that contains the selected value (the Value property specified in a single tag), separated by commas:
Hobby = Hobby025, Hobby003, Hobby010
If you use a more complex set traversal code (each subkey is displayed separately), you will get:
Hobby:
subkey 1 value = Hobby025
subkey 2 value = Hobby003
Subkey 3 value = Hobby010
This is the same as the check box for the same name above. In fact, you can assume that a select list is a list of check boxes to select (not select) the appropriate entries.
However, the list box also has the specified value, and if you set the Value property in the tag, you will get the textual content of the selected option, and the Request.Form set will contain such a project:
Hobby = swimming, Reading, sleeping
And, similarly, the complex collection traversal code returns the following results:
Hobby:
subkey 1 value = Swimming
subkey 2 value = Reading
subkey 3 value = Sleeping
Of course, if a single item is selected and the Value property is provided in , the result contains only:
Hobby = Hobby025
If you do not supply the Value property, you get:
Hobby = Swimming
This allows the option text to be displayed either by default (that is, no value), or it can be changed accordingly. The latter situation is extremely useful in some cases, such as displaying (a descriptive string) and passing a completely different content (such as representing a descriptive string with a short code).
D) HTML submit and image controls
check boxes and radio boxes are examples of Boolean controls, select or select Return to ON, unlike text boxes and most other HTML controls, and browsers do not contain values for controls that are not selected or are not selected. There is another commonly used Boolean control, called an HTML button, for
. such as , ... The type. A control of the
button type does not return any values because it has no direct effect on the form. The browser does not include the value of the button type control in any request, even if the submit method used to invoke the form is used. Similarly, the value of a
However, the input button control submit and image types actually submit forms to the server whose Value property contains the values of other controls on the form, as long as you include a name attribute in the HTML definition. For example, this form may be part of a wizard-type Web application that allows the user to step in or cancel a process:
In a form, you can include multiple submit buttons. In this case, you should give each button a unique Value property, as shown above. When a form is committed, traversing the value of the Request.Form collection produces a value that depends on which button is pressed to submit the form. If the user presses the "Previous" button, it will get:
Btnsubmit = Previous
Therefore, you can query the Request.Form collection to determine the next displayed page, for example:
Select case Request.Form ("btnsubmit")
Case "Next"
Response.Redirect "Page_3.asp"
Case "Previous"
Response.Redirect "Page_1.asp"
Case "Cancel"
Response.Redirect "Main_menu.asp"
End Select
You can also use a different Name property for each button as needed. and select the name of the control whose value is contained in the form collection. It is extremely useful in cases where the control does not have a complete tag but is followed by a longer text label, as shown in the following illustration.
The interface on this screen is generated by the following code:
What do and want to does now?
go back to the previous page
" ""
In an ASP page, after receiving data, you can check which button is pressed by the value provided by the name of the twist.
If Len (Request.Form ("Btnnext")) Then Response.Redirect "page_3.asp"
If Len (Request.Form ("btnprevious")) Then Response.Redirect "page_1.asp"
If Len (Request.Form ("Btncancel")) Then Response.Redirect "main_menu.asp"
This job is to query the ASP collection on a key and return an empty string if it does not exist. In other words, if the second button (previous page) is pressed, the value of Request.Form ("Btnnext") is an empty string, and its length is zero without generating an error. When the second button is pressed, the value Request.Form ("Btnprevious") of the entry in the Form collection will be "" with a length greater than 0.
E) Increase the efficiency of using the request collection
Accessing an ASP collection to download a value is a time-consuming process that requires computing resources because it contains a series of searches for the related collection, which is much slower than accessing a local variable. Therefore, if you intend to use a value in the collection more than once in a page, you should consider storing it as a local variable, for example:
Strtitle = Request.Form ("Title")
strFirstName = Request.Form ("FirstName")
strLastName = Request.Form ("LastName")
If Len (sttitle) Then strtitle = strtitle & ""
If strfirstname = "" Then
Strfullname = strtitle & "" & strLastName
ElseIf Len (strfirstname) = 1 Then
Strfullname = strtitle & strFirstName & "•" & strLastName
Else
Strfullname = strtitle & strFirstName & "" & Strla Stname
End If
F) Search all request Collections
In some cases, a key name that may know a value appears in the Request collection, but does not know exactly which collection. For example, if you have several pages (or different segments of a page) that send a value to the same ASP script, it may appear in the form or QueryString collection.
To see why a value might appear in a different collection, consider this: use a hyperlink element to request a page. In this case, the only way to add a value to the request is to add it to the URL. However, the same value may already appear in
in another page, or in different parts of the same page:
...
...
...
for
help page
...
In this case, by pressing the Help button on the form, a pair of name/value "Page=help" in the Request.Form collection is sent. However, pressing the hyperlink may also send a name/value "Page=help", but this time it is in the QueryString collection. To access this value, you can use a special feature of the ASP Request object:
Strpage = Request ("page")
This will search the entire set of--querystring, Form, Cookies, ClientCertificate, ServerVariables until the name of the first matching value is found. This is less efficient than direct access to the appropriate set, and is unsafe unless it is absolutely guaranteed that the value does not appear in another set.
For example, you might want to collect the name of a Web server that satisfies a customer request, which is done by looking for "server_name" in the Request.ServerVariables collection in each query. However, if any other collection also contains a value named "SERVER_NAME" (remember that the key name is case-insensitive), when you use Request ("SERVER_NAME"), you get the wrong result. Using Reqeust.servervariables ("SERVER_NAME") syntax, we will be very difficult to track error.
All in all, use the "Search all sets" technique with extra care and only when no other technology can provide the results you need.
g) access to other collections
In this section of this article, we've focused on the form collection, which is probably the most used one. However, all of these techniques apply equally to other objects. Includes collections that are provided by the request object (i.e. form, querystring, Cookies, ServerVariables, and ClientCertificate). and cookies provided by the response object (and the collection provided by other objects).
We will briefly see how a value enters a querystring set, with its advantages and disadvantages. However, at the same time the two cookies collection has additional features that make it easier to use cookies, as discussed below.