连动

ashx 

<%@ WebHandler Language="C#" Class="GetCity" %>

using System;
using System.Web;

public class GetCity : IHttpHandler {
   
    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string CityResult = "";
        Class2 csi = new Class2();
       
        string type = context.Request.QueryString["ProvinceType"].ToString();
       System.Data.DataTable gettable= csi.getCityInfo(Convert.ToInt32(type)).Tables[0];
       for (int i = 0; i < gettable.Rows.Count; i++)
       {
           CityResult += gettable.Rows[i]["CityName"] + ";";
       }
       context.Response.Write(CityResult);

    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}

 

页面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="OmyGood.aspx.cs" Inherits="OmyGood" %>

<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList ID="DropDownList1" runat="server" >
        </asp:DropDownList>
        <asp:DropDownList ID="DropDownList2" runat="server">
        </asp:DropDownList>
    </div>
    </form>
</body>
<script type="text/javascript" language="javascript">
var xmlHttp=false;
document.body.οnlοad=function()
{
document.getElementById("DropDownList2").options[0]=new Option("请先选择省份");

}
function send_request()
{
if(window.XmlHttpRequest)
{
xmlHttp=new XMLHttpRequest();
}
else
{
try{
xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");


}catch(e)
{
try
{
xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
}catch(e)
{}
}

xmlHttp.onreadystatechange=GetCity;
xmlHttp.open("get","GetCity.ashx?provinceType="+document.getElementById('DropDownList1').value,true);
xmlHttp.send(null);

}
function GetCity()
{
if(xmlHttp.readyState==4&&xmlHttp.status==200)
{
if(xmlHttp.responseText!="")
{
   var myarr=new Array(7);
   var ResponseText=xmlHttp.responseText;
   var pos=ResponseText.indexOf(";");
   var i=0;
   while(pos!=-1)
  {
var myText=ResponseText.substring(0,pos);
if(myText!=""){
myarr[i]=myText;
 i++;      
}
 ResponseText=ResponseText.substr(pos+1);
 pos=ResponseText.indexOf(";");
  continue;
                     }
     for(var n=0;n<i;n++)
     {
    document.getElementById("DropDownList2").options[n]=new Option(myarr[n],"");
   document.getElementById("DropDownList2").length=i;
   }


}
   else
                 {
                     document.getElementById("DropDownList2").options[0]=new Option("请先选择省份","");
                     document.getElementById("DropDownList2").length=1;
                 }      

}

}
}


</script>
</html>

 

 

后台代码

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class OmyGood : System.Web.UI.Page
{
 
    protected void Page_Load(object sender, EventArgs e)
    {
        Class2 myclas = new Class2();
        DataTable dt = myclas.getProvince().Tables[0];
        if (!IsPostBack)
        {
            this.DropDownList1.DataSource = dt.DefaultView;
            DropDownList1.DataValueField = "Pid";
            DropDownList1.DataTextField = "PidName";
            this.DropDownList1.DataBind();        

            DropDownList1.Attributes.Add("onchange", "send_request()");

        }
    }
}

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

CPU_2

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值