應用ajax統計靜態新聞點擊數

     最近做了個新聞系統,新聞都是生成靜態頁面的,在新聞中心頁面通過讀數據庫裏的標題和鏈接來生成新聞列表,用的是datalist:

<asp:datalist id=DataList2 runat="server">
     <ItemTemplate>
<a id="<%# DataBinder.Eval(Container.DataItem, "nid") %> " οnclick="abc(this);" href="<%# DataBinder.Eval(Container.DataItem, "filename") %>" target="_blank"><font color="#0000ff" class=STYLE1>·<%# DataBinder.Eval(Container.DataItem, "biaoti") %></font></a></ItemTemplate>  
</asp:datalist>

 

老闆說要統計新聞點擊數,靜態的不能加代碼啊,在新聞中心頁面上我用的都是html的鏈接形式<a></a>

開始加上linkbutton,用Response.Redirect,但是打開新窗口的時候新聞中心頁面也關了

用Response.Write("<script language=/"javascript/">window.open(/"/");</script>");的時候新聞中心頁面又自動

新加載一次,效果很不好。

故想到了ajax

注意這一行紅字

<a id="<%# DataBinder.Eval(Container.DataItem, "nid") %> " οnclick="abc(this);" href="<%# DataBinder.Eval(Container.DataItem, "filename") %>" target="_blank"><font color="#0000ff" class=STYLE1>·<%# DataBinder.Eval(Container.DataItem, "biaoti") %></font></a>

我把id設爲新聞的nid值,這裏的我開始是id=<%# DataBinder.Eval(Container.DataItem, "nid") %>

沒有加引號,把我給調試得苦死了,人都要瘋了,html還是不行。

οnclick="abc(this);"

js代碼如下:很簡單的

<script language=javascript>
function abc(e)
{
 
    SFAjaxMethod.hitacc(e.id);
  
}

</script>

最後就是ajax後臺方法了:

//統計點擊數
  [Ajax.AjaxMethod()]
  public void hitacc(string nid)
  {
   int dnid=Convert.ToInt32(nid);
   SqlCommand SqlCmd = new SqlCommand();
   SqlCmd.Connection = new SqlConnection(ConfigurationSettings.AppSettings["ConnString"]);
   SqlCmd.CommandText = "update news set hit=hit+1 where nid="+dnid;
   SqlCmd.CommandType = CommandType.Text;
   SqlCmd.Connection.Open();
   SqlCmd.ExecuteNonQuery();
   SqlCmd.Connection.Close();
  }
  //統計點擊數

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章