Ajax轮询

来源:互联网 发布:淘宝上的伟哥药能买吗 编辑:程序博客网 时间:2024/06/07 19:20

 

 

前台的HTML代码:

[javascript] view plaincopy
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="mainTalk.aspx.cs" Inherits="wj_test.Talk.mainTalk" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head runat="server">  
  5.     <title></title>  
  6.     <mce:script src="../Scripts/jquery-1.4.1.js" mce_src="Scripts/jquery-1.4.1.js" type="text/javascript"></mce:script>  
  7.     <mce:style><!--  
  8.         body  
  9.         {  
  10.             font-size: 12px;  
  11.         }  
  12.         #content  
  13.         {  
  14.             width: 200px;  
  15.             height: 50px;  
  16.             border: solid 1px #dedede;  
  17.             background-color: #eee;  
  18.         }  
  19.         #news  
  20.         {  
  21.             width: 200px;  
  22.             height: 13px;  
  23.             font-weight: bold;  
  24.         }  
  25.         li  
  26.         {  
  27.             height: 16px;  
  28.             line-height: 16px;  
  29.             list-style: none;  
  30.             color: #226B19;  
  31.         }  
  32.         a  
  33.         {  
  34.             text-decoration: none;  
  35.             color: #9E3423;  
  36.         }  
  37.       
  38. --></mce:style><style mce_bogus="1">        body  
  39.         {  
  40.             font-size: 12px;  
  41.         }  
  42.         #content  
  43.         {  
  44.             width: 200px;  
  45.             height: 50px;  
  46.             border: solid 1px #dedede;  
  47.             background-color: #eee;  
  48.         }  
  49.         #news  
  50.         {  
  51.             width: 200px;  
  52.             height: 13px;  
  53.             font-weight: bold;  
  54.         }  
  55.         li  
  56.         {  
  57.             height: 16px;  
  58.             line-height: 16px;  
  59.             list-style: none;  
  60.             color: #226B19;  
  61.         }  
  62.         a  
  63.         {  
  64.             text-decoration: none;  
  65.             color: #9E3423;  
  66.         }  
  67.     </style>  
  68.     <mce:script type="text/javascript"><!--  
  69.         function getData() {//获取留言数据  
  70.             var temp = "";  
  71.             $.getJSON("Data.aspx", { ID: "2", time: new Date() }, function (json) {//利用ajax返回json的方式  
  72.                 if (json.length > 0) {//返回有数据  
  73.                     $("#news").html(" <font color='Red'>有新消息:</font>");  
  74.                     for (var i = 0; i < json.length; i++) {//循环json,生成需要的标签  
  75.                         temp += "<li id='" + json[i].ID + "' ><img src="../img/t.gif" mce_src="img/t.gif" style='height: 14px; width: 14px' /><a  href="#" mce_href="#" onclick='getHref(" + json[i].ID + "); return false;' >" + json[i].SendID + "给您留言了</a></li>";  
  76.                     }  
  77.                     $("#info li").remove(); //移除之前的元素li  
  78.                     $("#info").append(temp); //追加新的  
  79.                     $("li").hide(); //隐藏全部,只显示前2条  
  80.                     $("li:eq(0)").show();  
  81.                     $("li:eq(1)").show();  
  82.                 }  
  83.                 else {  
  84.                     $("#news").html(" <font color='#999'>无新消息:</font>"); //无数据时的提示  
  85.                 }  
  86.             })  
  87.         };  
  88.         $(function () {//间隔3s自动加载一次  
  89.             getData(); //首次立即加载  
  90.             window.setInterval(getData, 3000); //循环执行!!  
  91.             }  
  92.          );  
  93.         function getHref(id) {//重定向页面并且移除当前li标签  
  94.             location.href = 'ShowAndRe.aspx?ID=' + id;  
  95.             $(document.getElementById(id)).remove();  
  96.         }  
  97.       
  98. // --></mce:script>  
  99. </head>  
  100. <body>  
  101.     <form id="form1" runat="server">  
  102.     <div id="content">  
  103.         <div id="news">  
  104.         </div>  
  105.         <ul id="info" style="margin: 0 0 0 0;" mce_style="margin: 0 0 0 0;">  
  106.         </ul>  
  107.     </div>  
  108.     </form>  
  109. </body>  
  110. </html>  

 

后台的C#代码,获取数据:

[c-sharp] view plaincopy
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;  
  6. using System.Web.UI.WebControls;  
  7. using System.Data.SqlClient;  
  8. using System.Data;  
  9. using System.Text;  
  10. namespace wj_test.Talk  
  11. {  
  12.     public partial class Data : System.Web.UI.Page  
  13.     {  
  14.         protected void Page_Load(object sender, EventArgs e)  
  15.         {  
  16.             if (Request.QueryString["ID"] != null)  
  17.             {  
  18.                 string id = Request.QueryString["ID"].ToString();  
  19.                 SqlConnection scn = new DB().getDB();  
  20.                 scn.Open();  
  21.                 string str = "select * from test_talk where Station=0 and ReID="+id+" order by ID desc";  
  22.                 SqlDataAdapter ada = new SqlDataAdapter(str, scn);  
  23.                 DataTable dt = new DataTable();  
  24.                 ada.Fill(dt);  
  25.                 scn.Close();  
  26.                 if (dt.Rows.Count > 0)  
  27.                 {  
  28.                     string json = DataTable2Json(dt);  
  29.                     Response.Write(json);  
  30.                 }  
  31.                 else  
  32.                     Response.Write("{}");//为空时返回{} 使json对象的length=0  
  33.             }  
  34.         }  
  35.         public static string DataTable2Json(DataTable dt)  
  36.         {  
  37.             StringBuilder jsonBuilder = new StringBuilder();  
  38.             jsonBuilder.Append("[");  
  39.             for (int i = 0; i < dt.Rows.Count; i++)  
  40.             {  
  41.                 jsonBuilder.Append("{");  
  42.                 for (int j = 0; j < dt.Columns.Count; j++)  
  43.                 {  
  44.                     jsonBuilder.Append("/"");  
  45.                     jsonBuilder.Append(dt.Columns[j].ColumnName);  
  46.                     jsonBuilder.Append("/":/"");  
  47.                     jsonBuilder.Append(dt.Rows[i][j].ToString());  
  48.                     jsonBuilder.Append("/",");  
  49.                 }  
  50.                 jsonBuilder.Remove(jsonBuilder.Length - 1, 1);  
  51.                 jsonBuilder.Append("},");  
  52.             }  
  53.             jsonBuilder.Remove(jsonBuilder.Length - 1, 1);  
  54.             jsonBuilder.Append("]");  
  55.             return jsonBuilder.ToString();  
  56.         }    
  57.     }  
  58. }  

0 0