45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:三级联动下拉菜单的方法

三级联动下拉菜单的方法

2016-08-31 11:00:43 来源:www.45fan.com 【

三级联动下拉菜单的方法

写个CS,
AJAX也可以using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// <summary>
/// provcityservstat 的摘要说明
/// 这个类主要是用来当省/城市/服务站都是下拉进判断,省和城市教师都是autopostback
/// </summary>
public class provcityservstat
{
public DropDownList prov;
public DropDownList city;
public DropDownList servstat;
public string servstat_id;
public provcityservstat()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//绑定省的下拉
public void dataBindToProv()
{
SqlConnection con = db.createSqlConneciton();
con.Open();
SqlCommand cmd = new SqlCommand("select * from t_prov", con);
SqlDataReader sdr = cmd.ExecuteReader();
this.prov.DataSource = sdr;
this.prov.DataTextField = "prov_name";
this.prov.DataValueField = "prov_id";
this.prov.DataBind();
sdr.Close();
con.Close();
}

//绑定城市下拉
public void dataBindToCity()
{
SqlConnection con = db.createSqlConneciton();
con.Open();
SqlCommand cmd = new SqlCommand("select * from t_city where city_provid = " + this.prov.SelectedValue, con);
SqlDataReader sdr = cmd.ExecuteReader();
this.city.DataSource = sdr;
this.city.DataTextField = "city_name";
this.city.DataValueField = "city_id";
this.city.DataBind();
sdr.Close();
con.Close();
}

//绑定服务站
public void dataBindToServstat()
{
if (this.city.SelectedValue != "")
{
SqlConnection con = db.createSqlConneciton();
con.Open();
SqlCommand cmd = new SqlCommand("select * from t_servstat where servstat_cityid = " + this.city.SelectedValue, con);
SqlDataReader sdr = cmd.ExecuteReader();
this.servstat.DataSource = sdr;
this.servstat.DataTextField = "servstat_name";
this.servstat.DataValueField = "servstat_id";
this.servstat.DataBind();
sdr.Close();
con.Close();
}
else
{
this.servstat.Items.Clear();
}
}

//一般用于新增时省,城市,服务站的下拉
public void dataBindAdd()
{
dataBindToProv();
dataBindToCity();
dataBindToServstat();
}

//当省的下拉发生变化时
public void databinddropdownprov()
{
dataBindToCity();
dataBindToServstat();
}

//下面开始是用于修改的,根据已知的servstat_id得出是哪个服务站,哪个城市,哪个省
public void dataBindMod()
{
SqlConnection con = db.createSqlConneciton();
con.Open();
SqlCommand cmd = new SqlCommand("select * from t_servstat,t_city,t_prov where prov_id = city_provid and city_id = servstat_cityid and servstat_id = " + servstat_id, con);
SqlDataReader sdr = cmd.ExecuteReader();
if (sdr.Read())
{
dataBindModProv(sdr["prov_id"].ToString());
dataBindModCity(sdr["prov_id"].ToString(), sdr["city_id"].ToString());
dataBindModServstat(sdr["city_id"].ToString(), sdr["servstat_id"].ToString());
}
sdr.Close();
con.Close();
}

//绑定到修改时的省
public void dataBindModProv(string prov_id)
{
SqlConnection con = db.createSqlConneciton();
con.Open();
SqlCommand cmd = new SqlCommand("select * from t_prov", con);
SqlDataReader sdr = cmd.ExecuteReader();
this.prov.Items.Clear();
int i = 0;
while (sdr.Read())
{
this.prov.Items.Insert(i, new ListItem(sdr["prov_name"].ToString(), sdr["prov_id"].ToString()));
if (prov_id == sdr["prov_id"].ToString())
{
this.prov.Items[i].Selected = true;
}
i++;
}
sdr.Close();
con.Close();
}

//绑定到修改时的城市
public void dataBindModCity(string prov_id, string city_id)
{
SqlConnection con = db.createSqlConneciton();
con.Open();
SqlCommand cmd = new SqlCommand("select * from t_city where city_provid = " + prov_id, con);
SqlDataReader sdr = cmd.ExecuteReader();
this.city.Items.Clear();
int i = 0;
while (sdr.Read())
{
this.city.Items.Insert(i, new ListItem(sdr["city_name"].ToString(), sdr["city_id"].ToString()));
if (city_id == sdr["city_id"].ToString())
{
this.city.Items[i].Selected = true;
}
i++;
}
sdr.Close();
con.Close();
}

//绑定到修改时的服务站
public void dataBindModServstat(string city_id, string servstat_id)
{
SqlConnection con = db.createSqlConneciton();
con.Open();
SqlCommand cmd = new SqlCommand("select * from t_servstat where servstat_cityid = " + city_id, con);
SqlDataReader sdr = cmd.ExecuteReader();
this.servstat.Items.Clear();
int i = 0;
while (sdr.Read())
{
this.servstat.Items.Insert(i, new ListItem(sdr["servstat_name"].ToString(), sdr["servstat_id"].ToString()));
if (servstat_id == sdr["servstat_id"].ToString())
{
this.servstat.Items[i].Selected = true;
}
i++;
}
sdr.Close();
con.Close();
}
}

参考一下我的,我这个是三级的
注意,下拉的autopostback要先上

 

本文地址:http://www.45fan.com/a/question/70245.html
Tags: 下拉菜单 三级 联动
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部