<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class Handler : IHttpHandler {
using System.Web;
using System.Data;
using System.Data.SqlClient;
public class Handler : IHttpHandler {
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/plain";
string majorid = context.Request.Params["major"].ToString().Trim();
string major = "";
//避免不识别汉字
if (majorid == "1")
{
major = "一年级";
}
else if (majorid == "2")
{
major = "二年级";
}
//从数据库提取数据
SqlConnection conn = new SqlConnection("server=.;database=checkattendance;uid=chenxiuhong;pwd=123456;");
SqlDataAdapter da = new SqlDataAdapter("select gradename from tab_grade where gradename like '" + major + "%'", conn);
DataSet ds = new DataSet();
da.Fill(ds);
//定义响应文本的格式,以返回
string result = "";
foreach (DataRow row in ds.Tables[0].Rows)
{
result += row[0].ToString().Trim() + " ";
}
context.Response.Write(result.Trim());
}
{
context.Response.ContentType = "text/plain";
string majorid = context.Request.Params["major"].ToString().Trim();
string major = "";
//避免不识别汉字
if (majorid == "1")
{
major = "一年级";
}
else if (majorid == "2")
{
major = "二年级";
}
//从数据库提取数据
SqlConnection conn = new SqlConnection("server=.;database=checkattendance;uid=chenxiuhong;pwd=123456;");
SqlDataAdapter da = new SqlDataAdapter("select gradename from tab_grade where gradename like '" + major + "%'", conn);
DataSet ds = new DataSet();
da.Fill(ds);
//定义响应文本的格式,以返回
string result = "";
foreach (DataRow row in ds.Tables[0].Rows)
{
result += row[0].ToString().Trim() + " ";
}
context.Response.Write(result.Trim());
}
public bool IsReusable
{
get
{
return false;
}
}
{
get
{
return false;
}
}
}
------------------------------------------------
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type ="text/javascript" >
var xmlHttp;
var requesttype="";
function createXmlHttp()
{
if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();//mozilla浏览器
}
else if(window.ActiveXObject)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");//IE旧版本
}
catch(e)
{
}
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//IE新版本
}
catch(e)
{
}
if(!xmlHttp)
{
window.alert("不能创建XMLHTTPREQUEST对象!");
return false;
}
}
}
function startRequest()
{
createXmlHttp();
//使用GET方式
var url="Handler.ashx?major="+document.getElementById("major").options.value;
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=bindclass;
xmlHttp.send(null);
}
function bindclass()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var selclass=document.getElementById("class");
//如果被动(被激发的)下拉有内容,要首先清空
while(selclass.hasChildNodes())
{
var node=selclass.children(0)
selclass.removeChild(node);
}
//获取响应内容
var result=xmlHttp.responseText;
//分割以方便绑定
var optiontext=result.split(' ');
//将分割后的内容绑定到被动下拉列表
for(var i=0;i<optiontext.length;i++)
{
var optionnode=document.createElement("OPTION");
optionnode.text=optiontext[i];
selclass.add(optionnode);
}
}
}
}
<html xmlns=" http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>无标题页</title>
<script type ="text/javascript" >
var xmlHttp;
var requesttype="";
function createXmlHttp()
{
if(window.XMLHttpRequest)
{
xmlHttp=new XMLHttpRequest();//mozilla浏览器
}
else if(window.ActiveXObject)
{
try
{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");//IE旧版本
}
catch(e)
{
}
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");//IE新版本
}
catch(e)
{
}
if(!xmlHttp)
{
window.alert("不能创建XMLHTTPREQUEST对象!");
return false;
}
}
}
function startRequest()
{
createXmlHttp();
//使用GET方式
var url="Handler.ashx?major="+document.getElementById("major").options.value;
xmlHttp.open("GET",url,true);
xmlHttp.onreadystatechange=bindclass;
xmlHttp.send(null);
}
function bindclass()
{
if(xmlHttp.readyState==4)
{
if(xmlHttp.status==200)
{
var selclass=document.getElementById("class");
//如果被动(被激发的)下拉有内容,要首先清空
while(selclass.hasChildNodes())
{
var node=selclass.children(0)
selclass.removeChild(node);
}
//获取响应内容
var result=xmlHttp.responseText;
//分割以方便绑定
var optiontext=result.split(' ');
//将分割后的内容绑定到被动下拉列表
for(var i=0;i<optiontext.length;i++)
{
var optionnode=document.createElement("OPTION");
optionnode.text=optiontext[i];
selclass.add(optionnode);
}
}
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<select id="major" onchange ="startRequest();">
<option value ="1">一年级</option>
<option value ="2" >二年级</option>
</select>
<select id="class">
</select>
</div>
</form>
</body>
</html>