using System; using System.Collections; using System.ComponentModel; using System.Data; using System.Drawing; using System.Web; using System.Web.SessionState; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.HtmlControls; namespace PostalCode { /// /// 郵便番号検索 /// public class WebForm1 : System.Web.UI.Page { protected System.Web.UI.WebControls.Label Label1; protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1; protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1; protected System.Web.UI.WebControls.TextBox code; protected System.Web.UI.WebControls.Button searchBtn; protected System.Web.UI.WebControls.DropDownList candList; protected System.Web.UI.WebControls.DropDownList prefList; protected System.Web.UI.WebControls.Label address; protected System.Web.UI.WebControls.ListBox cityList; protected System.Web.UI.WebControls.ListBox townList; protected System.Web.UI.WebControls.RangeValidator RangeValidator1; private void ShowAddress() { address.Text = prefList.SelectedItem.Text; if (cityList.SelectedItem != null) address.Text += cityList.SelectedItem.Text; if (townList.SelectedItem != null) address.Text += townList.SelectedItem.Text; code.Enabled = true; candList.Visible = false; } private void SetList(int no) { PostData pd = PostData.Instance(Server); ArrayList a = pd.GetTownData(int.Parse(code.Text)); if (a == null) { address.Text = "無効な番号です"; return; } TownData tw = (TownData)a[no]; CityData ct = tw.Parent; PrefData pf = ct.Parent; prefList.SelectedIndex = pf.ID; prefList_SelectedIndexChanged(null,null); cityList.SelectedIndex = ct.ID; cityList_SelectedIndexChanged(null,null); townList.SelectedIndex = tw.ID; townList_SelectedIndexChanged(null,null); if (a.Count == 1) return; code.Enabled = false; candList.Visible = true; candList.Items.Clear(); candList.Items.Add("やり直す"); foreach (TownData t in a) candList.Items.Add(t.Parent.Value + t.Value); candList.SelectedIndex = no+1; } private void Page_Load(object sender, System.EventArgs e) { // ページを初期化するユーザー コードをここに挿入します。 if (IsPostBack) return; PostData pd = PostData.Instance(Server); for (int i=0;i<47;++i) prefList.Items.Add(pd[i].Value); prefList.SelectedIndex = 12; prefList_SelectedIndexChanged(null,null); } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: この呼び出しは、ASP.NET Web フォーム デザイナで必要です。 // InitializeComponent(); base.OnInit(e); } /// /// Designer サポートに必要なメソッドです。コード エディタで /// このメソッドのコンテンツを変更しないでください。 /// private void InitializeComponent() { this.code.TextChanged += new System.EventHandler(this.searchBtn_Click); this.searchBtn.Click += new System.EventHandler(this.searchBtn_Click); this.candList.SelectedIndexChanged += new System.EventHandler(this.candList_SelectedIndexChanged); this.prefList.SelectedIndexChanged += new System.EventHandler(this.prefList_SelectedIndexChanged); this.cityList.SelectedIndexChanged += new System.EventHandler(this.cityList_SelectedIndexChanged); this.townList.SelectedIndexChanged += new System.EventHandler(this.townList_SelectedIndexChanged); this.Load += new System.EventHandler(this.Page_Load); } #endregion private void prefList_SelectedIndexChanged(object sender, System.EventArgs e) { PostData pd = PostData.Instance(Server); PrefData pf = pd[prefList.SelectedIndex]; cityList.Items.Clear(); townList.Items.Clear(); foreach (CityData ct in pf.List) cityList.Items.Add(ct.Value); ShowAddress(); } private void cityList_SelectedIndexChanged(object sender, System.EventArgs e) { PostData pd = PostData.Instance(Server); PrefData pf = pd[prefList.SelectedIndex]; CityData ct = (CityData)pf.List[cityList.SelectedIndex]; townList.Items.Clear(); foreach (TownData tw in ct.List) townList.Items.Add(tw.Value); ShowAddress(); } private void townList_SelectedIndexChanged(object sender, System.EventArgs e) { PostData pd = PostData.Instance(Server); PrefData pf = pd[prefList.SelectedIndex]; CityData ct = (CityData)pf.List[cityList.SelectedIndex]; TownData tw = (TownData)ct.List[townList.SelectedIndex]; code.Text = tw.Code.ToString(); ShowAddress(); } private void searchBtn_Click(object sender, System.EventArgs e) { SetList(0); } private void candList_SelectedIndexChanged(object sender, System.EventArgs e) { if (candList.SelectedIndex == 0) { ShowAddress(); return; } SetList(candList.SelectedIndex-1); } } }