ajax的简单应用select(一)

<%@ WebHandler Language="C#" Class="Handler" %>
using System;
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());
 
    }
    public bool IsReusable
    {
        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);
              }
        }
    }
}

    </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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值