天天看点

System.Web.UI.WebControls

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.UI;    http://www.gitom.com/973631
  6. using System.Web.UI.WebControls;  
  7. using System.Data.SqlClient;  
  8. using System.Data;  
  9. namespace 省市联动  
  10. {  
  11.     public partial class 省市联动 : System.Web.UI.Page  
  12.     {  
  13.         string strCon = "server=.;database=Place;uid=sa;pwd=123456";  
  14.         protected void Page_Load(object sender, EventArgs e)  
  15.         {  
  16.             //DropDownList1.Text = "北京市";  
  17.             if (!IsPostBack)  
  18.             {  
  19.                 SqlConnection sqlcon = new SqlConnection(strCon);  
  20.                 string sqlstr = "select * from Province";  
  21.                 string strCity = "select * from City where Province = '北京市'";  
  22.                 SqlDataAdapter myda = new SqlDataAdapter(sqlstr, strCon);  
  23.                 SqlDataAdapter mydaCity = new SqlDataAdapter(strCity, sqlcon);  
  24.                 DataSet myds = new DataSet();  
  25.                 DataSet mydsCity = new DataSet();  
  26.                 sqlcon.Open();  
  27.                 myda.Fill(myds);  
  28.                 mydaCity.Fill(mydsCity);  
  29.                 DropDownList1.DataSource = myds;  
  30.                 DropDownList1.DataValueField = "ProvinceName";  
  31.                 DropDownList1.DataBind();  
  32.                 DropDownList2.DataSource = mydsCity;  
  33.                 DropDownList2.DataValueField = "CityName";  
  34.                 DropDownList2.DataBind();  
  35.                 sqlcon.Close();  
  36.             }  
  37.         }  
  38.         protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)  
  39.         {  
  40.             SqlConnection sqlcon = new SqlConnection(strCon);  
  41.             string sqlstr = "select * from City where Province = '" + DropDownList1.SelectedValue.Trim() + "'";  
  42.             SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);  
  43.             DataSet myds = new DataSet();  
  44.             sqlcon.Open();  
  45.             myda.Fill(myds);  
  46.             DropDownList2.DataSource = myds;  
  47.             DropDownList2.DataValueField = "CityName";  
  48.             DropDownList2.DataBind();  
  49.             sqlcon.Close();  http://www.gitom.com/973631
  50.         }