asp.net 国家,省,城市三级联动
下面代码当中,国家我设置的只是中国,当然也可以从数据里取出别的国家来.
BinderToNationOrArea()绑定国家或地区,省份或洲,城市#region BinderToNationOrArea()绑定国家或地区,省份或洲,城市
/**//// <summary>
/// 绑定国家或地区
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToNationOrArea()
...{
//打开数据库的连接
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//国家或地区
SqlCommand cmd = new SqlCommand("select * from NationArea where NationArea = '中国' ",cn);
SqlDataReader sdr = cmd.ExecuteReader();
this.ddlNationOrArea.DataSource = sdr;
this.ddlNationOrArea.DataTextField = "NationArea";
this.ddlNationOrArea.DataValueField = "NationAreaID";
this.ddlNationOrArea.DataBind();
sdr.Close();
//省份或洲
//得到国家的ID
string NationAreaID = this.ddlNationOrArea.SelectedValue;
cmd.CommandText = "select * from Province where NationAreaID = '" + NationAreaID + "'";
sdr = cmd.ExecuteReader();
this.ddlProvince.DataSource = sdr;
this.ddlProvince.DataTextField = "ProvinceName";
this.ddlProvince.DataValueField = "ProvinceID";
this.ddlProvince.DataBind();
sdr.Close();
//城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
cmd.CommandText = "select * from City where ProvinceID = '" + ProvinceID + "'";
sdr = cmd.ExecuteReader();
this.ddlCity.DataSource = sdr;
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
sdr.Close();
//关闭数据库的连接
cn.Close();
//本个控件的值
//国家
this.txtNationOrArea.Text = this.ddlNationOrArea.SelectedItem.Text.Trim();
//省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim();
//显示邮政编码
this.PostCode();
}
#endregion
BinderToProvince()绑定各个省,或洲#region BinderToProvince()绑定各个省,或洲
/**//// <summary>
/// 绑定各个省,或洲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToProvince()
...{
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//省份或洲
//得到国家的ID
string NationAreaID = this.ddlNationOrArea.SelectedValue;
SqlDataAdapter sda = new SqlDataAdapter("select * from Province where NationAreaID = '" + NationAreaID + "'",cn);
DataSet ds = new DataSet();
sda.Fill(ds,"Province");
this.ddlProvince.DataSource = ds.Tables["Province"];
this.ddlProvince.DataTextField = "ProvinceName";
this.ddlProvince.DataValueField = "ProvinceID";
this.ddlProvince.DataBind();
//城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
sda.Fill(ds,"City");
this.ddlCity.DataSource =ds.Tables["City"];
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
//关闭数据库的连接
cn.Close();
//省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//城市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim();
//显示相应邮政编码
this.PostCode();
}
#endregion
BinderToCity() 绑定各个城市#region BinderToCity() 绑定各个城市
/**//// <summary>
/// 绑定各个城市
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToCity()
...{
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
SqlDataAdapter sda = new SqlDataAdapter("select * from City where ProvinceID = '" + ProvinceID + "'",cn);
DataSet ds = new DataSet();
sda.Fill(ds,"City");
this.ddlCity.DataSource = ds.Tables["City"];
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
//关闭数据库的连接
cn.Close();
//省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim();
//显示相应的邮政编码
this.PostCode();
}
#endregion
BinderToNationOrArea()绑定国家或地区,省份或洲,城市#region BinderToNationOrArea()绑定国家或地区,省份或洲,城市
/**//// <summary>
/// 绑定国家或地区
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToNationOrArea()
...{
//打开数据库的连接
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//国家或地区
SqlCommand cmd = new SqlCommand("select * from NationArea where NationArea = '中国' ",cn);
SqlDataReader sdr = cmd.ExecuteReader();
this.ddlNationOrArea.DataSource = sdr;
this.ddlNationOrArea.DataTextField = "NationArea";
this.ddlNationOrArea.DataValueField = "NationAreaID";
this.ddlNationOrArea.DataBind();
sdr.Close();
//省份或洲
//得到国家的ID
string NationAreaID = this.ddlNationOrArea.SelectedValue;
cmd.CommandText = "select * from Province where NationAreaID = '" + NationAreaID + "'";
sdr = cmd.ExecuteReader();
this.ddlProvince.DataSource = sdr;
this.ddlProvince.DataTextField = "ProvinceName";
this.ddlProvince.DataValueField = "ProvinceID";
this.ddlProvince.DataBind();
sdr.Close();
//城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
cmd.CommandText = "select * from City where ProvinceID = '" + ProvinceID + "'";
sdr = cmd.ExecuteReader();
this.ddlCity.DataSource = sdr;
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
sdr.Close();
//关闭数据库的连接
cn.Close();
//本个控件的值
//国家
this.txtNationOrArea.Text = this.ddlNationOrArea.SelectedItem.Text.Trim();
//省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim();
//显示邮政编码
this.PostCode();
}
#endregion
BinderToProvince()绑定各个省,或洲#region BinderToProvince()绑定各个省,或洲
/**//// <summary>
/// 绑定各个省,或洲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToProvince()
...{
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//省份或洲
//得到国家的ID
string NationAreaID = this.ddlNationOrArea.SelectedValue;
SqlDataAdapter sda = new SqlDataAdapter("select * from Province where NationAreaID = '" + NationAreaID + "'",cn);
DataSet ds = new DataSet();
sda.Fill(ds,"Province");
this.ddlProvince.DataSource = ds.Tables["Province"];
this.ddlProvince.DataTextField = "ProvinceName";
this.ddlProvince.DataValueField = "ProvinceID";
this.ddlProvince.DataBind();
//城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
sda.Fill(ds,"City");
this.ddlCity.DataSource =ds.Tables["City"];
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
//关闭数据库的连接
cn.Close();
//省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//城市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim();
//显示相应邮政编码
this.PostCode();
}
#endregion
BinderToCity() 绑定各个城市#region BinderToCity() 绑定各个城市
/**//// <summary>
/// 绑定各个城市
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToCity()
...{
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
SqlDataAdapter sda = new SqlDataAdapter("select * from City where ProvinceID = '" + ProvinceID + "'",cn);
DataSet ds = new DataSet();
sda.Fill(ds,"City");
this.ddlCity.DataSource = ds.Tables["City"];
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
//关闭数据库的连接
cn.Close();
//省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim();
//显示相应的邮政编码
this.PostCode();
}
#endregion