ASP 3.0 Advanced Programming (25)

Source: Internet
Author: User
Tags contains integer root directory
Programming | Advanced 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
Objcounter.increment ("Response_maybe")
%>
If this is the first time a specified response has been received, the program will create a new counter and automatically initialize to 1.
Counters are not limited in scope, because counters objects are created in file Global.asa, which means that they are available on any page created in a virtual application or Web site, so this "survey counter" can be used on any page of the application. Remember that a single Counters object can provide as many independent counters as needed, and you do not need to create many counters object instances.
In the previous example of the Ad Rotator Component page, you studied how to use the counters component to store the number of clicks per advertiser, or you can use the counters component's Get method to display the current value in the page.
Wrox Press: <b><% = Objcounters.get ("Wrox")%></b><br>
Stonebroom: <b><% = Objcounters.get ("Stonebroom")%></b><br>
Xtras: <b><% = Objcounters.get ("Xtras")%></b><br>
ComponentSource: <b><% = Objcounters.get ("compsrc")%></b><br>
Four CDs: <b><% = Objcounters.get ("Fourcds")%></b><br>
Lunar: <b><% = objcounters.get ("Lunar")%></b><br>
The current value of the counter is automatically updated each time the page is loaded. However, the page also contains a number of other two methods that the control can call the counters component, that is, to delete a counter (equivalent to setting it to 0) and set the counter to a specified value, as shown in Figure 6-6:

Figure 6-6 Counters Component use Demo
These controls on a <FORM>, when you click on any small blank button, the form is presented to the same page in almost the same way as all the pages in this chapter. The following program is the HTML code that creates the control for the Remove method.
<form action= "<% = Request.ServerVariables (" Script_name ")%>" method= "POST >

<input type= "SUBMIT" name= "Cmdremove" "value=" ">
Counter.remove ("
<select name= "Lstremove" size= "1" >
<option value= "Wrox" >wrox press</option>
<option value= "Stonebroom" >Stonebroom</OPTION>
<option value= "Xtras" >Xtras</OPTION>
<option value= "COMPSRC" >ComponentSource</OPTION>
<option value= "Fourcds" >four cds</option>
<option value= "Lunar" >Lunar</OPTION>
</SELECT> ") <P>
...
</FORM>
When you load the page, check the Request.Form collection to see the clicked button, and if a button is found, the corresponding part of the code is run. When you click the Remove button, the corresponding code is:
If Len (Request.Form ("Cmdremove")) Then
Strcountername = Request.Form ("lstremove") ' Get the counter name
Objcounters.remove Strcountername
Response.Write &



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.