SQL建表
HTML——HTMLPage1.htm
- <htmlxmlns="http://www.w3.org/1999/xhtml">
- head>
- title>Ajax实现省市县三级联动【无刷新】三层</styletype="text/css" select
- {
- width:130px;
- }
- stylescriptsrc="js/Jquery1.7.js"type="text/javascript">scriptscripttype="text/javascript" $(function(){
- $.ajax({
- type:"post",
- contentType:"application/json",
- url:"WebService1.asmx/GetProvince",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> data:"{}",248); line-height:18px"> success:function(result){
- varstroption='';
- for(vari=0;i<result.d.length;i++){
- stroption+='optionvalue='+result.d[i].provinceID+'>';
- stroption+=result.d[i].provincename;
- option $('#seprovince').append(stroption);
- })
-
- $('#seprovince').change(function(){
- $('#secityoption:gt(0)').remove();
- $('#seareaoption:gt(0)').remove();
-
- $.ajax({
- type:"post",248); line-height:18px"> contentType:"application/json",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> url:"WebService1.asmx/GetCItyByPro",248); line-height:18px"> data:"{proid:'"+$(this).val()+"'}",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> success:function(result){
- varstrocity='';
- for(vari=0;iresult.d.length;i++){
- strocity+='optionvalue='+result.d[i].cityID+'>';
- strocity+=result.d[i].cityname;
- }
- $('#secity').append(strocity);
- })
- $('#secity').change(function(){
- url:"WebService1.asmx/GetAreaByCity",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> data:"{cityid:'"+$(this).val()+"'}",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> varstroarea='';
- stroarea+='optionvalue='+result.d[i].areaID+' stroarea+=result.d[i].areaname;
- $('#searea').append(stroarea);
- bodytabletrtd 用户名
- inputid="Text1"type="text"/>
- 密码
- inputid="Text2"type="text" 确认密码
- inputid="Text3"type="text" 邮箱
- inputid="Text4"type="text" 地址
- selectid="seprovince">--请选择--select 省
- selectid="secity">市
- selectid="searea">县
- html>
创建Web服务——WebService1.asmx
copy
usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Web;
- usingSystem.Web.Services;
- usingSystem.Data;
- namespaceWebApplication1
- {
- ///summary ///WebService1的摘要说明
- [WebService(Namespace="http://tempuri.org/")]
- [WebServiceBinding(ConformsTo=WsiProfiles.BasicProfile1_1)]
- [System.ComponentModel.ToolboxItem(false)]
- //若要允许使用ASP.NETAJAX从脚本中调用此Web服务,请取消对下行的注释。
- [System.Web.Script.Services.ScriptService]
- publicclassWebService1:System.Web.Services.WebService
- [WebMethod]
- publicstringHelloWorld()
- return"HelloWorld";
- [WebMethod]
- publicListModel.province>GetProvince()
- BLL.provincebpro=newBLL.province();
- List>list=bpro.GetListModel();
- returnlist;
- publicListModel.city>GetCItyByPro(stringproid)
- BLL.citybcity=newBLL.city();
- List>list=bcity.GetListModel("father='"+proid+"'");
- returnlist;
- Model.area>GetAreaByCity(stringcityid)
- BLL.areabarea=newBLL.area();
- >list=barea.GetListModel("father='"+cityid+"'");
- }
====三层 ——> 创建类库 BLL 、DAL、DataAccess、MODEL——> 在类库里添加类
province表——动软生成类
GetListModel在BLL层定义
GetListModel转到定义——DAL层
city表——动软生成类
city在BLL层定义
copy
publicList>GetListModel(stringstrsql)
- returndal.GetListModel(strsql);
- }
city转到定义——DAL层
publicSystem.Collections.Generic.List System.Collections.Generic.List>list=newSystem.Collections.Generic.List>();
- DataTabledt=GetList(strsql).Tables[0];
- foreach(DataRowrowindt.Rows)
- Model.citymcity=newModel.city();
- mcity.id=Convert.ToInt32(row["id"]);
- mcity.cityID=row["cityID"].ToString();
- mcity.cityname=row["cityname"].ToString();
- list.Add(mcity);
- area表——动软生成类
area在BLL层定义
area转到定义——DAL层
copy
DataTabledt=GetList(strsql).Tables[0];
- System.Collections.Generic.List>();
- Model.areamarea=newModel.area()
- id=Convert.ToInt32(row["id"]),
- areaID=row["areaID"].ToString(),
- areaname=row["areaname"].ToString()
- };
- list.Add(marea);
- =========================================================三级联动—有刷新【两种实现方法】
第一种:
SQL建表
三层 ——> 创建类库 BLL 、DAL、DataAccess、MODEL——> 在类库里添加类
Web窗体——WebForm1.aspx
前台:
copy
head runat="server">三级联动—有刷新formid="form1"runat="server"div>地址asp:DropDownListID="ddlprovince"runat="server"AutoPostBack="True"
- onselectedindexchanged="ddlprovince_SelectedIndexChanged"asp:DropDownList>省
- asp:DropDownListID="ddlcity"runat="server"AutoPostBack="True"
- onselectedindexchanged="ddlcity_SelectedIndexChanged"asp:DropDownListID="ddlarear"runat="server">县
- form后台:——引用了动软生成的类库
copy
usingSystem.Web.UI;
- usingSystem.Web.UI.WebControls;
- usingSystem.Data;
- namespaceWebApplication1
- publicpartialclassWebForm1:System.Web.UI.Page
- protectedvoidPage_Load(objectsender,EventArgse)
- if(!IsPostBack)
- LoadProvince();
- Model.provincep=newModel.province();
- p.id=1;
- p.provinceID="222221";
- p.provincename="北京";
- Model.provincep1=newModel.province();
- p1.id=2;
- p1.provinceID="222222";
- p1.provincename="天津";
- privatevoidLoadProvince()
- BLL.provincebpro=newBLL.province();
- DataTabledt=bpro.GetList("").Tables[0];
- ddlprovince.DataSource=dt;
- ddlprovince.DataTextField="provincename";
- ddlprovince.DataValueField="provinceID";
- ddlprovince.DataBind();
- protectedvoidddlprovince_SelectedIndexChanged(objectsender,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> if(this.ddlarear.Items.Count>0)
- this.ddlarear.Items.Clear();
- BLL.citybcity=newBLL.city();
- stringproid=this.ddlprovince.SelectedItem.Value;
- DataTabledt=bcity.GetList("father='"+proid+"'").Tables[0];
- ddlcity.DataSource=dt;
- ddlcity.DataTextField="cityname";
- ddlcity.DataValueField="cityID";
- ddlcity.DataBind();
- //绑定默认显示的市级区划下面所有的县
- stringcityid=dt.Rows[0]["cityID"].ToString();
- BLL.areabaraer=newBLL.area();
- DataTabledtarea=baraer.GetList("father='"+cityid+"'").Tables[0];
- ddlarear.DataSource=dtarea;
- ddlarear.DataTextField="areaname";
- ddlarear.DataValueField="areaID";
- ddlarear.DataBind();
- protectedvoidddlcity_SelectedIndexChanged(objectsender,EventArgse)
- BLL.areabaraer=newBLL.area();
- stringcityid=this.ddlcity.SelectedItem.Value;
- DataTabledt=baraer.GetList("father='"+cityid+"'").Tables[0];
- ddlarear.DataSource=dt;
- }
Web窗体——WebForm2.aspx
前台:
asp:DropDownListID="ddlarear"runat="server"
- 后台:
copy
publicpartialclassWebForm2:System.Web.UI.Page
- protectedvoidPage_Load(objectsender,248); line-height:18px"> if(!IsPostBack)
- LoadProvince();
- BLL.provincebprovince=newBLL.province();
- >list=bprovince.GetListModel();
- this.ddlprovince.DataSource=list;
- this.ddlprovince.DataTextField="provincename";
- this.ddlprovince.DataValueField="provinceID";
- this.ddlprovince.DataBind();
- protectedvoidddlprovince_SelectedIndexChanged(objectsender,248); line-height:18px"> stringid=this.ddlprovince.SelectedItem.Value;
- >list=bcity.GetListModel("father="
- +id);
- this.ddlcity.DataSource=list;
- this.ddlcity.DataTextField="cityname";
- this.ddlcity.DataValueField="cityID";
- this.ddlcity.DataBind();
- protectedvoidddlcity_SelectedIndexChanged(objectsender,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px"> stringid=this.ddlcity.SelectedItem.Value;
- BLL.areabarear=newBLL.area();
- >list=barear.GetListModel("father="
- +id);
- this.ddlarear.DataSource=list;
- this.ddlarear.DataTextField="areaname";
- this.ddlarear.DataValueField="areaID";
- this.ddlarear.DataBind();
- }
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|