首页 > 编程 > .NET > 正文

Asp.Net中的记数器[初级]

2024-07-10 12:56:11
字体:
来源:转载
供稿:网友

global.asa
----------------------------------------------------------------------
<script language="c#" runat="server">
void session_onstart(){
application.lock();
application["whoson"]=convert.toint32(application["whoson"])+1;
application.unlock();
}
void session_onend(){
application.lock();
application["whoson"]=convert.toint32(application["whoson"])-1;
application.unlock();
}
public void application_onstart(){
application.lock();
application["whoson"]=0;
application.unlock();
}
</script>
-------------------------------------------------------------------------

count_txt.asp

-------------------------------------------------------------------------

<%@ page language="c#"%>
<%@ import namespace="system.io"%>

<script language="c#" runat="server">
public void page_load(object src,eventargs e)
{
//以下为读取文件,当前目录必须有count.txt这个文件否则会出错
streamreader sr=file.opentext(server.mappath(".")+"//count.txt");
application.lock();
application["count"]=sr.readline();
application["count"]=convert.toint32(application["count"])+1;
application.unlock();
sr.close();

//建立文件
streamwriter rw=file.createtext(server.mappath(".")+"//count.txt");
application.lock();
rw.writeline(application["count"]);
application.unlock();
rw.flush(); //写入
rw.close();

count_1.text="您是本站第"+application["count"].tostring()+"位访问者";
}
</script>
<html>
<head>
</head>
<body>
<hr>
<asp:label id="count_1" runat="server"/>
</body>
</html>



global.asax文件与文本记数器的一样,在此省略;
在images目录中需要有0-9.gif十张图片,同样当前目录需要有count.txt这个文件,否则会出错
count_pic.aspx
-----------------------------------------------------------------------------------
<%@ page language="c#" contenttype="text/html" responseencoding="gb2312" %>
<%@ import namespace="system.io"%>
<script language="c#" runat="server">
public void page_load(object src,eventargs e)
{
//以下为读取文件
streamreader sr=file.opentext(server.mappath(".")+"//count.txt");
application.lock();
application["count"]=sr.readline();
application["count"]=convert.toint32(application["count"])+1;
application.unlock();
sr.close();

//建立文件
streamwriter rw=file.createtext(server.mappath(".")+"//count.txt");
application.lock();
rw.writeline(application["count"]);
application.unlock();
rw.flush(); //写入
rw.close();
}
public string g(int counter)
{
string myimage="";
string s=counter.tostring();
//strreplace=replace(strreplace,chr[92]);
for(int i=0;i<=s.length-1;i++)
{
myimage =myimage+"<img src=http://www.163design.net/n/a/images/"+s.substring(i,1)+".gif>";
}
return myimage;
}
</script>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=gb2312">
<title>图形记数器</title>
</head>
<body>
<%=g(convert.toint32((application["count"])))%>
</body>
</html>

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表