Production of VB.net web page counters

Source: Internet
Author: User

This afternoon, I made a web page counter. In fact, there are a lot of third-party counters on the Internet, and the discovery speed is slow, it is very uncomfortable to register, so I checked the information on the Internet and wrote it myself, it is very easy to use. In fact, the principle is also very simple. Currently, there are two popular production methods: one is to put "count" in the database. The second method puts "count" in the TXT file. I used the latter, mainly involving reading, writing, and counting files in the global. asax file.

Imports system. Web
Imports system. Web. sessionstate
Imports system. Io
Imports system. Collections
Imports system. componentmodel
Imports system

Namespace transplat
Public class global
Inherits system. Web. httpapplication

# Region "code generated by the component designer"

Public sub new ()
Mybase. New ()

'The call is required by the component designer.
Initializecomponent ()

'Add any initialization after initializecomponent () is called.

End sub

'Required by the component designer
Private components as system. componentmodel. icontainer

'Note: The following process is required by the component designer
'You can use the component designer to modify this process.
'Do not use the code editor to modify it.
<System. Diagnostics. debuggerstepthrough ()> private sub initializecomponent ()
Components = new system. componentmodel. Container
End sub

# End Region

Sub application_start (byval sender as object, byval e as eventargs)
'Triggered when the application is started
Dim count as integer = 0
Dim SRD as streamreader
Dim file_path as string = server. mappath ("counter.txt ")
SRD = file. opentext (file_path)
Dim STR as string
Do While SRD. Peek ()> = 0

STR = SRD. Readline ()
Count = integer. parse (STR)
Loop
Dim OBJ as object = count
Application ("counter") = OBJ
SRD. Close ()

End sub

Sub session_start (byval sender as object, byval e as eventargs)
'Triggered during session startup
Application. Lock ()
Dim jishu as integer = 0
Jishu = integer. parse (Application ("counter"). tostring)
Jishu = jishu + 1
Dim OBJ as object = jishu
Application ("counter") = OBJ

'Write data records to a file
Dim file_path as string = server. mappath ("counter.txt ")
Dim FS as streamwriter = new streamwriter (file_path, false)
FS. writeline (jishu)
FS. Close ()

End sub

Sub application_beginrequest (byval sender as object, byval e as eventargs)
'Triggered at the beginning of each request
End sub

Sub application_authenticaterequest (byval sender as object, byval e as eventargs)
'Triggered when trying to authenticate the use
End sub

Sub application_error (byval sender as object, byval e as eventargs)
'Triggered when an error occurs
End sub

Sub session_end (byval sender as object, byval e as eventargs)
'Triggered at the end of the session
End sub

Sub application_end (byval sender as object, byval e as eventargs)
'Triggered at the end of the application

Dim JS as integer
JS = integer. parse (Application ("counter"). tostring)
'Write data records to a file
Dim file_path as string = server. mappath ("counter.txt ")
Dim FS as streamwriter = new streamwriter (file_path, false)
FS. writeline (JS)
FS. Close ()

End sub

End Class
End namespace
The preceding files are stored in global. asax. VB.

I prefer using Dreamweaver in combination with vs.net. To use a counter, you only need to add a statement <% = Application ("counter") %> to get the Count value. Obviously, the number corresponding to the image is more beautiful. The following is some code in the template:

<Div align = "center">
<P> All Rights Reserved & copy; 2006 Institute of Remote Sensing Application, Chinese Emy of Sciences </P>
<Font face = "" color = "#330033"> you are the first
<% Dim s as string
Dim I as integer
S = CSTR (Application ("counter") + 10 ^ 6)
S = mid (S, 2, 6)
For I = 1 to 6
%>
. gif 'width = '15' Height = '20'>
<% Next %> visitor! </Font>
</Div>

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.