老生常谈,就不用再多介绍省市区三级联动的用处了,直接上效果图:
前台代码:
<asp:DropDownList ID="seachprov" runat="server" CssClass="span4" AutoPostBack="true" OnSelectedIndexChanged="seachprov_SelectedIndexChanged"></asp:DropDownList> <asp:DropDownList ID="seachcity" runat="server" CssClass="span4" AutoPostBack="true" OnSelectedIndexChanged="seachcity_SelectedIndexChanged"> <asp:ListItem Value="0">请选择</asp:ListItem></asp:DropDownList> <asp:DropDownList ID="seachdistrict" runat="server" CssClass="span4"> <asp:ListItem Value="0">请选择</asp:ListItem></asp:DropDownList>
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindCityArea("0", seachprov);//初始化代码绑定省份 if (!string.IsNullOrEmpty(getCookies["ClientTel"])) { this.BindShowUpInfo(getCookies["ClientTel"]); } } } //编辑是如何在绑定省市区显示 public void BindShowUpInfo(string strUpid) { if (!string.IsNullOrEmpty(strUpid)) { using (EastAgricultureDataContext db = new EastAgricultureDataContext()) { UserInfo getNot = db.UserInfo.Single(x => x.tel ==strUpid); var area = getNot.areacity.Split(' '); if (area.Length > 0) { seachprov.Items.FindByText(area[0]).Selected = true; //var getCity = (from p in db.areas where p.parentid == db.areas.Single(x => x.areaname == area[1]).parentid select new { p.id, p.areaname }).ToList(); //seachcity.DataTextField = "areaname"; //seachcity.DataValueField = "id"; //seachcity.DataSource = getCity; //seachcity.DataBind(); seachcity.Items.Insert(0, new ListItem("请选择")); BindCityArea(db.areas.Single(x => x.areaname == area[1]).parentid, seachcity); seachcity.Items.FindByText(area[1]).Selected = true; //var getDis = (from p in db.areas where p.parentid==db.areas.Single(x => x.areaname == area[1]).id select new { p.id, p.areaname }).ToList(); //seachdistrict.DataTextField = "areaname"; //seachdistrict.DataValueField = "id"; //seachdistrict.DataSource = getDis; //seachdistrict.DataBind(); seachdistrict.Items.Insert(0, new ListItem("请选择")); BindCityArea(db.areas.Single(x => x.areaname == area[1]).id, seachdistrict); seachdistrict.Items.FindByText(area[2]).Selected = true; } } } } protected void seachprov_SelectedIndexChanged(object sender, EventArgs e) { string pid = seachprov.SelectedItem.Value.Trim(); if (pid != "0") { //初始化 市和县级列表 //初始化市列表 seachcity.Items.Clear(); /*seachdistrict.Items.Clear(); seachdistrict.Items.Insert(0, new ListItem("请选择"));*/ using (EastAgricultureDataContext db = new EastAgricultureDataContext()) { var getCity = (from p in db.areas where p.parentid == pid select new { p.id, p.areaname }).ToList(); if (getCity.Count == 1) { seachcity.DataTextField = "areaname"; seachcity.DataValueField = "id"; seachcity.DataSource = getCity; seachcity.DataBind(); seachcity.Items.Insert(0, new ListItem("请选择")); //var getDis = (from p in db.areas where p.parentid == getCity[0].id select new { p.id, p.areaname }).ToList(); //seachdistrict.DataTextField = "areaname"; //seachdistrict.DataValueField = "id"; //seachdistrict.DataSource = getDis; //seachdistrict.DataBind(); seachdistrict.Items.Insert(0, new ListItem("请选择")); BindCityArea(getCity[0].id, seachdistrict); } else { seachcity.DataTextField = "areaname"; seachcity.DataValueField = "id"; seachcity.DataSource = getCity; seachcity.DataBind(); seachcity.Items.Insert(0, new ListItem("请选择")); } } } } protected void seachcity_SelectedIndexChanged(object sender, EventArgs e) { string pid = seachcity.SelectedItem.Value.Trim(); if (pid != "0") { BindCityArea(pid, seachdistrict); } } /// <summary> /// 共通引用部分 绑定省市区 ///初始化 市和县级列表 ///初始化市列表 /// </summary> /// <param name="codeid"></param> /// <param name="ddlArea"></param> public void BindCityArea(string codeid, DropDownList ddlArea) { ddlArea.Items.Clear(); using (EastAgricultureDataContext db = new EastAgricultureDataContext()) { var getDis = (from p in db.areas where p.parentid == codeid select new { p.id, p.areaname }).ToList(); ddlArea.DataTextField = "areaname"; ddlArea.DataValueField = "id"; ddlArea.DataSource = getDis; ddlArea.DataBind(); ddlArea.Items.Insert(0, new ListItem("请选择")); } }
-- ---------------------------- -- Table structure for areas -- ---------------------------- DROP TABLE [dbo].[areas] GO CREATE TABLE [dbo].[areas] ( [id] nvarchar(10) NULL , [areaname] nvarchar(100) NULL , [parentid] nvarchar(10) NULL , [shortname] nvarchar(50) NULL , [lng] nvarchar(30) NULL , [lat] nvarchar(30) NULL , [level] int NULL , [position] nvarchar(100) NULL , [sort] int NULL ) GO 数据过大另附下载地址: http://download.csdn.net/detail/kongwei521/9427179