AJAX+Js实现html页面点击数自动更新

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>html页面点击数的自动更新</title>
  </head>
<body  >

<span id="kk"></span>
<script language="javascript" type ="text/javascript">
var xmlHttp;
if (window.ActiveXObject){
   xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}
else if (window.XMLHttpRequest){
   xmlHttp=new XMLHttpRequest();
}
var now = new Date();

var url="articleAddBrow.aspx?cid=180";
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=yang;
xmlHttp.send(null);

function yang()
{
if(xmlHttp.readyState==4){
  if(xmlHttp.status==200){
    var mes= xmlHttp.responseText;
    document.getElementById("kk").innerHTML=mes
  }
}
}
</script>

</body>
</html>

aspx页面

 

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class AddBrow : System.Web.UI.Page
{
    public string tableName;
    public string item;
    public int classID;
    public int hot=0;

    protected void Page_Load(object sender, EventArgs e)
    {

        classID = int.Parse(Request.QueryString["cid"].ToString());
        
        addbrows();
    }

    public void addbrows()
    {
        DB ydy = new DB();
        SqlConnection ydyconn = ydy.getcon();
        ydyconn.Open();
        SqlCommand ydycom = new SqlCommand("Article set Hot=Hot+1 where id='" + classID + "' ", ydyconn);
        ydycom.ExecuteNonQuery();
        ydyconn.Close();

        ydyconn.Open();
        SqlCommand ydycomd = new SqlCommand("select * from Article where id='" + classID + "' ", ydyconn);
        SqlDataReader ydydr = ydycomd.ExecuteReader();
        while (ydydr.Read())
        {
            hot = ydydr.GetInt32(8);
            Response.Write(hot);
        }

    }

}

 

 

 

注意:var url="articleAddBrow.aspx?cid=180";//是向页面articleAddBrow.aspx传值,将cid=180换成cid=$$article_id$$生成页面是用数据替换模板,就ok

文章出处:DIY部落(http://www.diybl.com/course/4_webprogram/asp.net/netjs/20090316/161763.html)

你可能感兴趣的:(JavaScript,html,Ajax,String,XHTML,XMLhttpREquest)