三级联动

来源:互联网 发布:centos安装pptp 编辑:程序博客网 时间:2024/06/10 01:20
 <div class="reg_s1 blank10"><span class="rs">*</span>所在省份:</div>    <div class="reg_s2 blank10">        <asp:DropDownList ID="ddlProvince" runat="server" Width="200" AutoPostBack="false" onchange="changePro(this.value)" >            <%--<asp:ListItem Value="0" Text="请选择省"></asp:ListItem>  --%>                    </asp:DropDownList>    </div>    <div class="clearer"></div>    <!------------------------------------------------------------------------>     <div class="reg_s1 blank10"><span class="rs">*</span>所在城市:</div>    <div class="reg_s2 blank10">        <%--<asp:DropDownList ID="ddlCity" runat="server" Width="200"              AutoPostBack="false" Enabled="false" onchange="changeCity(this.value)">            <%--<asp:ListItem Value="0" Text="请选择市"></asp:ListItem>            </asp:DropDownList>--%>        <select id="ddlCity" runat="server" style="width:200px;" onchange="changeCity(this.value)"></select>    </div>    <div class="clearer"></div>    <!------------------------------------------------------------------------>     <div class="reg_s1 blank10"><span class="rs">*</span>所在县或市:</div>    <div class="reg_s2 blank10">        <%--<asp:DropDownList ID="ddlCounty" runat="server" Enabled="false" Width="200" >          <%--<asp:ListItem Value="0" Text="请选择县"></asp:ListItem>        </asp:DropDownList>--%>         <select id="ddlCounty" runat="server" style="width:200px;"></select>    </div>    <div class="clearer"></div>    <!------------------------------------------------------------------------>

 

2.JavaScript

    

    <script type="text/javascript">        function changePro(provinceid) {            if (provinceid != '0') {                document.getElementById("ddlCity").disabled = false;                document.getElementById("ddlCounty").disabled = true;                var url = "procity.ashx?type=province&id=" + provinceid;                var xmlhttp = GetXmlHttpRequest();                xmlHttp.open("POST", url, true);                xmlHttp.send(null);                xmlHttp.onreadystatechange = BindCity;                                      }        }//end changePro        function BindCity(){            if (xmlHttp.readyState == 4) {                if (xmlHttp.status == 200) {                    //document.getElementById("ddlCity") =xmlHttp.responseText;                    //document.getElementById("txtClinicIntro").value = xmlHttp.responseText;                    var ddlCity = document.getElementById("ddlCity");                    var str = xmlHttp.responseText;                    var items = str.split("|");                    ddlCity.options.length = 0;                    ddlCity.add(new Option("请选择市","0"))                    for (var i = 0; i < items.length; i++){                        var item = items[i].split(",");                        //document.getElementById("txtClinicIntro").value = item;                        ddlCity.add(new Option(item[1],item[0]));                                        }                                }                                                }        }//end BindCity        function changeCity(cityid) {            if (cityid != '0') {                document.getElementById("ddlCounty").disabled = false;                var url = "procity.ashx?type=city&id=" + cityid;                xmlHttp.open("POST", url, true);                xmlHttp.send(null);                xmlHttp.onreadystatechange = BindCounty;                                    }                  }        function BindCounty() {            if (xmlHttp.readyState == 4) {                if (xmlHttp.status == 200) {                    var ddlCounty = document.getElementById("ddlCounty");                    //document.getElementById("txtClinicIntro").value = xmlHttp.responseText;                    var str = xmlHttp.responseText;                    var items = str.split("|");                    ddlCounty.options.length = 0;                    ddlCounty.add(new Option("请选择县","0"))                    for (var i = 0; i < items.length; i++) {                        var item = items[i].split(",");                        ddlCounty.add(new Option(item[1],item[0]));                                                                                      }                }                                  }               }    </script>


3. XMLHTTPRequest

 

var xmlHttp = null/*获取xmlHttpRequest对象*/function GetXmlHttpRequest() {    if (window.ActiveXObject) {        try {            xmlHttp = new ActiveXObject("Microsoft.XMLHTTP"); //IE新版本        } catch (e) {            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");      //IE老版本        }    } else {        xmlHttp = new XMLHttpRequest();    }    return xmlHttp;}


4.DALMsSql   数据访问层

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using System.Data.Common;using SMJKW.COMMON;namespace SMJKW.DALSQLSERVER{       public class CommonData    {       private DataTable _dt = null;       private DbHelper tempdb = new DbHelper();          //获得省的表       public DataTable GetProvince() {           string Query = "select Province_ID,Province_Name from Province_Info";           DbCommand cmd = tempdb.GetSqlStringCommond(Query);           _dt=tempdb.ExecuteDataTable(cmd);           cmd.Dispose();           if (_dt == null)           {               return null;           }           else {               return _dt;                      }                                            }       //获得市的表       public DataTable GetCity(string provinceid) {                    string Query="select City_ID,City_name from City where Province_ID='"+provinceid+"'";            DbCommand cmd = tempdb.GetSqlStringCommond(Query);            _dt = tempdb.ExecuteDataTable(cmd);            if (_dt == null)            {                return null;            }            else {                return _dt;                        }              }       //获得县的表       public DataTable GetCounty(string cityid) {           string Query = "select Country_ID,Country_Name from Country where City_ID='"+cityid+"'";           DbCommand cmd = tempdb.GetSqlStringCommond(Query);           _dt = tempdb.ExecuteDataTable(cmd);           if (_dt == null)           {               return null;           }           else {               return _dt;                      }                  }   }}


5.BLL

 

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data;using SMJKW.DALSQLSERVER;namespace SMJKW.BLL{   public class Common_Bll    {        private CommonData comdata = new CommonData();//声明一个对象,以后都通过这个对象调用       /*构造方法*/       public Common_Bll() {                      }       //获取省       public DataTable GetProvince() {          return comdata.GetProvince();             }       //获得市       public DataTable GetCity(string provinceid) {           return comdata.GetCity(provinceid);       }       //获得县       public DataTable GetCounty(string cityid) {           return comdata.GetCounty(cityid);              }    }}


6. 页面的.cs文件 (省的数据是数据绑定来的)

 

 public partial class ClinicReg : System.Web.UI.Page    {           private string mesg;        protected void Page_Load(object sender, EventArgs e)        {            if (!Page.IsPostBack)            {                                ddlBindPro();            }        }        //绑定省        public void ddlBindPro()        {            Common_Bll com = new Common_Bll();            DataTable dt = null;            dt=com.GetProvince();            if (dt.Equals(null))            {                this.ddlProvince.Items.Add(new ListItem("请选择省", "0"));            }            else            {                this.ddlProvince.Items.Add(new ListItem("请选择省", "0"));                this.ddlProvince.DataSource = dt;                this.ddlProvince.DataTextField = "Province_Name";                this.ddlProvince.DataValueField = "Province_ID";                this.ddlProvince.DataBind();                           }                  }


7.一般处理文件,将数据表组装成字符,发送到请求
 

using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Data;using SMJKW.BLL;namespace SMJK.WEB.Clinic{    /// <summary>    /// procity 的摘要说明    /// </summary>    public class procity : IHttpHandler    {        public void ProcessRequest(HttpContext context)        {            context.Response.ContentType = "text/plain";            //context.Response.Write("Hello World");            //context.Response.Write(DateTime.Now.ToShortDateString());            string type = context.Request.QueryString["type"];            if (type == "province") {                string id = context.Request.QueryString["id"];                context.Response.ContentType = "text/plain";                context.Response.Write(getCityByID(id));                             }            else if (type == "city") {                                  string id=context.Request.QueryString["id"];                context.Response.ContentType = "text/plain";                context.Response.Write(getCountyByID(id));                                     }        }        public string getCityByID(string id) {            string str = "";            Common_Bll com = new Common_Bll();            DataTable dt=com.GetCity(id);            if (dt.Equals(null))            {            }            else                        {                for (int i = 0; i < dt.Rows.Count; i++)                {                    if (i == dt.Rows.Count - 1)                    {                        //最后一条不用"|"隔断了                        str += dt.Rows[i]["City_ID"].ToString() + "," + dt.Rows[i]["City_name"].ToString();                    }                    else                    {                        str += dt.Rows[i]["City_ID"].ToString() + "," + dt.Rows[i]["City_name"].ToString() + "|";                    }                }                                   }                            return str;                }        public string getCountyByID(string id) {            string str = "";            Common_Bll com = new Common_Bll();            DataTable dt = com.GetCounty(id);            if (dt == null)            {            }            else                         {                for (int i = 0; i < dt.Rows.Count; i++) {                    if (i == dt.Rows.Count - 1)                    {                        str += dt.Rows[i]["Country_ID"].ToString() + "," + dt.Rows[i]["Country_Name"].ToString();                    }                    else {                        str += dt.Rows[i]["Country_ID"].ToString() + "," + dt.Rows[i]["Country_Name"].ToString() + "|";                                        }                                }                                                                }            return str;        }        public bool IsReusable        {            get            {                return false;            }        }    }}

 

 

 

原创粉丝点击