ASP 3.0 Advanced Programming (10)

Source: Internet
Author: User
Tags array end reference variable
Programming | Advanced (1) code to traverse the Contents collection
To traverse the Contents collection, you can use a For each ... Next structure. Each item in the collection can be a simple variant type variable, a Variant array, or a reference to an object. Because each type of value needs to be handled differently, you have to check each one to determine its type.
You can use the VarType function in VBScript to do this work. Here you use the IsObject and IsArray functions instead:
For each objitem in application.contents
If IsObject (application.contents (objitem)) Then
Response.Write "Object Reference: '" & objitem & "'

ElseIf IsArray (application.contents (objitem)) Then
Response.Write "Array:" "& objitem &" ' contents are:

Vararray = application.contents (objitem)
' note:the following with a one-dimensional array
For intloop = 0 to UBound (vararray)
Response.Write "Index (" & intloop & ") =" & _
Vararray (intloop) & "

Next
Else
Response.Write "Variable:" "& objitem &" ' = "_
& Application.Contents (objitem) & "

End If
Next
Notice how the program retrieves the array from the Application object. Assign it to a local (Variant) variable, using the following statement:
Vararray = application.contents (objitem)
You can use the UBound function to find the size of the array (the number of elements), which can be used as a termination condition for traversal:
For intloop = 0 UBound (vararray)
This example is a one-dimensional array and will only display the contents of such an array. You can edit your code to handle multidimensional arrays as needed, for example:
For intloop = 0 to UBound (vararray)
Intnumberofdimensions = UBound (Vararray, 1)
For intdimension = 0 to Intnumberofdimensions
Response.Write "Index (" & intloop & ") =" _
& Vararray (Intloop, intdimension)
Next
Response.Write "

Next
(2) Traversing the code of the StaticObjects collection
The StaticObjects collection contains all the object references that are declared using <OBJECT> elements in Global.asa. Because each entry is an object variable, the array can be traversed by simple code. We will output the name of the object (as defined in the id attribute):
For each objitem in application.staticobjects
If IsObject (Application.staticobjects (objitem)) Then
Response.Write "<OBJECT> element:id= '" & Objitem & "

End If
Next
1. Add value to Contents collection
The method of adding a value to the Contents collection is the same as the method used in the script code of the Global.asa Web page. The example Web page allows a new variant value to be added to the Application object with the suggested name and value (editable as needed), as shown in Figure 3-15:

Figure 3-15 The screen that adds a value to the Contents collection
Click the button, reload the page, add the value to the Application.Contents collection, and display it in the list, as shown in Figure 3-16:

Figure 3-16 Screen showing the contents of the Contents collection
Add the code for the new contents entry
All the buttons and other HTML controls are placed on a form in the sample Web page. Action sets the path of the current Web page, and then reload the form when it is submitted. The method property is "POST," so the values in the control appear in the Request.Form collection. These two techniques have been used in previous chapters:
<form action= "<% = Request.ServerVariables (" Script_name ")%>" method= "POST >
The buttons on the form are normal HTML input controls with the same title (three spaces) but different names. For example, the code that creates the first button (which adds the value to the Application object) is:
<input type= "SUBMIT" name= "Cmdadd" "value=" ">
When you reload the page, check the Request.Form collection, determine which submit button is clicked, and handle it accordingly. If you add a button that adds a value to the Application object (the button is named Cmdadd in the HTML <INPUT> element), use the following program segment:
If Len (Request.Form ("Cmdadd")) Then
Strvarname = Request.Form ("Txtvarname")
Strvarvalue = Request.Form ("Txtvarvalue")
Application.Lock
Application ("strvarname") = Strvarvalue
Application.UnLock
End If
Note how programs use the Application.Lock and Application.UnLock methods to ensure that these values are not confused by two of users accessing concurrently. This is generally not possible if you are setting up a specific value. But it's wise to always use the lock and unlock methods.
2. Delete value in Contents collection
There are two buttons at the bottom of the example page, as shown in Figure 3-17:

Figure 3-17 shows two buttons at the bottom of the page
These two buttons allow you to delete values from the Application.Contents collection. The first button deletes a single specified value from the collection, and the Drop-down list box displays a list of the names of the contents collection values (remember that you cannot delete values from the StaticObjects collection because they are static).
This list is created by traversing the contents collection (as we did earlier) to execute an ASP Web page. However, we only collect the names of each item and place them in the <OPTION> element within the <SELECT> list element:
...
<select name= "Lstremove" size= "1" >
<%
For each objitem in application.contents
Response.Write "<OPTION>" & Objitem & "</OPTION>"
Next
&>
</SELECT>
...
After the ASP code is executed, the results seen in the browser are:
<select name= "Lstremove" size= "1" >
<OPTION>ADOConnection</OPTION>
<OPTION>Variant_Array</OPTION>
<OPTION>Start_Time</OPTION>
<optio

[1] [2] [3] Next page



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.