protected override void CreateChildControls() { this.Controls.Clear(); if (!this.dataLoaded) { if (!string.IsNullOrEmpty(this.Context.Request.Form["regionSelectorValue"])) { this.currentRegionId = new int?(int.Parse(this.Context.Request.Form["regionSelectorValue"])); } this.dataLoaded = true; } if (this.currentRegionId.HasValue) { XmlNode region = RegionHelper.GetRegion(this.currentRegionId.Value); if (region != null) { if (region.Name == "county") { this.cityId = new int?(int.Parse(region.ParentNode.Attributes["id"].Value)); this.provinceId = new int?(int.Parse(region.ParentNode.ParentNode.Attributes["id"].Value)); } else { if (region.Name == "city") { this.cityId = new int?(this.currentRegionId.Value); this.provinceId = new int?(int.Parse(region.ParentNode.Attributes["id"].Value)); } else { if (region.Name == "province") { this.provinceId = new int?(this.currentRegionId.Value); } } } } } this.Controls.Add(RegionSelectoNoCountys.CreateTitleControl(this.ProvinceTitle)); this.ddlProvinces = this.CreateDropDownList("ddlRegions1"); RegionSelectoNoCountys.FillDropDownList(this.ddlProvinces, RegionHelper.GetAllProvinces(), this.provinceId); this.Controls.Add(RegionSelectoNoCountys.CreateTag("<span>")); this.Controls.Add(this.ddlProvinces); this.Controls.Add(RegionSelectoNoCountys.CreateTag("</span>")); this.Controls.Add(RegionSelectoNoCountys.CreateTitleControl(this.CityTitle)); this.ddlCitys = this.CreateDropDownList("ddlRegions2"); if (this.provinceId.HasValue) { RegionSelectoNoCountys.FillDropDownList(this.ddlCitys, RegionHelper.GetCitys(this.provinceId.Value), this.cityId); } this.Controls.Add(RegionSelectoNoCountys.CreateTag("<span>")); this.Controls.Add(this.ddlCitys); this.Controls.Add(RegionSelectoNoCountys.CreateTag("</span>")); }
private static void FillDropDownList(WebControl ddlRegions, Dictionary <int, string> regions, int?selectedId) { foreach (int current in regions.Keys) { WebControl webControl = RegionSelectoNoCountys.CreateOption(current.ToString(CultureInfo.InvariantCulture), regions[current]); if (selectedId.HasValue && current == selectedId.Value) { webControl.Attributes.Add("selected", "true"); } ddlRegions.Controls.Add(webControl); } }