省市的二级联动
/// <summary>
/// 省列表框
/// </summary>
private void DisplayProvince()
{
SqlDataReader sdr=db.RunProcGetReader("select * from Province");
ddlProvince.DataSource=sdr;
ddlProvince.DataTextField="ProName";
ddlProvince.DataValueField="ProID";
ddlProvince.DataBind();
sdr.Close();
}
/// <summary>
/// 市列表
/// </summary>
private void DisplayCity()
{
SqlDataAdapter sda=db.CreateDa("select * from City");
DataTable dt=new DataTable();
sda.Fill(dt);
//将市数据放入一个label中,此label设置属性隐藏即可
lblTemp.Text="";
foreach(DataRow row in dt.Rows)
{
lblTemp.Text=lblTemp.Text+row["proid"]+"-"+row["cityid"]+"-"+row["cityname"]+",";
}
sda.Dispose();
}
/// 省列表框
/// </summary>
private void DisplayProvince()
{
SqlDataReader sdr=db.RunProcGetReader("select * from Province");
ddlProvince.DataSource=sdr;
ddlProvince.DataTextField="ProName";
ddlProvince.DataValueField="ProID";
ddlProvince.DataBind();
sdr.Close();
}
/// <summary>
/// 市列表
/// </summary>
private void DisplayCity()
{
SqlDataAdapter sda=db.CreateDa("select * from City");
DataTable dt=new DataTable();
sda.Fill(dt);
//将市数据放入一个label中,此label设置属性隐藏即可
lblTemp.Text="";
foreach(DataRow row in dt.Rows)
{
lblTemp.Text=lblTemp.Text+row["proid"]+"-"+row["cityid"]+"-"+row["cityname"]+",";
}
sda.Dispose();
}
<script language="javascript">
//将隐藏的字符串取出来,然后分割到一个数组中去
var str = document.getElementById("lblTemp").innerText;
arr=str.split(",");
a=arr.length
ar=new Array()
for (i=0;i<a;i++){
ar[i]=arr[i].split("-");
}
onecount=ar.length;
//此处实现联动
function ld() {
document.Form1.ddlCity.length=0
lid=Form1.ddlProvince.value;
for (i=0;i<onecount;i++) {
if (ar[i][0] == lid) {
document.Form1.ddlCity.options.add(new Option(ar[i][2], ar[i][1]));
}
}
}
function showMsg()
{
alert(document.Form1.ddlProvince.value);
alert(document.Form1.ddlCity.value);
}
</script>
//将隐藏的字符串取出来,然后分割到一个数组中去
var str = document.getElementById("lblTemp").innerText;
arr=str.split(",");
a=arr.length
ar=new Array()
for (i=0;i<a;i++){
ar[i]=arr[i].split("-");
}
onecount=ar.length;
//此处实现联动
function ld() {
document.Form1.ddlCity.length=0
lid=Form1.ddlProvince.value;
for (i=0;i<onecount;i++) {
if (ar[i][0] == lid) {
document.Form1.ddlCity.options.add(new Option(ar[i][2], ar[i][1]));
}
}
}
function showMsg()
{
alert(document.Form1.ddlProvince.value);
alert(document.Form1.ddlCity.value);
}
</script>