C# 实现 省市区三级联动(表中包含经纬度取自淘宝地址管理)

老生常谈,就不用再多介绍省市区三级联动的用处了,直接上效果图:


前台代码:

 <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>

后台C#代码:

    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("请选择"));
			}
		}

SQL表结构及数据:

 

-- ----------------------------
-- 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 


评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值