/
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//绑定省
SqlConnection con = DB.createConnection();
con.Open();
SqlCommand cmd = new SqlCommand("select * from province", con);
SqlDataReader dr = cmd.ExecuteReader();
this.DropDownList1.DataSource = dr;
this.DropDownList1.DataTextField = "proName";
this.DropDownList1.DataValueField = "proID";
this.DataBind();
dr.Close();
//绑定市
SqlCommand cmdCity = new SqlCommand();
cmdCity.Connection = con;
cmdCity.CommandText = "select cityID,cityName from city where proID=" + this.DropDownList1.SelectedValue;
dr = cmdCity.ExecuteReader();
this.DropDownList2.DataSource = dr;
this.DropDownList2.DataTextField = "cityName";
this.DropDownList2.DataValueField = "cityID";
this.DropDownList2.DataBind();
dr.Close();
con.Close();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//dropdownlist这个属性,每选中一个Items值就会执行一次
//使得省市可以动态连接
SqlConnection con = DB.createConnection();
con.Open();
string proID = this.DropDownList1.SelectedValue;
SqlCommand cmdcity = new SqlCommand("select cityID,cityName from city where proID="+proID,con);
SqlDataReader dr = cmdcity.ExecuteReader();
this.DropDownList2.DataSource = dr;
this.DropDownList2.DataTextField = "cityName";
this.DropDownList2.DataValueField = "cityID";
this.DropDownList2.DataBind();
dr.Close();