ashx文件,.net一般处理程序。继承IHttpHandler接口,HttpContext实现个别HTTP请求。
实现:利用本地SQL2000默认的 northwind 数据库
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Text;
public class Handler : IHttpHandler {
public void ProcessRequest (HttpContext context)
{
/*---------------------------自定义代码----------------------------*/
string str = @"server = .;uid =sa;password = sa;database = northwind";
//取数据库中的表
SqlConnection Con = new SqlConnection(str);
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = new SqlCommand("select * from Employees", Con);
Con.Open();
sda.Fill(ds);
Con.Close();
//将表填充到DataTable
DataTable dt = ds.Tables[0];
// 构造输出流格式
StringBuilder strPart = new StringBuilder();
strPart.Append("
for (int i = 0; i < 5; i++)
{
strPart.AppendFormat("
{1}", dt.Rows[i]["EmployeeID"],dt.Rows[i]["LastName"]);}
strPart.Append("
");string s = strPart.ToString();
//根据HTTP局部请求返回流到页面
context.Response.Write(s);
}
public bool IsReusable
{
get {
return false;
}
}
}
运行下这个ASHX文件将地址复制下来
在静态页面调用这个ASHX文件
无标题页function list(url,divList) //ASHX文件的地址,留输出DIV的ID(参数)
{
var xmlHttp;
try
{
xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
}
catch(e)
{
xmlHttp = new XMLHttpRequest();
}
xmlHttp.open("get",url,true);
xmlHttp.onreadystatechange = function()
{
if(xmlHttp.readyState == 4)
{
if(xmlHttp.status == 200)
{
document.getElementById(divList).innerHTML =xmlHttp.responseText;
}
}
}
xmlHttp.send(null);
}