如何实现省市级的联动!
代码分享!
protected void Button1_Click(object sender, EventArgs e)
{
//实现省市的联动 省市初始化
string str = @"data source=.\SQLEXPRESS;integrated security=sspi;initial catalog=area";
DataSet ds = new DataSet();
using (SqlConnection sqlcnn = new SqlConnection())
{
sqlcnn.ConnectionString = str;
SqlCommand sqlcmm = sqlcnn.CreateCommand();
sqlcmm.CommandText = "select ProID,Proname from Province";
SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
da.Fill(ds);
}
this.dropProcince.DataSource = ds.Tables[0];
this.dropProcince.DataTextField = "Proname";
this.dropProcince.DataValueField = "ProID";
this.dropProcince.DataBind();
//这个是给初始的省级相对应的 市级赋值
//using (SqlConnection sqlcnn = new SqlConnection())
//{
// sqlcnn.ConnectionString = str;
// SqlCommand sqlcmm = sqlcnn.CreateCommand();
// sqlcmm.CommandText = "select ProID,CityName from City where ProID='" + this.dropProcince.SelectedValue.ToString() + "'";
// SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
// da.Fill(ds);
//}
//this.dropcity.DataSource = ds.Tables[0];
//this.dropcity.DataTextField = "CityName";
//this.dropcity.DataBind();
}
protected void dropProcince_SelectedIndexChanged(object sender, EventArgs e)
{
//当省得selectindex改变时,同时赋值给市级的dropdownlist
string str = @"data source=.\SQLEXPRESS;integrated security=sspi;initial catalog=area";
DataSet ds = new DataSet();
using (SqlConnection sqlcnn = new SqlConnection())
{
sqlcnn.ConnectionString = str;
SqlCommand sqlcmm = sqlcnn.CreateCommand();
sqlcmm.CommandText = "select * from City where ProID='" + this.dropProcince.SelectedValue.ToString() + "'";
SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
da.Fill(ds);
}
this.dropcity.DataSource = ds.Tables[0];
this.dropcity.DataTextField = "CityName";
this.dropcity.DataValueField = "ProID";
this.dropcity.DataBind();
}