ASPNET9月28日笔记:网站访问计数器
2006-09-28 15:42
316 查看
1、网站访问计数器
(1)在Global.asax中作如下修改
protected void Application_Start(Object sender, EventArgs e)
{
StreamReader rd = new StreamReader(Server.MapPath("Counter.txt"));//读Counter.txt文件,里面记录访问的次数。
int Num = int.Parse(rd.ReadLine());
Application.Lock();
Application["Counter"] = Num;//Application["Counter"]记录之前访问数量
Application.UnLock();
rd.Close();
}
protected 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);//Session开始将访问次数加1写入 Counter.txt
sw.WriteLine(Application["Counter"]);
sw.Close();
}
protected void Session_End(Object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(Server.MapPath("Counter.txt"),false);//Session结束将访问次数加1写入Counter.txt
sw.WriteLine(Application["Counter"]);
sw.Close();
}
(2)在工程中添加0-9十张图片,图片的内容就是相应的数字
(3)在web主窗体中添加一个Panel控件MyPanel
(4)在web主窗体的Page_Load事件中添加下列代码
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Image Img;
Label lbl;
string count ;
string graph;
lbl=new Label();
lbl.Text="该站点已被访问次数:";
MyPanel.Controls.Add(lbl);
count =Application["Counter"].ToString();//将访问次数转换为字符
for (int i=0;i<count.Length;i++)
{
//动态生成Image控件
Img=new Image();
//依次将访问次数的百位十位个位取出,将Img的ImageUrl关联到相应的图片“数字”.gif
graph=count.Substring(i,1);
Img.ImageUrl=graph + ".gif";
MyPanel.Controls.Add(Img);
}
};
}
(1)在Global.asax中作如下修改
protected void Application_Start(Object sender, EventArgs e)
{
StreamReader rd = new StreamReader(Server.MapPath("Counter.txt"));//读Counter.txt文件,里面记录访问的次数。
int Num = int.Parse(rd.ReadLine());
Application.Lock();
Application["Counter"] = Num;//Application["Counter"]记录之前访问数量
Application.UnLock();
rd.Close();
}
protected 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);//Session开始将访问次数加1写入 Counter.txt
sw.WriteLine(Application["Counter"]);
sw.Close();
}
protected void Session_End(Object sender, EventArgs e)
{
StreamWriter sw = new StreamWriter(Server.MapPath("Counter.txt"),false);//Session结束将访问次数加1写入Counter.txt
sw.WriteLine(Application["Counter"]);
sw.Close();
}
(2)在工程中添加0-9十张图片,图片的内容就是相应的数字
(3)在web主窗体中添加一个Panel控件MyPanel
(4)在web主窗体的Page_Load事件中添加下列代码
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
Image Img;
Label lbl;
string count ;
string graph;
lbl=new Label();
lbl.Text="该站点已被访问次数:";
MyPanel.Controls.Add(lbl);
count =Application["Counter"].ToString();//将访问次数转换为字符
for (int i=0;i<count.Length;i++)
{
//动态生成Image控件
Img=new Image();
//依次将访问次数的百位十位个位取出,将Img的ImageUrl关联到相应的图片“数字”.gif
graph=count.Substring(i,1);
Img.ImageUrl=graph + ".gif";
MyPanel.Controls.Add(Img);
}
};
}
相关文章推荐
- 20160410servlet学习笔记网站访问次数计数器
- 通过application实现网站访问计数器
- 一个简单的网站访问JS计数器 刷新1次加1次访问
- ASPNET9月28日笔记:广告条控件AdRotator
- 过滤器的使用实现网站访问计数器
- C#网站登录学习笔记(二):访问需登录后才能访问的页面
- arcgis开发笔记【silverlight同步访问WCF和网站数据】
- 个人笔记 asp 02 cookis实现访问次数计数器
- web网站访问计数器
- 使用过滤器实现网站访问计数器的功能
- 一个使用TXT文本的JSP网站访问计数器(不用任何数据库)
- 网站访问计数器
- 笔记4:IIS6发布网站后“对XX路径的访问被拒绝”
- 08-网站访问计数器(jsp内置对象)
- 一个简单的网站访问JS计数器,刷新1次加1次访问
- Servlet过滤器实现网站访问计数器功能
- python爬虫笔记之用cookie访问需要登录的网站
- 《Perl编程24学时教程》笔记第23课 服务器推送和访问次数计数器
- jsp制作网站计数器(同一用户多次访问计数器只加一次)