protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetProvince();
}
}
private void LoadData(string id,DropDownList ddl)
{
string strcon = @"Data Source=YHB-PC;Initial Catalog=News;Persist Security Info=True;User ID=sa;Password=yhb@163";
SqlConnection conn = new SqlConnection(strcon);
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "SELECT A_Id,A_Name FROM T_Arear WHERE A_ParentId=@parentid ORDER BY A_Id";
cmd.Parameters.AddWithValue("@parentid", id);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
adapter.Fill(dt);
cmd.Dispose();
conn.Dispose();
ddl.DataSource = dt;
if (id == "0")
{
ListItem item = new ListItem("---请?选?择?---", "0");
//this.ddlProvince.Items.Add(item);
ddl.Items.Insert(0, item);
}
}
private void GetProvince()
{
LoadData("0",this.ddlProvince);
}
protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
{
string id = this.ddlProvince.SelectedItem.Value;
if (id != "0")
{
LoadData(id, this.ddlCity);
string selectid = this.ddlCity.SelectedItem.Value;
LoadData(selectid, this.ddlArear);
}
else
{
ddlCity.Items.Clear();
ddlArear.Items.Clear();
}
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
string id = this.ddlCity.SelectedItem.Value;
LoadData(id, this.ddlArear);
}
*****************************************************下面是自己写的不够简化
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;
namespace 省市选择器
{
public partial class WebForm1 : System.Web.UI.Page
{
string str = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataDropprovince();
}
}
private void DataDropprovince()//省
{
DataTable dt = DataLoad("0");
Dropprovince.DataSource = dt;
Dropprovince.DataTextField = "A_Name";
Dropprovince.DataValueField = "A_Id";
Dropprovince.DataBind();
ListItem item = new ListItem("--请选择--","0");
Dropprovince.Items.Insert(0, item);
}
private DataTable DataLoad(string id)
{
SqlConnection con = new SqlConnection(str);
SqlCommand cmd = con.CreateCommand();
cmd.CommandText = "SELECT A_Id,A_Name FROM T_Arear WHERE A_ParentId=@parentid ORDER BY A_Id";
cmd.Parameters.AddWithValue("@parentid",id);
DataTable dt = new DataTable();
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
adapter.Fill(dt);
cmd.Dispose();
con.Dispose();
return dt;
}
protected void Dropprovince_SelectedIndexChanged(object sender, EventArgs e)
{
string id= this.Dropprovince.SelectedItem.Value;
if (id != "0")
{
DataTable dt = DataLoad(id);
Dropcity.DataSource = dt;
Dropcity.DataTextField = "A_Name";
Dropcity.DataValueField = "A_Id";
Dropcity.DataBind();
//绑定默认显示的市下面的区县
DataTable dt1 = DataLoad(this.Dropcity.SelectedItem.Value);
Droparea.DataSource = dt1;
Droparea.DataTextField = "A_Name";
Droparea.DataValueField = "A_Id";
Droparea.DataBind();
}
else
{
Dropcity.Items.Clear();
Droparea.Items.Clear();
}
}
protected void Dropcity_SelectedIndexChanged(object sender, EventArgs e)
{
DataTable dt = DataLoad(this.Dropcity.SelectedItem.Value);
Droparea.DataSource = dt;
Droparea.DataTextField = "A_Name";
Droparea.DataValueField = "A_Id";
Droparea.DataBind();
}
}
}