跪求:二级分类联动代码,,请各位来看一下啊,感谢! 我想用DropDownList实现二级联动菜单:点击第一个大类的某一项如湖北,然后第二个小类就显示湖北的地级市,这样如何实现啊,知道的给coding一下源代码啊,谢谢各位! 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 加我MSN给你[email protected] http://www.cnblogs.com/singlepine/archive/2005/10/19/257954.html老是拿 小山 老师的blog接分,都有点不好意思了。。 下面代码是我以前写的,你看看我想对你会有帮助的.看着自己改改吧 #region BinderToNationOrArea()绑定国家或地区,省份或洲,城市 /// <summary> /// 绑定国家或地区 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BinderToNationOrArea() { //打开数据库的连接 if (cn.State == System.Data.ConnectionState.Closed) cn.Open(); //国家或地区 SqlCommand cmd = new SqlCommand("select * from NationArea where NationArea = '中国' ",cn); SqlDataReader sdr = cmd.ExecuteReader(); this.ddlNationOrArea.DataSource = sdr; this.ddlNationOrArea.DataTextField = "NationArea"; this.ddlNationOrArea.DataValueField = "NationAreaID"; this.ddlNationOrArea.DataBind(); sdr.Close(); //省份或洲 //得到国家的ID string NationAreaID = this.ddlNationOrArea.SelectedValue; cmd.CommandText = "select * from Province where NationAreaID = '" + NationAreaID + "'"; sdr = cmd.ExecuteReader(); this.ddlProvince.DataSource = sdr; this.ddlProvince.DataTextField = "ProvinceName"; this.ddlProvince.DataValueField = "ProvinceID"; this.ddlProvince.DataBind(); sdr.Close(); //城市 //得到省的ID string ProvinceID = this.ddlProvince.SelectedValue; cmd.CommandText = "select * from City where ProvinceID = '" + ProvinceID + "'"; sdr = cmd.ExecuteReader(); this.ddlCity.DataSource = sdr; this.ddlCity.DataTextField = "CityName"; this.ddlCity.DataValueField = "CityID"; this.ddlCity.DataBind(); sdr.Close(); //关闭数据库的连接 cn.Close(); //本个控件的值 //国家 this.txtNationOrArea.Text = this.ddlNationOrArea.SelectedItem.Text.Trim(); //省 this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim(); //市 this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim(); //显示邮政编码 this.PostCode(); } #endregion #region BinderToProvince()绑定各个省,或洲 /// <summary> /// 绑定各个省,或洲 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BinderToProvince() { if (cn.State == System.Data.ConnectionState.Closed) cn.Open(); //省份或洲 //得到国家的ID string NationAreaID = this.ddlNationOrArea.SelectedValue; SqlDataAdapter sda = new SqlDataAdapter("select * from Province where NationAreaID = '" + NationAreaID + "'",cn); DataSet ds = new DataSet(); sda.Fill(ds,"Province"); this.ddlProvince.DataSource = ds.Tables["Province"]; this.ddlProvince.DataTextField = "ProvinceName"; this.ddlProvince.DataValueField = "ProvinceID"; this.ddlProvince.DataBind(); //城市 //得到省的ID string ProvinceID = this.ddlProvince.SelectedValue; sda.Fill(ds,"City"); this.ddlCity.DataSource =ds.Tables["City"]; this.ddlCity.DataTextField = "CityName"; this.ddlCity.DataValueField = "CityID"; this.ddlCity.DataBind(); //关闭数据库的连接 cn.Close(); //省 this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim(); //城市 this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim(); //显示相应邮政编码 this.PostCode(); } #endregion #region BinderToCity() 绑定各个城市 /// <summary> /// 绑定各个城市 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BinderToCity() { if (cn.State == System.Data.ConnectionState.Closed) cn.Open(); //城市 //得到省的ID string ProvinceID = this.ddlProvince.SelectedValue; SqlDataAdapter sda = new SqlDataAdapter("select * from City where ProvinceID = '" + ProvinceID + "'",cn); DataSet ds = new DataSet(); sda.Fill(ds,"City"); this.ddlCity.DataSource = ds.Tables["City"]; this.ddlCity.DataTextField = "CityName"; this.ddlCity.DataValueField = "CityID"; this.ddlCity.DataBind(); //关闭数据库的连接 cn.Close(); //省 this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim(); //市 this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim(); //显示相应的邮政编码 this.PostCode(); } #endregion 现从数据库中找出省的名称,绑定到dropdownlist1中,然后把dropdownlist1的AutoPostBacks属性设置为true;然后在dropdownlist1中的SelectedIndexChanged方法中查找数据库中选定省份的所有城市绑定到dropdownlist2中!这是最简单的方法。 cantonstar你的思路就是我的思路我已经给两个控件赋值了,但不知道如何选择内容后,第二如何如何选择我的代码:第一个:DataTable dt=this.ExecuteReader(sql); if(dt!=null && dt.Rows.Count>0) { this.TrParentID.DataSource=dt; this.TrParentID.DataTextField="TrType"; this.TrParentID.DataValueField="Trid"; this.TrParentID.DataBind(); }第二个trid=TrParentID.SelectedItem.Value; string sqlC="SELECT * FROM m_Tr_Type WHERE TrParentID ="+trid; DataTable dtC=this.ExecuteReader(sqlC); if(dtC!=null && dtC.Rows.Count>0) { this.TrChildID.DataSource=dtC; this.TrChildID.DataTextField="TrType"; this.TrChildID.DataValueField="TrChildID"; this.TrChildID.DataBind(); }知道的能否告诉我在程序了怎么布局这两个程序呢 private void Page_Load(object sender, System.EventArgs e) { if(!Page.IsPostBack) { string sqlstr = "select * from class where Sort='作为一级栏目'"; conn.Open(); SqlCommand cmd =new SqlCommand(sqlstr,conn); SqlDataReader dr =cmd.ExecuteReader(); this.Sort.DataSource = dr; this.Sort.DataValueField = "className"; this.Sort.DataBind(); dr.Close(); conn.Close(); smallSort.Items.Add(new ListItem("请选择大类")); } }public void Sort_SelectedIndexChanged(object sender, System.EventArgs e) { sqlstr="select * from class where Sort='"+this.Sort.SelectedValue.Trim()+"'"; conn.Open(); SqlCommand cmd =new SqlCommand(sqlstr,conn); SqlDataReader dr =cmd.ExecuteReader(); this.smallSort.DataSource = dr; this.smallSort.DataValueField = "className"; this.smallSort.DataBind(); dr.Close(); conn.Close(); }这两个方法就行了 要先把DropDownList的AutoPostBack属性设为True,在SelectedIndexChanged()方法里先得到选择的大类的ID或名称,然后写查询再次绑顶小类的DropDownList。 有沒有不刷新聯動的dropdownlist的代碼阿?網上都是數字或者英文的,但是對於如果關鍵字是中文就不行了 DropDownList的二级连动应该没什么的吧,只是回发一下,填的资料不会丢的额! asp.net菜菜问之,文件传输问题。 写过控件的请进 用jmail如何获取未读邮件呢? 好怪的问题,写代码时空格和回车不能用了? 大家说说用vs2005发布网站有什么好处? 在线等~~~~~~~~请教一句关闭代码 大家帮我看看这段代码有什么问题!谢谢 AjaxPro postback时候的问题 高手指点,关于asp.net调用com组件的问题,在线. 下载了WEB CONTROLS包,也安装了,想用TREEVIEW控件,引用里却找不着??? 继续问问,用CheckBox批量修改数据,小弟刚学,大哥大姐指导一下!分少别嫌弃 基础问题Application["xxx"]~~~
给你
[email protected]
看着自己改改吧
#region BinderToNationOrArea()绑定国家或地区,省份或洲,城市 /// <summary>
/// 绑定国家或地区
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToNationOrArea()
{
//打开数据库的连接
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//国家或地区
SqlCommand cmd = new SqlCommand("select * from NationArea where NationArea = '中国' ",cn);
SqlDataReader sdr = cmd.ExecuteReader();
this.ddlNationOrArea.DataSource = sdr;
this.ddlNationOrArea.DataTextField = "NationArea";
this.ddlNationOrArea.DataValueField = "NationAreaID";
this.ddlNationOrArea.DataBind();
sdr.Close(); //省份或洲
//得到国家的ID
string NationAreaID = this.ddlNationOrArea.SelectedValue; cmd.CommandText = "select * from Province where NationAreaID = '" + NationAreaID + "'";
sdr = cmd.ExecuteReader();
this.ddlProvince.DataSource = sdr;
this.ddlProvince.DataTextField = "ProvinceName";
this.ddlProvince.DataValueField = "ProvinceID";
this.ddlProvince.DataBind();
sdr.Close(); //城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
cmd.CommandText = "select * from City where ProvinceID = '" + ProvinceID + "'";
sdr = cmd.ExecuteReader();
this.ddlCity.DataSource = sdr;
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
sdr.Close();
//关闭数据库的连接
cn.Close(); //本个控件的值
//国家
this.txtNationOrArea.Text = this.ddlNationOrArea.SelectedItem.Text.Trim();
//省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim(); //显示邮政编码
this.PostCode();
}
#endregion
#region BinderToProvince()绑定各个省,或洲
/// <summary>
/// 绑定各个省,或洲
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToProvince()
{
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//省份或洲
//得到国家的ID
string NationAreaID = this.ddlNationOrArea.SelectedValue; SqlDataAdapter sda = new SqlDataAdapter("select * from Province where NationAreaID = '" + NationAreaID + "'",cn);
DataSet ds = new DataSet();
sda.Fill(ds,"Province");
this.ddlProvince.DataSource = ds.Tables["Province"];
this.ddlProvince.DataTextField = "ProvinceName";
this.ddlProvince.DataValueField = "ProvinceID";
this.ddlProvince.DataBind(); //城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
sda.Fill(ds,"City");
this.ddlCity.DataSource =ds.Tables["City"];
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
//关闭数据库的连接
cn.Close(); //省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//城市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim(); //显示相应邮政编码
this.PostCode();
}
#endregion
#region BinderToCity() 绑定各个城市
/// <summary>
/// 绑定各个城市
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void BinderToCity()
{
if (cn.State == System.Data.ConnectionState.Closed)
cn.Open();
//城市
//得到省的ID
string ProvinceID = this.ddlProvince.SelectedValue;
SqlDataAdapter sda = new SqlDataAdapter("select * from City where ProvinceID = '" + ProvinceID + "'",cn);
DataSet ds = new DataSet();
sda.Fill(ds,"City");
this.ddlCity.DataSource = ds.Tables["City"];
this.ddlCity.DataTextField = "CityName";
this.ddlCity.DataValueField = "CityID";
this.ddlCity.DataBind();
//关闭数据库的连接
cn.Close();
//省
this.txtProvince.Text = this.ddlProvince.SelectedItem.Text.Trim();
//市
this.txtCity.Text = this.ddlCity.SelectedItem.Text.Trim(); //显示相应的邮政编码
this.PostCode();
}
#endregion
我已经给两个控件赋值了,但不知道如何选择内容后,第二如何如何选择
我的代码:
第一个:
DataTable dt=this.ExecuteReader(sql);
if(dt!=null && dt.Rows.Count>0)
{
this.TrParentID.DataSource=dt;
this.TrParentID.DataTextField="TrType";
this.TrParentID.DataValueField="Trid";
this.TrParentID.DataBind();
}
第二个
trid=TrParentID.SelectedItem.Value;
string sqlC="SELECT * FROM m_Tr_Type WHERE TrParentID ="+trid;
DataTable dtC=this.ExecuteReader(sqlC);
if(dtC!=null && dtC.Rows.Count>0)
{
this.TrChildID.DataSource=dtC;
this.TrChildID.DataTextField="TrType";
this.TrChildID.DataValueField="TrChildID";
this.TrChildID.DataBind();
}
知道的能否告诉我在程序了怎么布局这两个程序呢
{
if(!Page.IsPostBack)
{
string sqlstr = "select * from class where Sort='作为一级栏目'";
conn.Open();
SqlCommand cmd =new SqlCommand(sqlstr,conn);
SqlDataReader dr =cmd.ExecuteReader();
this.Sort.DataSource = dr;
this.Sort.DataValueField = "className";
this.Sort.DataBind();
dr.Close();
conn.Close();
smallSort.Items.Add(new ListItem("请选择大类"));
}
}
public void Sort_SelectedIndexChanged(object sender, System.EventArgs e)
{
sqlstr="select * from class where Sort='"+this.Sort.SelectedValue.Trim()+"'";
conn.Open();
SqlCommand cmd =new SqlCommand(sqlstr,conn);
SqlDataReader dr =cmd.ExecuteReader();
this.smallSort.DataSource = dr;
this.smallSort.DataValueField = "className";
this.smallSort.DataBind();
dr.Close();
conn.Close();
}这两个方法就行了
網上都是數字或者英文的,但是對於如果關鍵字是中文就不行了