將地址切開成縣市、鄉鎮市區與其他地址_ASP.NET

這篇主要是記錄一下自己所遇到的一個問題,現在有一串地址,要將他切開為縣市鄉鎮市區其他地址分別塞到控制項中,以下說明我的作法。

這裡先給個畫面讓大家看到最後的結果,將一串地址拆開並將台東縣、太麻里鄉對應到兩個 DropDownList,剩餘地址對應到 TextBox 中。(這裡並沒有做縣市和鄉鎮市區的連動功能)

image

以下先說明大致的作法,然後再最後給出程式碼

  1. 通常在寫網站系統的時候,會將縣市、鄉鎮市區等等資料存到資料庫中,但是我們這裡只是個簡單範例,所以我直接再程式碼中產出這些資料並將他們塞進兩個 List<> 中,來假裝是資料庫的資料來源。
  2. 我觀察到縣市名稱都只有三個字,所以取得縣市名稱就只使用 Substring() 來取得。
  3. 當取得縣市名稱之後就可以在 List<> 中找到所對應的縣市代碼,用這個縣市代碼就可以取得所屬鄉鎮市區。
  4. 然後用所得到的鄉鎮市區來對應地址,找到地址字串的鄉鎮市區及其代碼。
  5. 最後就可以用得到的縣市代碼、鄉鎮市區代碼來對應兩個 DropDownList,剩下來的地址對應到 TextBox 裡,結束。

程式碼:

    <form id="form1" runat="server">
    <div>
        這是待宰的地址:<asp:Label ID="lbAddress" runat="server" Text="台東縣太麻里鄉中山路1號"></asp:Label><br />
        <asp:Button ID="btnCut"
            runat="server" Text="我切!" onclick="btnCut_Click" /><br />
        <asp:DropDownList ID="ddlCity" runat="server">
        </asp:DropDownList>
        <asp:DropDownList ID="ddlCountry" runat="server">
        </asp:DropDownList>
        <asp:TextBox ID="txtOtherAddress" runat="server"></asp:TextBox>
        <br />
    </div>
    </form>
        private List<City> cityList;
        private List<Country> countryList;

        protected void Page_Load(object sender, EventArgs e)
        {
            createData(); // 產生縣市、鄉鎮市區資料

            if (!IsPostBack)
            {
                // 將兩個DropDownList的資料繫結起來
                bindCityList();
                bindCountryList();
            }
        }

        /// <summary>
        /// 繫結 縣市 ddlCity
        /// </summary>
        private void bindCityList()
        {
            var city = from c in cityList
                       select c;

            ddlCity.DataSource = city;
            ddlCity.DataTextField = "cityName";
            ddlCity.DataValueField = "cityID";
            ddlCity.DataBind();
        }

        /// <summary>
        /// 繫結 鄉鎮市區 ddlCountry
        /// </summary>
        private void bindCountryList()
        {
            var country = from c in countryList
                          select c;

            ddlCountry.DataSource = country;
            ddlCountry.DataTextField = "countryName";
            ddlCountry.DataValueField = "countryID";
            ddlCountry.DataBind();
        }


        // 下面的資料通常會存在資料庫中,不過在這個範例我們把資料丟進List<>裡面
        #region 資料
        
        public class City
        {
            public string cityID { get; set; }
            public string cityName { get; set; }
        }

        public class Country
        {
            public string cityID { get; set; }
            public string countryID { get; set; }
            public string countryName { get; set; }
        }

        private void createData()
        {
            // 全台所有縣市
            cityList = new List<City>
            {
                new City { cityID="1",	cityName="台北市" },
                new City { cityID="2",	cityName="基隆市" },
                new City { cityID="3",	cityName="新北市" },
                new City { cityID="4",	cityName="連江縣" },
                new City { cityID="5",	cityName="宜蘭縣" },
                new City { cityID="6",	cityName="新竹市" },
                new City { cityID="7",	cityName="新竹縣" },
                new City { cityID="8",	cityName="桃園縣" },
                new City { cityID="9",	cityName="苗栗縣" },
                new City { cityID="10",	cityName="台中市" },
                new City { cityID="11",	cityName="彰化縣" },
                new City { cityID="12",	cityName="南投縣" },
                new City { cityID="13",	cityName="嘉義縣" },
                new City { cityID="14",	cityName="雲林縣" },
                new City { cityID="15",	cityName="台南市" },
                new City { cityID="16",	cityName="高雄市" },
                new City { cityID="17",	cityName="澎湖縣" },
                new City { cityID="18",	cityName="金門縣" },
                new City { cityID="19",	cityName="屏東縣" },
                new City { cityID="20",	cityName="台東縣" },
                new City { cityID="21",	cityName="花蓮縣" }
            };

            // 因為是範例,所以只取台東縣的鄉鎮市區
            countryList = new List<Country>
            {
                new Country { cityID = "20", countryID="1", countryName="臺東市"  },
		        new Country { cityID = "20", countryID="2", countryName="成功鎮"  },
		        new Country { cityID = "20", countryID="3", countryName="關山鎮"  },
		        new Country { cityID = "20", countryID="4", countryName="卑南鄉"  },
		        new Country { cityID = "20", countryID="5", countryName="大武鄉"  },
		        new Country { cityID = "20", countryID="6", countryName="東河鄉"  },
		        new Country { cityID = "20", countryID="7", countryName="長濱鄉"  },
		        new Country { cityID = "20", countryID="8", countryName="鹿野鄉"  },
		        new Country { cityID = "20", countryID="9", countryName="池上鄉"  },
		        new Country { cityID = "20", countryID="10",countryName="綠島鄉"  },
		        new Country { cityID = "20", countryID="11",countryName="延平鄉"  },
		        new Country { cityID = "20", countryID="12",countryName="海端鄉"  },
		        new Country { cityID = "20", countryID="13",countryName="達仁鄉"  },
		        new Country { cityID = "20", countryID="14",countryName="金峰鄉"  },
		        new Country { cityID = "20", countryID="15",countryName="蘭嶼鄉"  },
		        new Country { cityID = "20", countryID="16",countryName="太麻里鄉"}
            };
        }

        #endregion

        /// <summary>
        /// 切切切按鈕,按下去就會幫你把地址切開囉!
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnCut_Click(object sender, EventArgs e)
        {
            string address = lbAddress.Text; // 原本的地址
            string cityName = string.Empty; // 存縣市名稱
            string countryName = string.Empty; // 存鄉鎮市區
            string otherAddress = string.Empty; // 存剩下的地址(路名、號等等地址)

            // 因為縣市都是三個字,所以這裡直接切三個字出來
            cityName = address.Substring(0, 3);

            // 把原來的地址減去縣市名稱
            // 現在的地址剩下 鄉鎮市區+其他地址
            address = address.Replace(cityName, "");

            // 取得縣市的ID,為了繫結 DropDownList 用
            var city = from c in cityList
                       where c.cityName == cityName
                       select c;         
            string cityID = city.First().cityID;

            // 用縣市找他的所屬鄉鎮市區
            var country = from c in countryList
                          where c.cityID == cityID
                          select c;

            // 找出符合的鄉鎮市區名稱和鄉鎮市區ID
            string countryID = string.Empty;
            foreach (var item in country)
            {
                if (address.Contains(item.countryName))
                {
                    countryName = item.countryName;
                    countryID = item.countryID;
                }
            }

            // 把地址減掉鄉鎮市區名稱 剩下其他地址
            address = address.Replace(countryName, "");
            otherAddress = address;

            // 把得到的三部分地址塞回控制項秀出來
            ddlCity.SelectedValue = cityID;
            ddlCountry.SelectedValue = countryID;
            txtOtherAddress.Text = otherAddress;
        }

以上是我對於將地址切分為縣市、鄉鎮市區及其他地址的作法,如果有更好的作法,也請各位前輩多多指教。

posted @ 2012-06-07 17:05  yuanlin  阅读(763)  评论(0编辑  收藏  举报