vb.net 网页计数器的制作

来源:互联网 发布:淘宝图片尺寸怎么设置 编辑:程序博客网 时间:2024/06/05 04:56

今天下午自己做了一个网页计数器。其实网上有很多的第三方计数器,用了发现速度慢,很不爽的是还要注册,所以自己网上查查资料自己写了一个,很好用。 其实原理也很简单。目前流行的制作方法有两种,一种方法是把“计数”放在数据库中。第二种方法把“计数”放在txt文件中。我采用了后者,主要涉及在global.asax文件中进行文件的读写和计数的累加。

 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 " 组件设计器生成的代码 "

        Public Sub New()
            MyBase.New()

            '该调用是组件设计器所必需的。
            InitializeComponent()

            '在 InitializeComponent() 调用之后添加任何初始化

        End Sub

        '组件设计器所必需的
        Private components As System.ComponentModel.IContainer

        '注意: 以下过程是组件设计器所必需的
        '可以使用组件设计器修改此过程。
        '不要使用代码编辑器修改它。
        <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)
            ' 在应用程序启动时激发
            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)
            ' 在会话启动时激发
            Application.Lock()
            Dim jishu As Integer = 0
            jishu = Integer.Parse(Application("counter").ToString)
            jishu = jishu + 1
            Dim obj As Object = jishu
            Application("counter") = obj


            '将数据记录写入文件
            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)
            ' 在每个请求开始时激发
        End Sub

        Sub Application_AuthenticateRequest(ByVal sender As Object, ByVal e As EventArgs)
            ' 尝试对使用进行身份验证时激发
        End Sub

        Sub Application_Error(ByVal sender As Object, ByVal e As EventArgs)
            ' 在发生错误时激发
        End Sub

        Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
            ' 在会话结束时激发
        End Sub

        Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
            ' 在应用程序结束时激发

            Dim js As Integer
            js = Integer.Parse(Application("counter").ToString)
            '将数据记录写入文件
            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
以上的文件保存在global.asax.vb中。

我比较喜欢用dreamweaver结合vs.net开发,需要用到计数器的地方只需要加一个语句<%=application("counter")%>即可得到计数的值。可以做的好看一点就把数值的每一位分解成单个的字符,如1234分解后对应的图片为,1.gif,2.gif,3.gif,4.gif,显然图片对应的数字更美观。下面是在模版中的部分代码:

     <div align="center">       
         <p>版权所有&copy; 2006  中国科学院遥感应用研究所 </p>
   <FONT face="宋体" color="#330033">您是本站的第
           <% dim s as string
            dim I as integer
            s=cstr(application("counter")+10^6)
            s=mid(s,2,6)
            for I=1 to 6
          %>
          <img src='../image/counter/<%=mid(s,I,1)%>.gif' width='15' height='20'>
          <%next%>位访问者!</FONT>
          </div>

原创粉丝点击