1. 程式人生 > >地區選擇控制元件的展示和使用

地區選擇控制元件的展示和使用

從資料庫中讀取地區代號,翻譯成實際的地址

IEnumerable<string> values = RegionHelper.GetFullRegion(storeById.RegionId.Value, ",").Split(new char[] { ',' }).Take<string>(3);
public static string GetFullRegion(int currentRegionId, string separator)
        {
            XmlNode node = FindNode(currentRegionId);
            if (node == null)
            {
                return string.Empty;
            }
            //搜尋街道,然後迴圈搜尋,直至地區位置
            //街道,區+街道,省+區+街道
            string str = node.Attributes["name"].Value;
            for (XmlNode node2 = node.ParentNode; node2.Name != "region"; node2 = node2.ParentNode)
            {
                //區+街道,省+區+街道,地區+省+區+街道,
                str = node2.Attributes["name"].Value + separator + str;
            }
            return str;
        }
private static XmlNode FindNode(int id)
        {
            string str = id.ToString(CultureInfo.InvariantCulture);
            string xpath = string.Format("//street[@id='{0}']", str);
            //獲取儲存好地址資訊的目標檔案
            XmlDocument regionDocument = GetRegionDocument();
            //獲取第一個符合條件的值
            XmlNode node = regionDocument.SelectSingleNode(xpath);
            if (node != null)
            {
                return node;
            }
            xpath = string.Format("//county[@id='{0}']", str);
            node = regionDocument.SelectSingleNode(xpath);
            if (node != null)
            {
                return node;
            }
            xpath = string.Format("//city[@id='{0}']", str);
            node = regionDocument.SelectSingleNode(xpath);
            if (node != null)
            {
                return node;
            }
            xpath = string.Format("//province[@id='{0}']", str);
            node = regionDocument.SelectSingleNode(xpath);
            if (node != null)
            {
                return node;
            }
            return null;
        }
private static XmlDocument GetRegionDocument()
        {
            XmlDocument document = HiCache.Get<XmlDocument>("FileCache-Regions");
            if (document == null)
            {
                string filename = HttpContext.Current.Request.MapPath(Globals.ApplicationPath + "/config/region.config");
                document = new XmlDocument();
                document.Load(filename);
                HiCache.Insert("FileCache-Regions", document, 0x708);
            }
            return document;
        }
獲取控制元件中的值並存入欄位中
RegionId = this.dropRegion.GetSelectedRegionId().Value,
public int? GetSelectedRegionId()
        {
            if (!string.IsNullOrEmpty(this.Context.Request.Form[IDPrev + "regionSelectorValue"]))
            {
                return new int?(int.Parse(this.Context.Request.Form[IDPrev + "regionSelectorValue"]));
            }
            return null;
        }
獲取資料庫中的地區代號通過地區控制元件展示
this.dropRegion.SetSelectedRegionId(storeById.RegionId);
public void SetSelectedRegionId(int? selectedRegionId)
        {
            this.currentRegionId = selectedRegionId;
            this.dataLoaded = true;
        }
地區模板檔案形如
<?xml version="1.0" encoding="utf-8"?><root><region id="1" name="華東"><province id="100" name="浙江省"><city id="101" name="杭州市"><county id="102" name="上城區"><street id="10201" name="清波街道" />