省市联动 DropDownList AutoPostBack
protected void Page_Load(object sender, EventArgs e)
{
SQLHelper sqH;
if (!Page.IsPostBack)
{
sqH = new SQLHelper();
string strSelPro = "SELECT [proName] FROM [db_city].[dbo].[tb_province] ";
DataTable dt = sqH.ExecuteQuery(strSelPro,CommandType.Text);
if (dt.Rows.Count > 0)
{
DropDownList1.DataSource = dt;
DropDownList1.DataValueField = dt.Columns[0].ColumnName;
DropDownList1.DataTextField = dt.Columns[0].ColumnName;
DropDownList1.DataBind();
}
//try
//{
// using (SqlConnection sqlCon = new SqlConnection("server=.;database=db_city;uid=sa;pwd="))
// {
// sqlCon.Open();
// string strSelPro = "SELECT [proName] FROM [db_city].[dbo].[tb_province] ";
// SqlCommand sqlCom = new SqlCommand(strSelPro, sqlCon);
// SqlDataReader sdr = sqlCom.ExecuteReader();
// while (sdr.Read())
// {
// DropDownList1.Items.Add(sdr[0].ToString());
// }
// sdr.Close();
// string strSelCity = "SELECT [proName],b.[cityName],a.[proID],b.[proID] FROM [db_city].[dbo].[tb_province] as a,[db_city].[dbo].[tb_city] as b ";
// strSelCity += "where a.[proID]=b.[proID] and [proName]='" + DropDownList1.SelectedItem.Text + "'";
// sqlCom = new SqlCommand(strSelCity, sqlCon);
// sdr = sqlCom.ExecuteReader();
// while (sdr.Read())
// {
// DropDownList2.Items.Add(sdr[1].ToString());
// }
// sdr.Close();
// }
//}
//catch (Exception ex)
//{
// throw ex;
//}
//finally
//{
//}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//DropDownList2.Items.Clear();
//try
//{
// using (SqlConnection sqlCon = new SqlConnection("server=.;database=db_city;uid=sa;pwd="))
// {
// sqlCon.Open();
// string strSelCity = "SELECT [proName],b.[cityName],a.[proID],b.[proID] FROM [db_city].[dbo].[tb_province] as a,[db_city].[dbo].[tb_city] as b ";
// strSelCity += "where a.[proID]=b.[proID] and [proName]='" + DropDownList1.SelectedItem.Text + "'";
// SqlCommand sqlCom = new SqlCommand(strSelCity, sqlCon);
// SqlDataReader sdr = sqlCom.ExecuteReader();
// while (sdr.Read())
// {
// DropDownList2.Items.Add(sdr[1].ToString());
// }
// sdr.Close();
// }
//}
//catch (Exception ex)
//{
// throw ex;
//}
//finally
//{
//}
}
{
SQLHelper sqH;
if (!Page.IsPostBack)
{
sqH = new SQLHelper();
string strSelPro = "SELECT [proName] FROM [db_city].[dbo].[tb_province] ";
DataTable dt = sqH.ExecuteQuery(strSelPro,CommandType.Text);
if (dt.Rows.Count > 0)
{
DropDownList1.DataSource = dt;
DropDownList1.DataValueField = dt.Columns[0].ColumnName;
DropDownList1.DataTextField = dt.Columns[0].ColumnName;
DropDownList1.DataBind();
}
//try
//{
// using (SqlConnection sqlCon = new SqlConnection("server=.;database=db_city;uid=sa;pwd="))
// {
// sqlCon.Open();
// string strSelPro = "SELECT [proName] FROM [db_city].[dbo].[tb_province] ";
// SqlCommand sqlCom = new SqlCommand(strSelPro, sqlCon);
// SqlDataReader sdr = sqlCom.ExecuteReader();
// while (sdr.Read())
// {
// DropDownList1.Items.Add(sdr[0].ToString());
// }
// sdr.Close();
// string strSelCity = "SELECT [proName],b.[cityName],a.[proID],b.[proID] FROM [db_city].[dbo].[tb_province] as a,[db_city].[dbo].[tb_city] as b ";
// strSelCity += "where a.[proID]=b.[proID] and [proName]='" + DropDownList1.SelectedItem.Text + "'";
// sqlCom = new SqlCommand(strSelCity, sqlCon);
// sdr = sqlCom.ExecuteReader();
// while (sdr.Read())
// {
// DropDownList2.Items.Add(sdr[1].ToString());
// }
// sdr.Close();
// }
//}
//catch (Exception ex)
//{
// throw ex;
//}
//finally
//{
//}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//DropDownList2.Items.Clear();
//try
//{
// using (SqlConnection sqlCon = new SqlConnection("server=.;database=db_city;uid=sa;pwd="))
// {
// sqlCon.Open();
// string strSelCity = "SELECT [proName],b.[cityName],a.[proID],b.[proID] FROM [db_city].[dbo].[tb_province] as a,[db_city].[dbo].[tb_city] as b ";
// strSelCity += "where a.[proID]=b.[proID] and [proName]='" + DropDownList1.SelectedItem.Text + "'";
// SqlCommand sqlCom = new SqlCommand(strSelCity, sqlCon);
// SqlDataReader sdr = sqlCom.ExecuteReader();
// while (sdr.Read())
// {
// DropDownList2.Items.Add(sdr[1].ToString());
// }
// sdr.Close();
// }
//}
//catch (Exception ex)
//{
// throw ex;
//}
//finally
//{
//}
}