ASP 3.0 Advanced Programming (25)

Source: Internet
Author: User
6.2.4 Counters Components
The counters component can be used to create, store, increment, and retrieve values for each counter. Instead of confusing it with the page counter component that will be described later in this chapter, the counters component can be used to support statistics for any kind of data.
A counter contains an integer value that can be calculated by counters the component's method. Use the Set method to set the specified value of the counter, retrieve the value in the counter with the Get method, use the Increment method to add a counter value of 1, and remove a counter using the Remove method. The value of all counters is stored in a text file named Counters.txt, which can be found in the directory where the Counters.dll component resides.
1. Members of the Counters component
The counters component provides four methods for maintaining the values in each counter component, as shown in table 6-4:
Table 6-4 Counters assembly method and description
Method
Description
Get (Counter_name)
Returns the current value of the specified counter, if this counter was not previously created, and the moral is created and set to 0, with a return value of 0
Increment (Counter_name)
Increases the current value of the specified counter if this counter was not previously created, first created and set to 1
Remove (Counter_name)
Deletes the specified counter
Ser (Counter_name,value)
Sets the value of the specified counter to the integer value provided by the parameter value, which is created and set to the specified value if the counter was not previously created
2. Using the Counters Component
Because the Counters.txt file has only one copy that all component instances can access. Therefore, you should create only a single instance of the counters component and make it available to all pages of the Web site, a common way to do this is to create an application-scoped instance in the Global.asa file in the root directory of the default Web site.
Use the following procedure:
<!--Declare instance of the ASP counters component with application-level scope
-->
<object id= "Objcounters" runat= "Server" scope= "Application"
Progid= "MSWC. Counters ">
</OBJECT>
You can use the counters component to create a new counter for the task that needs to be completed. In the following procedure, a survey question with three choices is given, and the number of responses to each selection is counted, and the user submits the form that contains three selections, and the page is transferred. Suppose options are selected by clicking the Cmdyes, Cmdno, and Cmdmaybe of the Submit button, which are "yes", "no" and "possible" respectively.
<% ' in VBScript '
If Request.Form ("cmdyes") = "Yes" Then objcounter.increment ("Response_yes")
If Request.Form ("cmdno") = "No" Then objcounter.increment ("Response_no")
If Request.Form ("cmdmaybe") = "Maybe" Then

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.