连动

来源:互联网 发布:123d design mac 编辑:程序博客网 时间:2024/05/16 01:32
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.onload=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()");

        }
    }
}

 

 

原创粉丝点击