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)