访问量的统计方法据说有好几种,我也没都搞明白,有利用ajax的,写js脚本的,我用的这个也不知道是什么方法。

在生成的静态页面中有这样一句代码:<script src="http://localhost/.NET/NewsFiles/ClickCount.aspx?NewsId=46"></script>,其中的NewsId是生成静态页面的时候替换的。

其中ClickCount.aspx页面除了page指令都删除,如下:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ClickCount.aspx.cs" Inherits="ClickCount.NewsFiles_ClickCount" %>

ClickCount.aspx.cs的代码如下:

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;
using  Mysqlserver; // 数据库操作类
namespace  ClickCount
{
    
public partial class NewsFiles_ClickCount : System.Web.UI.Page
    
{
        
protected void Page_Load(object sender, EventArgs e)
        
{
            
string strNewsId = Request.QueryString["NewsId"].ToString();//接收传过来的新闻id
            SqlServerDataBase db = new SqlServerDataBase();//建立数据对象
            db.Update("update inNews set ViewCount=ViewCount+1 where NewsId='" + strNewsId + "'", null);//更新加1
            DataSet ds = db.Select("select ViewCount from inNews where NewsId='" + strNewsId + "'", null);//选择更新后的浏览值
            if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
                Response.Write(
"0");
            
else
            
{
                
string count = ds.Tables[0].Rows[0]["ViewCount"].ToString();
                Response.Write(
"document.write("" + count + "")");//很关键
            }

        }


    }

}

 

特别要注意要把ClickCount.aspx的其余代码删除,要不会报错。

更多推荐