ASP 3.0 Advanced Programming (27)

Source: Internet
Author: User
Tags abs end html form integer numeric value string valid root directory
Programming | Advanced 6.2.10 Tools Components
The tools component provides useful methods for checking whether a file exists, processing an HTML form, generating a random integer, and some methods for Macintosh computers, as well as checking for the presence of a server plug-in and checking whether a user is the owner of a Web site.
1. The page of the tools component
The tools component provides five methods, two of which are dependent on the operating system, as shown in table 6-8:
Table 6-8 Tools component Method and description
Method
Description

FileExists (Relative_url)
If the file specified by Relative_url exists, the return value is true, otherwise false. You must give the virtual relative path and file name, and the file must exist in the published Web site directory.

Random ()
Produces a random integer located between the -32768~32767. Use the ABS function (VBScript) or Math.Abs (JScript) to get a positive integer between 0~32768. Use the MoD operator (VBScript) or the% operator (JScript) to get a numeric value within the specified range. For example:
Intrand = (objtools.random Mod 76) + 25
Gets an integer between the 25~100.

ProcessForm (Output_url,
Template_url,[insertion_point])
Processes an HTML form by template_url the specified file and inserts a number from the form that has been submitted to the current page. The result is written into the file specified by Output_url, and if an optional insertion_point (string) parameter is specified, the component can find the string in an existing output file and insert the new content at that location. If the Insertion_point parameter is not specified, any existing Output_url file is replaced by the new output

Owner
Only for Macintosh machines, if the current user account is the owner of the Web site, the return value is true, otherwise the return value is False

PluginExists (Plugin_name)
Available only for Macintosh machines, if the specified server Plugin_name is installed on the machine, the return value is true, or false

2. Using the FileExists method
Before allowing users access, you can use the FileExists method to check whether some files exist in the server (note that this method works the same way as filesystemobject.fileexists).
In the following example, the user provides a relative URL to a Web page, and if the user wants to open the page by typing a URL in a text box named Txturl, it can be checked for existence before redirection.
<%//In Jscript:
var objtools = Server.CreateObject (' MSWC. Tools ');
var strURL = Request.Form (' Txturl '); Collect the page URL they entered
if (objtools.fileexists (strURL))//if it exists
Server.Transfer (strURL)//If it does, Transfer to it
Else//If not display a message
Response.Write (' Sorry, the page you requested does not exist ');
%>
This provides a sample page (using VBScript) to demonstrate three methods of the component (Non-Macintosh), which can be run from the ASP Installable Components main menu, as shown in Figure 6-16:

Figure 6-16 page of the method that runs the tools component
The first part of the Web page allows you to enter a relative URL for a file and tell the user if the file exists. The default value provided by the example is to see if there are global.asa files in the root directory of the Web site. When you click the button, the information that indicates whether the file can be found will be placed at the top of the page, as shown in Figure 6-17:

Figure 6-17 Results of running the FileExists method
Putting all the controls on the page in <FORM> and submitting them back to this page has become a specification. At the beginning of the page, see which button was clicked. If it is a fileexists button, the FileExists method of the component is invoked and the appropriate information is displayed.
' Look for a command sent ' 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>"
Else
Response.Write "The File" & strfile & "' <b>does not</b> exist.<p>"
End If
End If
3. Using the Tools.random method
In an ASP page, you sometimes need a random number to complete some tasks, such as relocating a user to a random page, selecting a color, or displaying a daily hint. You can use the RND function in VBScript, but you want to convert the resulting value to an integer in the specified range. The random method of the tools component is easier to use because it provides an integer value directly.
The result of the random method is an integer value in the -32768~32767 range, in order to obtain a specified range of integers, you can use the ABS function in the scripting language and modulo the next largest integer. For example, to create a 0~20 positive integer in the VBScript language, you can use the following statement:
Intrandom = Abs (objtools.random) Mod 21
In order to get the numerical value between the 50~100, you can use:
Intrandom = (Abs (objtools.random) Mod 51) + 50
Example Web pages when using this technique to generate random numbers, you first need to check the values entered by the user to ensure that they are both valid positive integers and have the correct relative relationship.
If Len (Request.Form ("Cmdrandom")) Then
Intmin =-1 ' Preset to illegal values and then
Intmax =-1 ' only set if a valid # 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>"
Else
Response.Write "<b>the numbers you entered are not valid.</b><p>"
End If
End If
When the page is reset, the results are displayed at the top of the page, as shown in Figure 6-18:

Figure 6-18 Results of running the random method
4. Using the Tools.ProcessForm method
The most complex method in the tools component is ProcessForm, which is used to read



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.