Tutorials to .com

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

Advanced Programming ASP 3.0 (27)

Print View , by: iSee ,Total views: 11 ,Word Count: 1994 ,Date: Sat, 25 Apr 2009 Time: 8:48 PM

6.2.10 Tools components
Tools component provides a number of useful methods, you can check the page file exists, dealing with a HTML form, as well as a random integer, as well as Macintosh computers for a number of methods can also check whether there is a server plug-ins and check whether the user is the owner of the site.
1. Tools components into a page
Tools component provides five methods, two of which depend on the operating system, such as shown in Table 6-8:
Table 6-8 Tools and components description

FileExists (relative_url)
If relative_url specified file exists, the return value is True, otherwise False. Must be given the virtual relative path and file name and file must exist in the published Web site directory.

Random ()
A -32768 ~ 32767 is located in a random integer between. The use of ABS function (VBScript) or Math.abs (JScript) to be in between 0 ~ 32768 positive integer. Mod operator to use (VBScript) or the% operator (JScript) to be designated within the scope of a value. For example:
intRand = (objTools.Random Mod 76) + 25
A 25 ~ 100 in the integer.

ProcessForm (output_url,
template_url, [insertion_point])
Template_url designated through an HTML form document processing, and inserted from the form has been submitted to the current page values. Output_url included in the results of the specified file, if the designated optional insertion_point (string) parameter, the components may already exist in the output file to find the string and insert a new position in the content. Insertion_point parameters if not specified, any existing document output_url output were replaced by new

Applies only to Macintosh machines, if the current user account is a Web site's owner, the return value is True, otherwise the return value is False

PluginExists (plugin_name)
Applies only to Macintosh machines, plugin_name If you specify a server installed in the machine, the return value is True, otherwise False

2. FileExists method to use
Allows the user to visit in, you can use the FileExists method to check the existence of certain documents to the server (Note that this method and FileSystemObject.FileExists work the same way).
The following example, the user provides the relative page URL, if the user would like to txtURL named by the URL text box to open the page, before the re-orientation can check whether there is.
<% / / In Jscript:
var objTools = Server.CreateObject ( 'MSWC.Tools');
var strURL = Request.Form ( 'txtURL'); / / collect the page URL they entered
if (objTools.FileExists (strURL)) / / see if it exists
Server.Transfer (strURL) / / if it does, transfer to it
Else / / or if not display a message
Response.Write ( 'Sorry, the page you requested does not exist');
Here is a sample page (using VBScript) to show the components of the three methods (non-Macintosh), available from asp Installable Components to run the main menu, as shown in Figure 6-16:

Figure 6-16 Components of the method of operation of the Tools page
The first part of the page to allow the importation of a file relative URL, and tell the user that the file exists. Examples of default values is provided to view the root directory of the site whether the global.asa file. Click the button, whether the document that the information can be found on the top of the page, as shown in Figure 6-17:

Figure 6-17 the results of running the FileExists method
To all the controls on the page <FORM>, the author back to this page, it has become a norm. The beginning of the page to see which button to click. If FileExists button component on the FileExists method to call and display the appropriate information.
'look for a command sent from the FORM section buttons
If Len (Request.Form ( "cmdExists")) Then
strFile = Request.Form ( "txtFile")
If objTools.FileExists (strFile) Then
Response.Write "The file '<B>" & strFile & "</ B>' does exist. <P>"
Response.Write "The file '" & strFile & "' <B> does not </ B> exist. <P>"
End If
End If
3. Tools.Random methods used
In the ASP page, and sometimes need a random number to complete certain tasks, for example, the user re-positioning to a random page, select the color or display daily tips. vbscript can use the Rnd function, but should be derived from the specified values into the framework of the integral. Tools of the Random method components easier to use, because it can provide an integer value directly.
Random methods in the result is a range -32768 ~ 32767 integer value, in order to obtain a specified range of integers, you can use the scripting language of the Abs function, and the next largest integer modulus. For example, using VBScript language in order to create a 0 to 20 of the positive integer, you can use the following statement:
intRandom = Abs (objTools.Random) Mod 21
In order to obtain between 50 to 100 values can be used:
intRandom = (Abs (objTools.Random) Mod 51) + 50
Sample page using this technology to generate random numbers, the first need to check the value entered by the user to ensure that these values are both effective and correct positive integer relative.
If Len (Request.Form ( "cmdRandom")) Then
intMin = -1 'preset to illegal values and then
intMax = -1 'only set if a valid number is entered
strMin = Request.Form ( "txtMinimum")
strMax = Request.Form ( "txtMaximum")
If IsNumeric (strMin) Then intMin = CStr (strMin)
If IsNumeric (strMax) Then intMax = CStr (strMax)
If (intMin> = 0) And (intMax> intMin) Then
intRandom = (Abs (objTools.Random) Mod (intMax - intMin + 1)) + intMin
Response.Write "Your random value is: <B>" & intRandom & "</ B> <P>"
Response.Write "<B> The numbers you entered are not valid. </ B> <P>"
End If
End If
When the page re-transferred, the results showed that at the top of the page, as shown in Figure 6-18:

Figure 6-18 the results of running Random method
4. Tools.ProcessForm methods used
Tools component is the most complex ProcessForm, there used to read the temporary files on disk, and insert in which the creation of information (possibly from the Request.Form collection of the current page content), and then the results as a file output to disk, this method syntax is:
ProcessForm (output_url, template_url, [insertion_point])
Temporary files and output files for the current page URL to use the definition of relative. Output file can be ASP page, and if so, when it opens in the browser, it will be normal. Temporary files can contain common ASP code, but does not run, just simply copy the output file. However, if the temporary files on the ASP code <%% ...%%> qualifiers, when the temporary files will be transferred to the implementation of the code, which allows dynamically generated values (such as processing time and date) into the to the output page.
The following is a sample file template.asp (in the Tools directory chapter06 subdirectory):
This file was created by the ASP Tools component
The content of the request was:

Output file name: <%% = Request.Form ( "txtOutput")%%>
Template file name: <%% = Request.Form ( "txtTemplate")%%>
Insertion point text: <%% = Request.Form ( "txtInsert")%%>

Created <%% = Now ()%%>
Examples of the target page contains temporary files to use the controls, these controls and create a temporary file in the same folder called output.asp the output file, as shown in Figure 6-19:

Figure 6-19 to run the interface Tools.ProcessForm method
Click the button, will run as part of ASP code, data collection from the text box and call ProcessForm methods:
If Len (Request.Form ( "cmdProcess")) Then
strTemplate = Request.Form ( "txtTemplate")
strOutput = Request.Form ( "txtOutput")
strInsertPoint = Request.Form ( "txtInsert")
'We display the template contents here

'Process the form contents
objTools.ProcessForm strOutput, strTemplate, strInsertPoint

'We display the output file contents here
End If
(1) set the output file access
If you get a "MSWC.Tools error 80004005 Couldn't open output file" error message, which means that IIS does not allow the specified directory to write output file. To solve this problem the quickest way is to, in the Properties dialog box in the Security tab, the corresponding directory, as well as the Tools of the directory stored output.asp group Everyone set to Full Control permission, as shown in Figure 6-20 :

Figure 6-20 Setting output file interface access
(2) See the contents of the documents
Figure 6-21 shows the default values above code to run with the default value of the results of operation, we can see the original content of temporary files, and the use of methods ProcessForm inserted into the contents of the output file.

Figure 6-21 the contents of output file
Omitted here show seen from the contents of the documents in front of the program code. Display methods and ASP Installable Components page in the main menu to display the contents of a list of links to the same documents. TextStream object FileSystemObject and use examples to read the entire document to a string, and then insert the page (remember to use Server.HTMLEncode approach to the characters "<" and ">" can be used to display into the characters).
QUOT = Chr (34) 'double-quote character
'create a FileSytemObject object to display the template file contents
Set objFSO = Server.CreateObject ( "Scripting.FileSystemObject")

Response.Write "The content of the template file <B>" & _
strTemplate & "</ B> is:"

Set objTStream = objFSO.OpenTextFile (Server.MapPath (strTemplate), ForReading)
strContent = Server.HTMLEncode (objTStream.ReadAll) 'read whole file

Response.Write "<DIV CLASS=" & QUOT & "showcode" & QUOT & "> <PRE>" & _
strContent & "</ PRE> </ DIV>"
(3) parameters on the insertion point
ProcessForm method insertion_point optional parameters can be used in the document to insert the text of the specific points, this method is to find a specific string in the output file of the first instance, and then inserted in the position to create new content, in the output file by the asterisk (*) on insertion_point string to place in front of new content. If omitted insertion_point parameters, new content will replace the entire output file.
Note insertion_point parameters Tools.ProcessForm method can not be used in an earlier version.

Asp Base Articles

Can't Find What You're Looking For?

Rating: Not yet rated


No comments posted.