欢迎光临
我们一直在努力

Asp.Net中的记数器[初级]-.NET教程,Asp.Net开发

建站超值云服务器,限时71元/月

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=./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>

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » Asp.Net中的记数器[初级]-.NET教程,Asp.Net开发
分享到: 更多 (0)