关于使用DropListDown实现二级联动的问题

      当要实现国家和省份,品牌和型号等二级联动时,使用两个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(); ;
}




 

posted @ 2011-11-06 14:58  janss  阅读(5146)  评论(4编辑  收藏  举报