Asp.net 2.0使用Global.asax制作网站计数器的一点心得

我给网站添加了Global.asax,文本文件Site_Counter.txt并写入数字0,代码分别如下:

Global.asax

<% @ Application Language = " C# "   %>
< script runat = " server " >

    
void  Application_Start( object  sender, EventArgs e) 
    
{
        
// 在应用程序启动时运行的代码
        
//读取网站访问数
        System.IO.StreamReader rd = new System.IO.StreamReader(Server.MapPath("Site_Counter.txt"));
        Application.Lock();
        Application[
"Site_Counter"= long.Parse(rd.ReadLine());
        Application.UnLock();
        rd.Close();
    }


    
void  Session_Start( object  sender, EventArgs e) 
    
{
        
// 在新会话启动时运行的代码
        
//更新网站访问数
        Application.Lock();
        Application[
"Site_Counter"= Convert.ToInt64(Application["Site_Counter"]) + 1;
        Application.UnLock();

        System.IO.StreamWriter wr 
= new System.IO.StreamWriter(Server.MapPath("Site_Counter.txt"), false);
        Application.Lock();
        wr.WriteLine(Application[
"Site_Counter"]);
        Application.UnLock();
        wr.Close();
    }

</ script >

然后在需要显示的页面显示系统 就可以了

比如简单的:

Label_Site_Counter.Text  =  Convert.ToString(Application[ " Site_Counter " ]);

假如想显示为图片的话,可以请美工做0-9个数字图片,然后加入下面的代码:呵呵,大功告成,测试下吧,是不是已经看到了呢?

counter =  ( long )Application[ " Site_Counter " ];

public   string  piclong( long  counter)
{
        
string returns="";
        
for(int i=1;i<=counter.ToString().Length;i++)
        
{
              returns
+="<td><img src='Image/"+counter.ToString().Substring(i-11)+".gif' width='14' height='19'></td>";
        }

        
return returns;
 }

 

参考:http://hi.baidu.com/hehenb/blog/item/5a734766ed35a024ab184cd6.html
 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值