protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string str = "server=.;database=exam;Integrated security=true";
DataSet ds=new DataSet ();
using (SqlConnection sqlCnn = new SqlConnection(str))
{
using (SqlCommand sqlCmm = new SqlCommand())
{
sqlCmm.Connection = sqlCnn;
sqlCmm.CommandText = "select name,pid from province";
SqlDataAdapter da = new SqlDataAdapter(sqlCmm);
da.Fill(ds);
}
this.DropDownList1.DataSource = ds.Tables[0];
this.DropDownList1.DataTextField = "name";
this.DropDownList1.DataValueField = "pid";
this.DataBind();
this.DropDownList1.Items.Insert(0, "--请选择省份--");
this.DropDownList2.Items.Insert(0, "--请选择城市--");
}
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string str = "server=.;database=exam;Integrated security=true";
DataSet ds = new DataSet();
using (SqlConnection sqlcnn = new SqlConnection(str))
{
SqlCommand sqlcmm = sqlcnn.CreateCommand();
sqlcmm.CommandText ="select cname,pid from city where pid =" + DropDownList1.SelectedValue;
SqlDataAdapter sda = new SqlDataAdapter(sqlcmm);
sda.Fill(ds);
}
this.DropDownList2.DataSource = ds.Tables[0];
this.DropDownList2.DataTextField = "cname";
this.DropDownList2.DataValueField = "pid";
this.DataBind();
}
省级联动
最新推荐文章于 2021-12-25 15:32:00 发布