2013-03-11

    public partial class WebForm1 : System.Web.UI.Page   

  {         string constr = ConfigurationManager.ConnectionStrings["sqlPro"].ConnectionString;  

       protected void Page_Load(object sender, EventArgs e)      

   {             if (!IsPostBack)    

         {                  proBind();         

    }               

     }        

//首先将省份信息绑定到dropdownlist1 中     

    private void proBind()       

  {             using (SqlConnection con=new SqlConnection(constr))     

        {                 using (SqlCommand cmd=con.CreateCommand())    

             {                     con.Open();             

        cmd.CommandText = " select provinceID,province from province";     

                using (SqlDataAdapter adapter=new SqlDataAdapter(cmd))     

                {                         DataTable dtPro = new DataTable();         

                adapter.Fill(dtPro);                   

      this.DropDownList1.DataSource = dtPro;       

                  this.DropDownList1.DataTextField = "province";    

                     this.DropDownList1.DataValueField = "provinceID";       

                  DropDownList1.DataBind();               

          DropDownList1.Items.Insert(0, "--请选择省份--");         

            }                       

              }    

         }      

   }

//然后将城市信息绑定到 dropdownlist2中  

       protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)  

       {             using (SqlConnection con = new SqlConnection(constr))     

        {                 using (SqlCommand cmd = con.CreateCommand())   

              {                     con.Open();      

               int proId = Convert.ToInt32(DropDownList1.SelectedValue);   

                  cmd.CommandText = " select cityID,city from city where father="+proId+"";       

              using (SqlDataAdapter adapter = new SqlDataAdapter

(cmd))              

       {                         DataTable dtCity = new DataTable();   

                      adapter.Fill(dtCity);             

            if (proId > 0)                 

        {                             this.DropDownList2.DataSource = dtCity;    

                         this.DropDownList2.DataTextField = "city";     

                        this.DropDownList2.DataValueField = "cityID";     

                        DropDownList2.DataBind();            

                 DropDownList2.Items.Insert(0,"-请选择城市-");           

              }                     

    else                   

      {                             DropDownList2.Items.Clear();         

                    DropDownList2.Items.Insert(0, new ListItem("--请选择城市--", "0"));      

                       DropDownList2.Items.Clear();              

               DropDownList2.Items.Insert(0, new ListItem("--请选择县区--", "0"));   

                                              }             

                               }

                }           

  }       

  }

        protected void DropDownList2_SelectedIndexChanged(object sender, EventArgs e)

        {             using (SqlConnection con = new SqlConnection(constr))    

         {                 using (SqlCommand cmd = con.CreateCommand())       

          {                     con.Open();          

           int cityId = Convert.ToInt32(DropDownList2.SelectedValue);         

            cmd.CommandText = " select areaID,area from area where father="+cityId+"";       

              using (SqlDataAdapter adapter = new SqlDataAdapter(cmd))        

             {                         DataTable dtCity = new DataTable();          

               adapter.Fill(dtCity);                   

      if (cityId > 0)             

            {                          

   this.DropDownList3.DataSource = dtCity;           

                  this.DropDownList3.DataTextField = "area";        

 

                     this.DropDownList3.DataValueField = "areaID";   

                          DropDownList3.DataBind();                  

           DropDownList3.Items.Insert(0, new ListItem("--请选择县区--", "0"));       

                  }                    

     else                 

        {                          

   DropDownList3.Items.Clear();     

                        DropDownList3.Items.Insert(0, new ListItem("--请选择县区--", "0"));          

               }          

           }

                }     

        }

        }

        protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)    

     {             string province = DropDownList1.SelectedItem.Text;        

     string  city = DropDownList2.SelectedItem.Text;       

      string area = DropDownList3.SelectedItem.Text;      

       Label1.Text = province + city + area;     

    }        

    }