静态页面中显示文章的点击数

来源:互联网 发布:易投水利软件 编辑:程序博客网 时间:2024/05/21 04:42

在aspx页面中只显示<%@这一行,其余的都删除

在aspx.cs中输出必须使用Response.Write("document.write('" + s + "');");

即:动态页面运行后显示的必须是document.write('Hello');格式的。否则不能在静态页面中调的。

只使用Response.Write不能正确的调用的。

闲话少说了下面把代码贴出来了

一、静态页面(new.htm)

在需要显示点击数的地方加上下面的红色代码段,Default2.aspx页面就是获取点击数的动态页面

<!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>
点击数:<script language="javascript" type="text/javascript" src='Default2.aspx?classid=BB' ></script>
</body>
</html>

二、Default2.aspx(只保留最上面的一行引用代码即可)

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

三、Default2.aspx.cs

Page_Load事件:

string c = Request["ClassID"].ToString();
        SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["cc_2005"].ConnectionString);
        connection.Open();
        SqlCommand sda = new SqlCommand("select hit from class where classid='" + c + "'", connection);
        int s = int.Parse(sda.ExecuteScalar().ToString());
        s++;
        SqlCommand command1 = new SqlCommand("update class set hit="+s+" where classid='" + c + "'", connection);
        command1.ExecuteNonQuery();
        Response.Write("document.write('" + s + "');");
        if (connection.State == ConnectionState.Open)
        {
            connection.Close();
        }

本文的测试环境:WinXP+VS2005

原创粉丝点击