- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI; http://www.gitom.com/973631
- using System.Web.UI.WebControls;
- using System.Data.SqlClient;
- using System.Data;
- namespace 省市联动
- {
- public partial class 省市联动 : System.Web.UI.Page
- {
- string strCon = "server=.;database=Place;uid=sa;pwd=123456";
- protected void Page_Load(object sender, EventArgs e)
- {
- //DropDownList1.Text = "北京市";
- if (!IsPostBack)
- {
- SqlConnection sqlcon = new SqlConnection(strCon);
- string sqlstr = "select * from Province";
- string strCity = "select * from City where Province = '北京市'";
- SqlDataAdapter myda = new SqlDataAdapter(sqlstr, strCon);
- SqlDataAdapter mydaCity = new SqlDataAdapter(strCity, sqlcon);
- DataSet myds = new DataSet();
- DataSet mydsCity = new DataSet();
- sqlcon.Open();
- myda.Fill(myds);
- mydaCity.Fill(mydsCity);
- DropDownList1.DataSource = myds;
- DropDownList1.DataValueField = "ProvinceName";
- DropDownList1.DataBind();
- DropDownList2.DataSource = mydsCity;
- DropDownList2.DataValueField = "CityName";
- DropDownList2.DataBind();
- sqlcon.Close();
- }
- }
- protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
- {
- SqlConnection sqlcon = new SqlConnection(strCon);
- string sqlstr = "select * from City where Province = '" + DropDownList1.SelectedValue.Trim() + "'";
- SqlDataAdapter myda = new SqlDataAdapter(sqlstr, sqlcon);
- DataSet myds = new DataSet();
- sqlcon.Open();
- myda.Fill(myds);
- DropDownList2.DataSource = myds;
- DropDownList2.DataValueField = "CityName";
- DropDownList2.DataBind();
- sqlcon.Close(); http://www.gitom.com/973631
- }