Introduction to ASP Programming (15): Component Counters_asp Basics

Source: Internet
Author: User
This component is used to create one or more counters that track information about the number of visits to a Web page or a Web site. You can only create one counter component, but the component may have more than one counter.

Once the counter has been created, it will continue until it is deleted.

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

Counters are not scoped by scope. Once a counter is created, any page on the site can retrieve and control its value.

First, Counters.Set & Counters.Get
Counters.Set is primarily used to set the initial value of a count, and Counters.Get is the value that is used to extract the count.

1,counts_set.asp

<%
Set counters=server.createobject ("MSWC. Counters ")
Counters.Set "Cnbruce", 10
Counters.Set "IT365CN", 20
Counters.Set "Blueidea", 30
%>
Total <%= counters.get ("Cnbruce")%> visit cnbruce.com website <br>
Total <%= counters.get ("IT365CN")%> visit it365cn.com website <br>
Total <%= counters.get ("Blueidea")%> visit blueidea.com website <br>


The usage is very simple, first " counters=server.createobject ("MSWC.") Counters ")"Indicates the establishment of a counters component connection; counters.set "Cnbruce",Indicates that the initial value, named "Cnbruce", was counted at 10; Counters.Get ("Cnbruce")Indicates the extraction of a value named "Cnbruce".

Second, counters.increment
The function of this method is: to count each plus one. In the example above you refresh the page, the count is not added, so now swap the increment method.

2,counts_inc.asp

<%
Set counters=server.createobject ("MSWC. Counters ")
%>
Total <%= counters.increment ("Cnbruce")%> visit cnbruce.com website <br>
Total <%= counters.increment ("IT365CN")%> visit it365cn.com website <br>
Total <%= counters.increment ("Blueidea")%> visit blueidea.com website <br>


The basic code is similar, mainly is the Counters.Increment function accumulates 1.

You can make a simple voting system based on the additive.

3,vote.asp

<%
Set Counters=server.createobject ("MSWC. Counters ")
Vote = Request.QueryString ("site")
Counters.Increment (vote)
%>
Vote for your favorite Site:
<form name= "Voting for Sites" method= "Get" action= "vote.asp" >
<input type= "RADIO" name= "Site" value= "Cnbruce" >cnbruce
<input type= "RADIO" name= "Site" value= "IT365CN" >IT365CN
<input type= "RADIO" name= "Site" value= "Blueidea" >blueidea
<br><input type= "Submit" value= "Submit Vote" >
</FORM>
Current Vote tally:<br>
Cnbruce: <% =counters.get ("Cnbruce")%><br>
IT365CN: <% = Counters.Get ("IT365CN")%><br>
Blueidea: <% = Counters.Get ("Blueidea")%><br>


The function is to select a radio button to submit, its corresponding value is Counters.Increment

Third, Counters.remove
This object is the function of the count empty.

4,remove.asp

<%
Set Counters=server.createobject ("MSWC. Counters ")
Vote = Request.QueryString ("site")
Clear = Request.QueryString ("clear")
Counters.Increment (vote)
If not clear= "" Then
Counters.remove ("Cnbruce")
Counters.remove ("IT365CN")
Counters.remove ("Blueidea")
End If
%>
Vote for your favorite Site:
<form name= "Voting for Sites" method= "Get" action= "remove.asp" >
<input type= "RADIO" name= "Site" value= "Cnbruce" >cnbruce
<input type= "RADIO" name= "Site" value= "IT365CN" &GT;IT356CN
<input type= "RADIO" name= "Site" value= "Blueidea" >blueidea
<br><input type= "Submit" value= "Submit Vote" >
</FORM>
Current Vote tally:<br>
Cnbruce: <% =counters.get ("Cnbruce")%><br>
IT365CN: <% = Counters.Get ("IT365CN")%><br>
Blueidea: <% = Counters.Get ("Blueidea")%>
<form name= "Clear Counters" method= "get" action= "remove.asp" >
<br><input type= "SUBMIT" value= "clear the Counters" name= "clear" >
</FORM>


By today's contact, it can be concluded that a simple counting system already has two methods: using the Application object, using the counters component. But more also use FSO, or need a database is something slowly talk.


There are a few things to watch out for when we use ASP components:

When we use server components to create objects, there are generally two methods. The more common approach is to use the CreateObject () method of the server object, like this:
SET Nextlink = Server.CreateObject ("MSWC. Nextlink ")

This approach works well, but there is a way to save system resources. Because when you create an object with the Server.CreateObject () method, the object is immediately loaded, and the system allocates memory and other resources to the object.

The second way to create an object is to use the HTML <OBJECT> tag, which is the syntax:
<object runat=sever id=nextlink progid= "MSWC. Nextlink "></OBJECT>

When you create an object with the <OBJECT> tag, the object you are creating is not loaded immediately, but until the object's methods or properties are referenced for the first time. In this way, we will save some resources. It's not much, but it helps save server resources when your system has to endure nearly million clicks a day. (pine)

As it says: Our counters can be placed in Global.asa.

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

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.