统计静态页面(新闻)访问量的问题,高手帮忙看看…………
静态页面是由程序动态成生的,
里面的代码是通过 <script src=http://localhost/.NET/NewsFiles/ClickCount.aspx?NewsId=46> </script> 调用一个动态页面的。
其中:
ClickCount.aspx
<asp:Label ID= "Label1 " runat= "server " Width= "173px "> </asp:Label>
ClickCount.aspx.cs一开始我是这样写的
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=46 ", null);
SqlServerDataBase db1 = new SqlServerDataBase();
DataSet ds = db1.Select( "select ViewCount from inNews where NewsId=46 ", null);
Response.Write(ds.Tables[0].Rows[0][ "ViewCount "].ToString());
Label1.Text = ds.Tables[0].Rows[0][ "ViewCount "].ToString();
}
后来在群里有人说应该这样:
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=46 ", null);
}
protected override void Render(HtmlTextWriter writer)
{
SqlServerDataBase db1 = new SqlServerDataBase();
DataSet ds = db1.Select( "select ViewCount from inNews where NewsId=46 ", null);
Response.Write(ds.Tables[0].Rows[0][ "ViewCount "].ToString());
Label1.Text = ds.Tables[0].Rows[0][ "ViewCount "].ToString();
}
------------
但这两个写法都不对,都不能现实浏览次数,但其中的更新代码是正确的,每次访问都加1。
单独访问ClickCount.aspx页面, Response.Write(ds.Tables[0].Rows[0][ "ViewCount "].ToString());能显示,但 Label1.Text = ds.Tables[0].Rows[0][ "ViewCount "].ToString();不能显示。
请高手帮忙……谢谢……
[解决办法]
参考http://g.ssxz.com
把统计做成脚本
[解决办法]
强烈建议楼主将新闻静态页面的查看次数统计功能用 AJAX 实现
[解决办法]
<script 是和本页面并行运行的,怎么写都出不来你想要的效果
建议改为
<iframe src=http://localhost/.NET/NewsFiles/ClickCount.aspx?NewsId=46 frameborder= "0 " scrolling= "no " > </iframe>
测试一下效果
[解决办法]
放在脚本里,每次静态页面被调用的时候,就会自动的调用那个脚本了,这样就可以达到统计的目的了!
[解决办法]
楼主查下ajax的实现方式
[解决办法]
<script src=http://localhost/.NET/NewsFiles/ClickCount.aspx?NewsId=46> </script>
要保证返回的代码复合js的语法。
比如
document.getElementById( "Lbl_Count ").InnerText = "100 ";
那么在ClickCount.aspx.cs 的 Page_Load 最后只需要
Response.Write( "document.getElementById( "Lbl_Count ").InnerText = '100 '; ");
就可以了。
另外 ClickCount.aspx 只保留第一行代码( <%@ Page ......),其他的都要去掉。