View Code
1 public partial class City : System.Web.UI.Page
2 {
3 protected void Page_Load(object sender, EventArgs e)
4 {
5 if(!IsPostBack)
6 {
7 BindProv();
8 BindCity();
9 }
10 }
11
12 //綁定市
13 private void BindCity()
14 {
15 ddlcity.Items.Clear();
16 DataTable citydt = new CitySer().GetAllCityByProvId(int.Parse(ddlprov.SelectedValue.ToString()));
17 foreach (DataRow dr in citydt.Rows)
18 {
19 ddlcity.Items.Add(new ListItem(dr["cityName"].ToString(), dr["id"].ToString()));
20 }
21 }
22 //綁定省
23 private void BindProv()
24 {
25 ddlprov.Items.Clear();
26 DataTable dt = new CitySer().GetAllProv();
27 foreach(DataRow dr in dt.Rows)
28 {
29 ddlprov.Items.Add(new ListItem(dr["provinceName"].ToString(),dr["id"].ToString()));
30 }
31 }
32 //聯動
33 protected void ddlprov_SelectedIndexChanged(object sender, EventArgs e)
34 {
35 if (ddlprov.Items.Count != 0)
36 {
37 ddlcity.Items.Clear();
38 DataTable citydt = new CitySer().GetAllCityByProvId(int.Parse(ddlprov.SelectedValue.ToString()));
39 foreach (DataRow dr in citydt.Rows)
40 {
41 ddlcity.Items.Add(new ListItem(dr["cityName"].ToString(), dr["id"].ToString()));
42 }
43 }
44 }
45 }