如何设计ASP.NET计数器 ? 这些东西。可以在数据库中也可以有文本形式来实现的。你到网上给搜索以下了。估计很多的 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 首先建立一个文本文件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 (); } } } 动态添加验证控件问题 打开网页,bin下的URLRewrite未能加载。 updatePanel的困惑 如何返回从数据库中提取出来的值? 怎么让dataview置顶 抽奖翻牌 程序实现 思路 讨论 请教 参数传递出问题,一个字出错?两个字就可以了 高分求救提交表单后刷新页面,结果响应提交按钮中的事件 如何在 datalist中传值问题?! 問個很菜的ASP.NET的問題 ? 很急的一个问题,关于DataTable 一个新手关于 ASP.NET的问题?
下面我们就可以正式的编写计数器的程序了。
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 ();
}
}
}