当前位置: 移动技术网 > IT编程>开发语言>.net > asp.net 实现静态页面累加访问量的三种方式

asp.net 实现静态页面累加访问量的三种方式

2017年12月12日  | 移动技术网IT编程  | 我要评论

国务院总理是谁,重生之宫斗炮灰的归来,山东同城

静态页面 statichtml.html
复制代码 代码如下:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>统计动态页面访问量的几种方法</title>
</head>
<body>
这是在层中显示
<div id="pv"></div>
<script src="addnumber.aspx"></script>
</body>
</html>

累加页面 addnumber.aspx
<%@ page language="c#" autoeventwireup="true" codefile="addnumber.aspx.cs" inherits="addnumber" %>
addnumber.aspx.cs
代码
复制代码 代码如下:

public partial class addnumber : system.web.ui.page
{
private static int count = 1;
protected void page_load(object sender, eventargs e)
{
count++;
response.write("var pv=document.getelementbyid('pv'); pv.innertext=" + count + ";");
}
}

第二种方法:
静态页面 statichtml.html
复制代码 代码如下:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>统计动态页面访问量的几种方法</title>
</head>
<body>
这是利用图片进行显示
<img src="imageaddnumber.aspx" alt="这是动态统计的数量" />
</body>
</html>

累加页面 imageaddnumber.aspx
复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codefile="imageaddnumber.aspx.cs" inherits="imageaddnumber" %>

imageaddnumber.aspx.cs
代码
复制代码 代码如下:

public partial class imageaddnumber : system.web.ui.page
{
private static int count = 1;
protected void page_load(object sender, eventargs e)
{
count++;
string pv = count.tostring();
system.drawing.bitmap image = new system.drawing.bitmap((int)math.ceiling((pv.length * 12.5)), 22);
graphics g = graphics.fromimage(image);
//图片背景色
g.clear(color.white);
font font = new system.drawing.font("arial", 12, (system.drawing.fontstyle.bold | system.drawing.fontstyle.italic));
system.drawing.drawing2d.lineargradientbrush brush = new system.drawing.drawing2d.lineargradientbrush(new rectangle(0, 0, image.width, image.height), color.white, color.red, (float)1.2f, true);
g.drawstring(pv,font, brush, 0, 0);
g.drawrectangle(new pen(color.gold), 0, 0, image.width - 1, image.height - 1);
system.io.memorystream ms = new system.io.memorystream();
image.save(ms, system.drawing.imaging.imageformat.gif);
response.clearcontent();
response.contenttype = "image/gif";
response.binarywrite(ms.toarray());
}
}

第三种方法:
静态页面 statichtml.html
复制代码 代码如下:

<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>统计动态页面访问量的几种方法</title>
</head>
<body>
这是利用ajax实现
<div id="ajaxpv"></div>
<script language="javascript" type="text/javascript">
function addpv(){
//建立跨浏览器的xmlhttprequest对象
var xmlhttp;
try{
xmlhttp= new activexobject('msxml2.xmlhttp');
}catch(e){
try{
xmlhttp= new activexobject('microsoft.xmlhttp');
}catch(e){
try{
xmlhttp= new xmlhttprequest();
}catch(e){}
}
}
//创建请求
xmlhttp.open("get","ajaxpv.aspx?news=1");
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readystate==4){
if(xmlhttp.status==200){
//根据responsetext判断用户名是否存在
var repv=xmlhttp.responsetext;
var mypv=document.getelementbyid("ajaxpv");
mypv.innerhtml=repv;
/*alert(repv);*/
}else{
alert("网络失败。");
}
}
} ;
xmlhttp.send(null);
window.settimeout("addpv",1000);
}
window.onload=addpv;
</script>
</body>
</html>

累加页面 ajaxpv.aspx
复制代码 代码如下:

<%@ page language="c#" autoeventwireup="true" codefile="ajaxpv.aspx.cs" inherits="ajaxpv" %>

ajaxpv.aspx.cs
复制代码 代码如下:

public partial class ajaxpv : system.web.ui.page
{
private static int count=1;
protected void page_load(object sender, eventargs e)
{
//累加到数据库
//读取数据库中数据,目前
count = 5;
response.write(count);
}
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网