站点计数器

新建一个网站,然后在Default.aspx里添加代码如下:

 1 < script language = " c# "  runat = " server " >
 2          private   void  Page_Load( object  sender, System.EventArgs e)
 3          {
 4            if (!IsPostBack)
 5                GlobalGraph(MyPanel);
 6        }

 7          void  GlobalGraph(Panel Container)
 8          {
 9            Label lbl;
10            Label lbl2;
11            string count;
12            lbl = new Label();
13            lbl2 = new Label();
14            lbl.Text = "该站点已被访问次数:";
15            Container.Controls.Add(lbl);
16            count = Application["Counter"].ToString();
17            for (int i = 0; i < count.Length; i++)
18            {
19                 lbl2.Text=count.Substring(i, 1);    
20               
21                Container.Controls.Add(lbl2 );  //加载到控件容器
22            }

23        }
    
24      </ script >
25
26 < body style = " background-color:White " >
27 < p >< strong style = "   font-family:@楷体_GB2312 " > Welcome to you !</ strong >   </ p >
28      < form id = " form1 "  runat = " server " >
29      < div >
30      < asp:Panel id = " MyPanel "  runat = " server "  width = " 50% "  BorderColor = " #C0FFC0 "  BorderStyle = " Groove " ></ asp:Panel >
31      </ div >
32      </ form >
33 </ body >
添加新项Global.asax文件,先声名,添加代码如下:
<% @ Import Namespace = " System.Web.SessionState "   %>
<% @ Import Namespace = " System.IO "   %>
在相应位置添加代码如下:
  void  Application_Start( object  sender, EventArgs e) 
    
{
        StreamReader rd 
= new StreamReader(Server.MapPath("counter.txt"));
        
int nNum = int.Parse(rd.ReadLine());
        Application.Lock();
        Application[
"Counter"= nNum;
        Application.UnLock();
        rd.Close();

    }


    
void  Session_Start( object  sender, EventArgs e) 
    
{
        Application.Lock();
        Application[
"Counter"= Convert.ToInt32(Application["Counter"]) + 1;
        Application.UnLock();
        
//写入
        StreamWriter sw = new StreamWriter(Server.MapPath("counter.txt"), false);//false为不追加
        sw.WriteLine(Application["Counter"]);
        sw.Close();
    }


    
void  Session_End( object  sender, EventArgs e) 
    
{
        
//写入
                    StreamWriter sw = new StreamWriter(Server.MapPath("counter.txt"),false);//false为不追加
                    sw.WriteLine(Application["Counter"]);
                    sw.Close();
    }
最后你还要在程序目录里新建Counter.txt文件,就OK了
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值