ASP 3.0 Advanced Programming (19)

Source: Internet
Author: User
Tags array end error handling key reference variable
Programming | Advanced 5.3.2 Dictionary Object example
This book provides a series of sample files that can be used to test the various properties of the script Run-time library.
The default page of this chapter Code provides a series of examples of VBScript links that you can use. Some examples are also valid for JScript. These samples are stored in the corresponding subdirectories in the Chapter05 directory, and the displayed interface is shown in Figure 5-2:

Figure 5-2 ASP script run-time Object sample page
To see the Dictionary object running, click on the first link on the menu page to open the page called show_dictionary.asp. This page shows the content of the Dictionary object we provide, allowing you to experiment with its properties and methods. The screen looks like figure 5-3:

Figure 5-3 Properties and methods of Dictionary objects
1. Dictionary's Global.asa file
One of the files provided with the Dictionary Object sample page is Global.asa. It creates and populates the Dictionary object of a session-level scope, so its contents are not lost between page requests. Generally (considering scalability), this is not an ideal practice. In this example, you can see the effects of Dictionary's properties and methods.
If you download and install the sample on your own server, you must create a virtual application that is based on this global.asa file. or add its contents to the Global.asa file in the root folder of the default site. The 3rd chapter describes how to create a virtual application with a wizard. For this example, however, the easiest way to create a virtual application is to right-click the dictionary subfolder in the Chapter05 sample folder, and in the Properties dialog box's Home Directory tab, click the Create button, as shown in Figure 5-4:

Figure 5-4 Creating a virtual application
In this global.asa file, the code creates a Scripting.Dictionary object instance of the session-level scope using the <OBJECT> element. It then puts a series of values into the dictionary in the Session_OnStart event handler and assigns a reference to the Dictionary object to the ASP session variable MyDictionary:
<object id= "objbooklist" runat= "SERVER" scope= "Session"
Progid= "Scripting.Dictionary" >
</OBJECT>

<script language= "VBScript" runat= "SERVER" >

Sub Session_OnStart ()
Objbooklist.add "2610", "Professional Active Server Pages 3.0"
Objbooklist.add "1274", "Instant JavaScript"
Objbooklist.add "2882", "beginning ASP components"
Objbooklist.add "1797", "Professional ASP techniques"
Objbooklist.add "1835", "AD0 2.0 Programmer ' s Reference"
Set session ("mydictionary") = Objbooklist
End Sub

</SCRIPT>
2. Dictionary Sample page
In the Scripting.Dictionary Object home page, the first task is to get a reference to the Dictionary object instance of the session-level scope. Note that this reference is an object variable, so you must use the SET keyword in VBScript.
Then, check to see if you got an object (this is a good habit), and if you don't have a virtual application that contains the Global.asa file correctly, check out where the problem is. You will see our own message instead of the ASP's error message (but note that the default error handling must be turned off for this operation).
<%

On Error Resume Next ' turn out error handling to test if object exists

' Retrieve Dictionary object from user ' s session
Set Objmydata = Session ("MyDictionary")

If IsObject (objmydata) Then ' found Dictionary object in session
...
%>

<p><div class= "Subhead" >iterating the Dictionary with arrays</div>
<%
Arrkeysarray = Objmydata.keys ' Get all of the Keys into an array
Arritemsarray = Objmydata.items ' Get all of the Items into an array
For intloop = 0 to Objmydata.count-1 ' iterate through the array
Response.Write "Key: <B>" & Arrkeysarray (intloop) & "</B> Value: <B>" _
& Arritemsarray (intloop) & "</B><BR>"
Next
%>
...
... Other code and controls go ...
...
<%
Else

' Could not find Dictionary object in the session
Response.Write "Dictionary Object not available in Global.asa for session"

End If
%>
The list of dictionary content displayed on the page is a two array created using the key and items methods of the Dictionary object, which can be traversed using the preceding code.
3. Dictionary page Control
Under Dictionary's list of contents is a series of HTML controls that can be used to set certain properties of the Dictionary object and to perform various methods. The controls are all within a <FORM>, and the Action property value is this page, so the contents of the form are submitted back to this page. The same technique is used in the examples in the previous chapter.
In <FORM>, changing a property or executing a method is achieved by a button (without a caption). The values for properties and methods are placed in the text box or list box next to the button.
The first button on the page is used to set the key property of the entry in the dictionary. A drop-down list is used to select a key value that already exists. The following code creates a control for that part of the page. To populate the list, another technique is used to traverse the Dictionary object, i.e. Next statement. The code is as follows:
...
<form action= "<% = Request.ServerVariables (" Script_name ")%>" method= "POST >

<p><div class= "subhead" >the Dictionary properties</div>
<input type= "SUBMIT" name= "Cmdchangekey" "value=" ">
Dict



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.