Tutorials to .com

Tutorials to .com » Asp » Base » Advanced Programming ASP 3.0 (18)

Advanced Programming ASP 3.0 (18)

Print View , by: iSee ,Total views: 18 ,Word Count: 2362 ,Date: Sun, 26 Apr 2009 Time: 1:18 AM

5.2.5 Object-type library reference
In an earlier version of ASP, in the script used in the object or component, the component of the public within the definition of constant (if any) in the asp will no longer be valid. This means that we need a statement of their own (or equivalent) and specify the corresponding value.
For example, when an earlier version of ASP in the use of ActiveX database Objects (ADO) components (to be detailed in Chapter 8 of the study), had to use the Open record set by adding a predefined constant method statement. For example:
Const adOpenKeyset = & H0001
Const adLockPessimistic = & H0003
Const adCmdTable = & H0002
...
rs.Open "Contact", "DSN = GlobalExampleData; UID = examples; Password =;", _
adOpenKeyset, adLockPessimistic, adCmdTable
...
Another method is to use the # include directive to insert a page adovbs.inc document. The document by the IIS / ASP to offer all the necessary ADO contains predefined constants. Update the code, must be confirmed using the latest version and check it for all the page requests are available.
For IIS 5.0, there is a better way, through the use of HTML elements METADATA Notes command, you can object to the type of components or increase the reference library (IIS 4.0 does not support this feature).
<! - METADATA TYPE = "TypeLib"
FILE = "path_and_name_of_file"
UUID = "type_library_uuid"
VERSSION = "major_version_number.minor_version_number"
LCID = "locale_id"
->
Of which:
Path_and_name_of_file is a type library file (. Tlb) or ActiveX DLL absolute physical path, we must provide this parameter or parameters type_library_uuid.
Is the type library type_library_uuid only a unique identifier, we must provide this parameter or parameters path_and_name_of_file.
Major_version_number.minor_version_number (optional) component of the definition of the required version. If you do not use the version of the latest version.
Locale_id (optional) is a regional identifier. If not found in the region type library, the computer will use the default (as defined in installation) Regional.
Therefore, the use of this technology, through the use of the following codes, enable the built-in ADO predefined constants can be used in the ASP page:
<! - METADATA TYPE = "TypeLib"
FILE = "C: \ Program Files \ Common Files \ system \ ado \ msado15.dll"
->
Msado15.dll file name can also be used later (after 2.50) components of the ADO.
If ASP can not load type library, it returns an error and stop the implementation of the page. Possible error, such as shown in Table 5-1:
Table 5-1 error code and description
Error Help
ASP 0222 Invalid type library description
ASP 0223 Type library not found
ASP 0224 Type library can not load
ASP 0225 Type library can not be packaged (ie, ASP is not the type library from the specified type library to create a package object)

5.2.6 Create the client object instance
Discussed in the ASP on the server object instance and component technology, It is worth emphasizing that in a browser running the client page and complete the same work. If you use ASP to create client-side script that contains the procedures page, or use elements <OBJECT> examples to create client-side component will be found very useful. In most cases, the script run-time object can be instantiated on the client and use the results of the ASP with the same server.
1. vbscript CreateObject method
The client to use CreateObject, the environment in the browser to create a component or object instance, they and the browser is running in the same memory space (ie, process), unless the object is achieved with a. Exe file name extension of the executable file.
Usually designated object ClassID, rather than using the ProgID string, so that can not be installed with other objects on the client's conflict.
<SCRIPT LANGUAGE="VBScript">
Dim objThis
Set objThis = CreateObject ( "clsid: 892D6DA7-E0F9-00105A42AF30")
...
</ SCRIPT>
Of course, we can use the ProgID, and the use of common objects or components (in particular, the standard installation of the object or component), then get the components of the risk of error is very small:
<SCRIPT LANGUAGE="VBScript">
Dim objThis
Set objThis = CreateObject ( "Scripting.Dictionary")
...
</ SCRIPT>
2. jscript ActiveXObject method
In order to instantiate the client Jscript objects and components, must use the ActiveXObject and new operators:
<SCRIPT LANGUAGE="JScript">
var objMyData = new ActiveXObject ( 'clsid: 892D6DA7-E0F9-00105A42AF30');
</ SCRIPT>
Or:
<SCRIPT LANGUAGE="JScript">
var objMyData = new ActiveXObject ( 'this.object');
</ SCRIPT>
3. <OBJECT> Element technology
<OBJECT> Elements can also be used to create client-side instance of an object or component. RUNAT attribute should be omitted or set to "CLIENT". However, this property is on the client are ignored, and therefore set up the sole purpose of this attribute is used in the ASP page <OBJECT> elements of server-side instance of the component instance to prevent confusion.
<OBJECT ID = "objThis" RUNAT = "CLIENT"
CLASSID = "clsid: 892D6DA7-E0F9-00105A42AF30"
CODEBASE = "http://yourserver.com/components/mycomponent.cab">
<PARAM NAME="param1" VALUE="value1">
<PARAM NAME="param2" VALUE="value2">
</ OBJECT>
Note that here the CODEBASE attribute that allows to download and install the component from the URL (if the component is not installed). IE 3.0 or above versions of this feature.
<OBJECT> Elements for the use of methods, the properties can be used in the value of the client to use to view the site http://msdn.Microsoft.com/workshop/author/dhtml/reference/objects/OBJECT.asp, or Windows 2000 Platform SDK documentation <OBJECT> tags, or look at the "IE5 Dynamic HTML Programmer 'Reference" book, ISBN 1-861001-74-6, Wrox Press.

Target 5.3 Scripting.Dictionary
Many of Microsoft's programming language, such as Visual Basic, VBScript and Jscript, provides a collection (collection). Can be imagined as an array collection, you can use the built-in function which complete data storage and manipulation of the basic tasks. Not have to worry about where the ranks of data, but the only button used for a visit.
VBScript and Jscript are the targets of similar, Scripting.Dictionary known object or Dictionary object. It is similar to two-dimensional array, the keys and related data entries stored together. But the real object-oriented approach, should not directly access the data entry, must be used to support the Dictionary object methods and properties to achieve.
This chapter provides some sample pages, allowing the pilot script run-time object methods and properties. The examples in the downloaded file in the subdirectory Chaper05.

5.3.1 create and use Dictionary object
Dictionary object to create a sample as follows:
'In VBScript:
Dim objMyData
Set objMyData = Server.CreateObject ( "Scripting.Dictionary")

/ / In Jscript:
var objMyData = Server.CreateObject ( 'Scripting.Dictionary');

<! - Server-Side with an OBJECT element ->
<OBJECT RUNAT = "SERVER" SCOPE = "PAGE" ID = "objMyData"
PROGID = "Scripting.Dictionary">
</ OBJECT>
Dictionary object can also be used in the client's IE.
1. Summary of object members Dictionary
Table 5-2 and Table 5-3 lists the Dictionary object properties and methods and the corresponding note.
When adding a key / entry of the time, if the key already exists; or delete a key / entry for, the keyword / entries do not exist, or change data already contains Dictionary object CompareMode, will generate an error.
Table 5-2 Dictionary object attribute and description
Attribute Description
CompareMode (only for VBScript) set or return the string comparison mode button
Count read-only. Return to Dictionary's keys / entries on the number of
Item (key) set or return the items specified key value
Key (key) Set key
Table 5-3 Dictionary object methods and description
Description
Add (key, item) increased key / entry for the Dictionary
Exists (key) If the specified key exists, return True, otherwise return False
Items () returns a Dictionary object that contains all the entries in the array
Keys () returns a Dictionary object that contains all the keys of the array
Remove (key) to delete a specified key / entry for
RemoveAll () delete all the keys / entries on the
2. Added to the Dictionary, and delete entries
Once a new (empty) Dictionary, you can add an entry, as well as gain entry to delete entries:
'In VBScript:
objMyData.Add "MyKey", "MyItem" 'Add Value MyItem with key MyKey
objMyData.Add "YourKey", "YourItem" 'Add value YourItem with key YourKey
blnIsThere = objMyData.Exists ( "MyKey") 'Returns True because the item exists
strItem = objMyData.Item ( "YourKey") 'Retrieve value of YourKey
strItem = objMyData.Remove ( "MyKey") 'Retrieve and remove YourKey
objMyData.RemoveAll 'Remove all the items
In JScript, the equivalent code is:
/ / In JScript;
objMyData.Add ( 'MyKey', 'MyItem'); / / Add Value MyItem with key MyKey
objMyData.Add ( 'YourKey', 'YourItem'); / / Add value YourItem with key YourKey
var blnIsThere = objMyData.Exists ( 'MyKey'); / / Returns True because the item exists
var strItem = objMyData.Item ( 'YourKey'); / / Retrieve value of YourKey
var strItem = objMyData.Remove ( 'MyKey'); / / Retrieve and remove YourKey
objMyData.RemoveAll (); / / Remove all the items
3. Modify the value of key or entry
May amend the value of key, or by modifying the keys associated with a particular entry of the data stored in the Dictionary to change the data. Change the following code for the MyKey key data entry.
ObjMyData.Item ( "MyKey") = "NewValue" 'In VBScript
ObjMyData.Item ( 'MyKey') = 'NewValue'; / / In JScript
If the specified key was not found in the Dictionary, will be to create a Dictionary for the MyKey key to the value of New Value for the entry of the new key / entry right. Interestingly, if you are using a non-existent entry key to retrieve not only be an empty string (which can be thought of), but also add a new Dictionary of the key / entry pairs key that is specified key However, the data entry is empty.
Key attributes can be used only to change the key without altering the value of the corresponding data entry. There will be a change for the MyKey key MyNewKey, can be used:
objMyData.Key ( "MyKey") = "MyNewValue" 'In VBScript
objMyData.Item ( 'MyKey') = 'MyNewValue'; / / In JScript
If the specified key is not found, then have a run-time error.
4. Comparison of mode settings
Dictionary of CompareMode attribute only applies to VBScript, can not be used in JScript. When comparing the string key, allows you to specify the way of comparison. Allow the value of the two BinaryCompare (0) and TextCompare (1). BinaryCompare (0) for binary control (that is case-sensitive); TextCompare (1) for the text of the control (that is not case-sensitive).
5. Ergodic Dictionary
Study Dictionary, there are two methods and a property in need of special attention, and they allow us to traverse Dictionary is stored in all the key / entry right. Items methods in a one-dimensional array form of the return of all the entries in Dictionary of data, and methods of keys with a one-dimensional array of all the key already exists. Can use the Count property to be the number of keys or entries.
For example, you can use the following code to be known as the Dictionary of objMyData all of the key and value entries. Note that although the Count property to preserve the Dictionary's keys / entry number, but the VBScript and JScript array subscript 0 always from the beginning. Therefore, the array subscript from 0 to Count-1.
'In VBScript:
arrKeys = objMyData.Keys' Get all the keys into an array
arrItems = objMyData.Items' Get all the items into an array

For intLoop = 0 To objMyData.Count -1 'Iterate through the array
StrThisKey = arrKeys (intLoop) 'This is the key value
StrThisItem = arrItems (intLoop) 'This is the item (data) value
Next

/ / In JScript
/ / Get VB-style arrays using the Keys () and Items () methods
var arrKeys = new VBArray (objMyData.Keys ()). toArray ();
var arrItems = new VBArray (objMyData.Items ()). toArray ();

for (intLoop = 0; intLoop <objMyData.Count; intLoop + +) (
/ / Iterate through the arrays
strThisKey = arrKeys [intLoop]; / / This is the key value
strThisItem = arrItems [intLoop]; / / This is the item (data) value
)
In VBScript also can use the For Each ... Next statement to complete the same function:
'Iterate the dictionary as a collection in VBScript
For Each objItem in arrItems
Response.Write objItem & "=" & arrItems (objItem) & "<BR>"
Next


Asp Base Articles


Can't Find What You're Looking For?


Rating: Not yet rated

Comments

No comments posted.