DropDownList的二级联动 (SqlDataSource实现和SQL 绑定实现)

1.sqldatasource 控件实现

二级联动,事实就是在第一个下拉列表选择了某项之后,第二个下拉列表根据这个选项来显示下一级的内容……从数据库方面说,就是先把第一个列表的内容显示出来,然后返回一个选择的值,再根据这个值去查第二个列表的内容……

两个dropdownlist都是数据库中的内容(代码基本不用写)

1.dropdownlist1 绑定 sqldatasource1 (sqldatasource1 数据源设置)

dropdownlist1  AutoPostBack属性改为 true

2.dropdownlist2 绑定 sqldatasource2(sqldatasource1 数据源设置   where子句 内容 如下图说是设置)

基本上可以了(如果dropdownlist2默认为空,设置了AppendDataBoundItems属性为true 还是会出现问题???)

2.sql绑定实现

当要实现国家和省份,品牌和型号等二级联动时,使用两个DropListDown可以方便实现。例如实现生产行业中一级类别和二级联动

数据库表为

aspx前台代码:

复制代码
 1 <asp:DropDownList ID="industry_fisrt_class" runat="server" DataSourceID="DS_Instrual_type"
2 DataTextField="option_text" DataValueField="option_value" AutoPostBack="True">
3 </asp:DropDownList>
4 <asp:SqlDataSource ID="DS_Instrual_type" runat="server" ConnectionString="<%$ ConnectionStrings:WEB_DBConnectionString %>"
5 SelectCommand="SELECT [option_text], [option_value] FROM [tb_SYS_SelectOption] WHERE ([group_name] = @group_name)">
6 <SelectParameters>
7 <asp:Parameter DefaultValue="行业类别" Name="group_name" Type="String" />
8 </SelectParameters>
9 </asp:SqlDataSource>
10<asp:DropDownList ID="industry_second_class" runat="server" DataSourceID="DS_Instrual_type2"
11 DataTextField="option_text" DataValueField="option_value">
12</asp:DropDownList>
13 <asp:SqlDataSource ID="DS_Instrual_type2" runat="server" ConnectionString="<%$ ConnectionStrings:WEB_DBConnectionString %>"
14 SelectCommand="SELECT [option_text], [option_value] FROM [tb_SYS_SelectOption] WHERE ([group_name] = @group_name)">
15 <SelectParameters>
16 <asp:ControlParameter ControlID="industry_fisrt_class" Name="group_name" PropertyName="SelectedValue" Type="String" />
17 </SelectParameters>
18</asp:SqlDataSource>
复制代码

最终效果

至此,二级联动就实现了,但是不够完美,例如在该记录要编辑时,用该方法实现的二级联动,第二个droplistdown初始化无效(industry_second_class.Text = "^从数据读的值^";),总是默认为第一个值

为了解决此问题,可以采用后台绑定数据源。

ASPX前台

1 <asp:DropDownList ID="industry_fisrt_class" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
2 </asp:DropDownList>
3 <asp:DropDownList ID="industry_second_class" runat="server">
4 </asp:DropDownList>

ASPX后台

复制代码
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{

BLL.tb_SYS_Dept bBLL = new BLL.tb_SYS_Dept();
Model.tb_SYS_Dept mModel = new Model.tb_SYS_Dept();
industry_fisrt_class.Text = mModel.industry_fisrt_class;
industry_second_class.Text = mModel.industry_second_class;
//执行绑定函数
BindDrop();
}//if(!IsPostBack) END
}



private void BindDrop()
{
//将数据捆绑到下拉列表中
//一级类别绑定
string sqlStr = "select * from tb_SYS_SelectOption where group_name = '行业类别'";
DataTable dt = DAL.DBOperator.GetDataSetBySqlString(sqlStr).Tables[0];
industry_fisrt_class.DataTextField = "option_text"; //设置列表显示的字
industry_fisrt_class.DataValueField = "option_value"; //设置列表提交后获得的字段,自己理解为隐藏绑定数据
industry_fisrt_class.DataSource = dt.DefaultView;
industry_fisrt_class.DataBind();
//industry_fisrt_class.Items.Insert(0, new ListItem("请选择大类", ""));//第一项中加入内容,重点是绑定后添加
//industry_second_class.Items.Insert(0, new ListItem("请选择小类", ""));//第一项中加入内容,重点是绑定后添加

//二级类别绑定
string type = industry_fisrt_class.SelectedValue;//页面加载后industry_fisrt_class.DataValueField隐藏绑定的数据,后边根据它查询industry_second_class要显现的数据
string sqlStr1 = "select * from tb_SYS_SelectOption where group_name ='" + type + "'";
DataTable dt1 = DAL.DBOperator.GetDataSetBySqlString(sqlStr1).Tables[0];
industry_second_class.DataTextField = "option_text"; //设置industry_fisrt_class事件SelectedIndexChanged改变后industry_second_class列表显示的数据
industry_second_class.DataSource = dt1.DefaultView;
industry_second_class.DataBind();
}



protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
string type = industry_fisrt_class.SelectedValue;//页面加载后industry_fisrt_class.DataValueField隐藏绑定的数据,后边根据它查询industry_second_class要显现的数据
string sqlStr = "select * from tb_SYS_SelectOption where group_name ='" + type + "'";

DataTable dt = DAL.DBOperator.GetDataSetBySqlString(sqlStr).Tables[0];
industry_second_class.DataTextField = "option_text"; //设置industry_fisrt_class事件SelectedIndexChanged改变后industry_second_class列表显示的数据
industry_second_class.DataSource = dt.DefaultView;
industry_second_class.DataBind(); ;
}
复制代码



(ps: 转至 : http://www.cnblogs.com/janss/archive/2011/11/06/2238112.html)

原文地址:https://www.cnblogs.com/LifeKingcn/p/2615533.html