<script type="text/javascript">
var xmlHttp;
function createXMLHttpRequest()
{
if(window.ActiveXObject)
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
else if(window.XMLHttpRequest)
{
xmlHttp = new XMLHttpRequest();
}
}
function startRequest(n)
{
var send_string="id="+n;
send_string= encodeURI(send_string)
createXMLHttpRequest();
xmlHttp.onreadystatechange = handleStateChange;
xmlHttp.open("GET","ReadDate.asp",true);
// xmlHttp.setRequestHeader("cache-control","no-cache");
xmlHttp.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
xmlHttp.send(send_string);
}
function handleStateChange()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
document.getElementById("Jcon").innerHTML=xmlHttp.responseText
//把服务器返回的数据插入ID为Jcon的DIV内
}
}
}
</script>
调用:οnclick="startRequest(n)"
ASP服务器端处理程序:
<%
Response.Buffer = True
Response.Expires = 0
Response.CacheControl = "no-cache"
Response.AddHeader "Pragma", "No-Cache"
response.Charset="GB2312" ' //数据返回的编码类型 显示中文数据必须
Dim id,ahtml
id = Request.Form("id") //获得Ajax客户端发送的ID
set myDb=New dbClass
myDb.sqlStr="select * from Town where Tid = "&id
set rs=myDb.rsDB()
if rs.bof and rs.eof then
Response.Write("<ul style='text-align:center'>没有数据!</ul>")
else
ahtml="<ul><table width='100%' border='0' cellspacing='0' cellpadding='0'>"
ahtml=ahtml+"<tr>"
ahtml=ahtml+"<td class='px14'> "&rs("tit")&"</td>"
ahtml=ahtml+"<td rowspan='2' valign='top'><a href='"&rs("picurl")&"'><img src="&rs("picurl")&" width='145' height='68' /></a></td>"
ahtml=ahtml+"</tr>"
ahtml=ahtml+"<tr>"
ahtml=ahtml+"<td><li id='TownCon'><a href='about.asp'>"&LeftTrim(rs("content"),230)&"...</a></li></td>"
ahtml=ahtml+"</tr>"
ahtml=ahtml+"</table></ul>"
Response.Write(ahtml) //输出数据
end if
myDb.cRs(rs) '关闭释放rs
set myDb=nothing
%>
ASP.Net服务器端处理程序(ashx):
<%@ WebHandler Language="C#" Class="MenuHandler" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using Souxiu.Data;
public class MenuHandler : IHttpHandler {
public void ProcessRequest (HttpContext context) {
//不让浏览器缓存
context.Response.Buffer = true;
context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
context.Response.AddHeader("pragma", "no-cache");
context.Response.AddHeader("cache-control", "");
context.Response.CacheControl = "no-cache";
context.Response.ClearContent();
//context.Response.ContentType = "text/plain";
context.Response.ContentType = "text/html";
string menuid = context.Request.QueryString["id"];
context.Response.Write(GetData(menuid).ToString());
context.Response.End();
}
public bool IsReusable {
get {
return false;
}
}
private string GetData(string id)
{
string TxtContent = "";
SqlDataReader dr = null;
SqlParameter[] param = { Helper.CreateInParam("@parentid", SqlDbType.Int, 4, id), };
Helper.RunProc("PROC_SystemMenu", out dr, param);
if (dr.Read())
{
while (dr.Read())
{
TxtContent += " <ul><li οnclick=/"LeftMenu(1)/" ><a href=/"" + dr["ahref"].ToString() + "/">" + dr["menuName"].ToString() + "</a></li></ul>" + "/r/n";
}
}
else
{
TxtContent = "暂无数据!";
}
return TxtContent;
}
}