以下示例,不使用任何的无刷新组件,直接通过XmlHttp完成!
一、JS文件代码
// JScript 文件
<!--
var MyXmlHttp= false;
try
{
MyXmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch(e)
{
try
{
MyXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e2)
{
MyXmlHttp = false;
}
}
if (!MyXmlHttp && typeof XMLHttpRequest != 'undefined')
{
MyXmlHttp = new XMLHttpRequest();
}
function OnMessageBack()
{
//判断请求状态及HTTP状态是否都能满足条件
if (MyXmlHttp.readystate==4 && MyXmlHttp.status==200)
{
//将返回的文本打印到页面上
document.write (MyXmlHttp.responsetext);
}
}
function SendToServer()
{
var url ="文件名.ashx";
MyXmlHttp.open("GET", url, false);
MyXmlHttp.onreadystatechange = GetServerData;
MyXmlHttp.send(null);
}
function GetServerData()
{
if (MyXmlHttp.readyState < 4)
{
document.getElementById("px").innerHTML="正在加载数据……请稍后!";
}
if (MyXmlHttp.readyState == 4)
{
var response = MyXmlHttp.responseText;
var pp = document.getElementById("px");
if(response!="")
{
var text="";
var array=response.split(";");
for(var i=0;i<array.length;i++)
{
//得到数据
var name=array[i].split(",")[1];
var datetime=array[i].split(",")[3];
var suggestion=array[i].split(",")[2];
//下面不用说了吧
text+="<div id='SuggestionListDiv'>用户名:"+name+";评论时间:"+datetime+";<br>评论内容:<br>"+suggestion+"</div><br>";
}
pp.innerHTML=text;
}
else
{
pp.innerHTML="目前尚无任何评论…";
}
}
}
-->
二、ASHX文件代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security; //加密
using System.Text;
using System.Data.SqlClient;
using System.Web.SessionState;
public class GetSuggestion : IHttpHandler,IRequiresSessionState
{
public void ProcessRequest (HttpContext context)
{
context.Response.ContentType = "text/plain";
//context.Response.Write("Hello World");
DataTable dt = GetSqlDataTable();
string response = string.Empty;
for (int i = 0; i < dt.Rows.Count; i++)
{
response += dt.Rows[i][0].ToString() + "," + dt.Rows[i][1].ToString() + "," + dt.Rows[i][2].ToString() + "," + dt.Rows[i][3].ToString() + ";";
}
if (response.EndsWith(";"))
{
response = response.Substring(0, response.Length - 1);
}
context.Response.Write(response);
context.Response.End();
}
private DataTable GetSqlDataTable()
{
DataClass.SqlDataClas MyClass = new DataClass.SqlDataClas();
SqlConnection conn = new SqlConnection(MyClass.GetMyDataBaseConStr());
string query = "select 字段1,字段2,字段3,字段4 from 表名 where YouID=@MyID order by ID desc";
SqlCommand cmd = new SqlCommand(query, conn);
cmd.Parameters.Add(new SqlParameter("@MyID", SqlDbType.VarChar, 8000));
cmd.Parameters["@MyID"].Value = HttpContext.Current.Session["变量名称"].ToString().Trim();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
try
{
conn.Open();
adapter.Fill(dt);
return dt;
}
catch
{
throw;
}
finally
{
cmd.Dispose();
conn.Close();
}
}
public bool IsReusable {
get
{
return false;
}
}
}
三、ASPX或HTML页面
里面仅需要一个按钮和一个div即可!
“<input id="OK" type="button" value="显示数据" onclick="SendToServer()"/>”
<div id=”px”></div>