这些东西。可以在数据库中也可以有文本形式来实现的。你到网上给搜索以下了。估计很多的

解决方案 »

  1.   

    首先建立一个文本文件counter.txt,打开文件输入一个大于0的整数作为访问记录的初始值。  
    下面我们就可以正式的编写计数器的程序了。  
    listing 1是webform1.aspx,主要是用于显示从文件中读出的访问次数的记录。由于在整个应用程序生命周期中,Application 对象都是有效的,所以在不同的页面中都可以对它进行存取,就像使用全局变量一样方便。  
    在代码中,使用<%=Application["counter"]%>来表示访问次数记录。  
    程序代码如下:  
    listing1 -----webform1.aspx-----  
    <%@ Page language="c#" Src="WebForm1.aspx.cs" Inherits="counter1.WebForm1" %>  
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >  
    <HTML>  
    <HEAD>  
    <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">  
    <meta name="CODE_LANGUAGE" Content="C#">  
    </HEAD>  
    <body>  
    <form id="Form1" method="post" runat="server">  
    <FONT face="宋体">您是第<%=Application["counter"]%>位访问者!</FONT>  
    </form>  
    </body>  
    </HTML>  
    Listing 2和listing3是global.asax和global.asax.cs文件代码,当执行webform1.aspx文件之前会执行它们。在global.asax.cs文件中,定义了一些事件和其响应代码,主要是用于读写文件和数值累加。  
    Listing 2 -----global.asax----  
    <%@ Application Src="Global.asax.cs" Inherits="counter2.Global" %>  
    listing 3 -----global.asax.cs-----  
    using System;  
    using System.Collections;  
    using System.ComponentModel;  
    using System.Web;  
    using System.Web.SessionState;  
    using System.IO ;  
    namespace counter2  
    {  
    public class Global : System.Web.HttpApplication  
    {  
    protected void Application_Start(Object sender, EventArgs e)  
    {  
    uint count=0;  
    StreamReader srd;  
    //取得文件的实际路径  
    string file_path=Server.MapPath ("counter.txt");  
    //打开文件进行读取  
    srd=File.OpenText (file_path);  
    while(srd.Peek ()!=-1)  
    {  
    string str=srd.ReadLine ();  
    count=UInt32.Parse (str);  
    }  
    object obj=count;  
    Application["counter"]=obj;  
    srd.Close ();  
    }  protected void Session_Start(Object sender, EventArgs e)  
    {  
    Application.Lock ();  
    //数值累加,注意这里使用了装箱(boxing)  
    uint jishu=0;  
    jishu=(uint)Application["counter"];  
    jishu=jishu+1;  
    object obj=jishu;  
    Application["counter"]=obj;  
    //将数据记录写入文件  
    string file_path=Server.MapPath ("counter.txt");  
    StreamWriter fs=new StreamWriter(file_path,false);  
    fs.WriteLine (jishu);  
    fs.Close ();  
    Application.UnLock ();  
    }  
    protected void Application_BeginRequest(Object sender, EventArgs e)  
    {  
    }  
    protected void Application_EndRequest(Object sender, EventArgs e)  
    {  
    }  
    protected void Session_End(Object sender, EventArgs e)  
    {  
    }  
    protected void Application_End(Object sender, EventArgs e)  
    {  
    //装箱  
    uint js=0;  
    js=(uint)Application["counter"];  
    //object obj=js;  
    //Application["counter"]=js;  
    //将数据记录写入文件  
    string file_path=Server.MapPath ("counter.txt");  
    StreamWriter fs=new StreamWriter(file_path,false);  
    fs.WriteLine(js);  
    fs.Close ();  
    }  
    }