Counters components in ASP

Source: Internet
Author: User
Tags contains iis integer
   Overview

The Counter component creates a counters object that can create, store, grow, and retrieve any number of independent counters.

A counter is a persistent value that contains an integer. You can control counters by using the GET, Increment, Set, and Remove methods of the Counters object. Once you create the counter, it continues until it is deleted.

Counters do not automatically increase when an event such as a page is accessed. You must use the set and Increment methods for manual setup or growth counters.

Counters are not scoped by scope. Once you create a counter, any page on your site can retrieve and control its value. For example, if you display and add a value for a counter called hits on a page named Page1.asp, and then add a hits value on another page called Page2.asp, the two pages will add the same counter. When you access Page1.asp, the value of the counter increases to 34, then the access page2.asp increases the hits to 35. The next visit to Page1.asp,hits will increase to 36.

All counters are stored in a separate text file named Counters.txt, which is stored in the same directory as the Counters.dll file.

Filename

Counters.dll counters components.

Counters.txt This file stores all the independent counters on a single site. Counters.txt is a UTF8 encoded file. You can use any Unicode character in the counter name.

Grammar

Add the following to the Global.asa file to create the counters object at once on your server:

Runat=server
Scope=application
Id=counter
Progid= "MSWC. Counters ">


Registry key

No.

Comments

Only one counters object is created on your site. This single Counters object can create any number of independent counters.

Note for the Personal Web Server on windows®95, a counters component has been specified in the Global.asa file in the default virtual directory. You can use the counters object created by the component by calling Counters.Get, Counters.Increment, Counters.remove, and Counters.Set as if it were built inside. You cannot create additional instances of the Counters object.

Method

Get returns the value of the counter.

The value of the Increment counter plus one.

Remove the counter from the Counters.txt file.

Set sets the value of the counter to a specific integer.

Example

Create an instance of the counters object in the Global.asa file by setting the ID property to Counter:

<object runat=server scope=application id=counter progid= "MSWC. Counters ">
</OBJECT>

You can then use the Counters object on a page to create all the required counters:

There have been <%= counter.increment (' defaultpagehits ')%> to this site.

Then on another page, you can increase the value of the counter by using the following methods:

You are are visitor number<%= counter.increment (' linkspagehits ')%> to this page.

Note This component can be obtained from a CD containing IIS IIS Resource Kit.

   Get Method

The Get method returns the current value of this counter based on the name of the counter. If this counter does not exist, the method creates it and resets it to 0.

Grammar

Counters.Get (CounterName)

Parameters

CounterName

A string that contains the name of the counter.

Example

The value of the counter can be displayed with the <%= counters.get (CounterName)%>. Assigns the value of a counter to a variable with <% Countervar = Counters.Get (CounterName)%>.

The following script shows the polling score for a poll of favorite colors.

<% If colornumber = "1" Then
Counters.Increment ("Greencounter")
Else
If Colornumber = "2" Then
Counters.Increment ("Bluecounter")
Else
If Colornumber = "0" Then
Counters.Increment ("Redcounter")
End If
End If
End If%>

Current vote tally:

Red: <% =counters.get ("Redcounter")%>

Green: <% = Counters.Get ("Greencounter")%>

Blue: <% = Counters.Get ("Bluecounter")%>

   Increment Method

The Increment method adds the value of the counter by 1, based on the name of the counter, and returns a new value for the counter. If the counter does not exist, this method creates the counter and sets its value to 1.

Grammar

Counters.Increment (CounterName)

Parameters

CounterName

A string that contains the counter value.

Example

Use the <% counters.increment (CounterName)%> to increase the value of the counter. Use the <%= counters.increment (CounterName)%> to increase and display counter values.

In order to get the value of the counter, you can use Counters.Get. To set the value of the counter to a special value, use Counters.Set.

The following code implements the online page access counter.

<p> There have been <%= counters.increment ("hits")%> visits to this Web page.
</P>

In the example above, each time a client makes a page request to the server, it uses Counters.Increment to add the counter value by 1.

Remove method

The Remove method deletes counters from the counters object and the Counters.txt file based on the name of the counter.

Grammar

Counters.remove (CounterName)

Parameters

CounterName

A string that contains the name of the counter.

Example

The following code deletes the counter hitscounter from the Counters.txt file.

<% Counters.remove (hitscounter)%>

   Set Method

The Set method sets the counter to the integer value based on the name of the counter and an integer parameter, and returns the new value. If the counter does not exist, Counters.Set creates the counter and sets its value to this integer.

To get the value of the counter, you can use Counters.Get. To add a value to the counter, use Counters.Increment.

Grammar

Counters.Set (CounterName, int)

Parameters

CounterName

A string that contains the name of the counter.

Int

The new integer value of the CounterName.

Example

The following code resets the access counter pagehits to 0.

<% Counters.Set (pagehits, 0)%>

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.