三級聯動 首先附圖一張,初步認識一下什麼是三級聯動: 註:選第一個後面兩個變,選第二個,最後一個改變。 其次,做三級聯動需要註意的方面:①DropDownList中的一個屬性——AutoPostBack:是否發生自動回傳到伺服器的操作。如果把該屬性設置為 TRUE,則啟用自動回傳,否則為 FALSE ...
三級聯動
首先附圖一張,初步認識一下什麼是三級聯動:
註:選第一個後面兩個變,選第二個,最後一個改變。
其次,做三級聯動需要註意的方面:①DropDownList中的一個屬性——AutoPostBack:是否發生自動回傳到伺服器的操作。如果把該屬性設置為 TRUE,則啟用自動回傳,否則為 FALSE。預設是 FALSE,在此需要true。
②綁定數據:封裝一個抽象的方法,靈活運用
例:web實現省市區三級聯動(方法有兩種,個人推薦第一種;第二種,代碼量多一些)
註:在此鏈接資料庫的類就不做呈現,但提示一點:資料庫根據父級代號條件寫查詢 返回list<>集合。
法一:
web中創建三個下拉列表框(DropDownList)
<form id="form1" runat="server"> <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"></asp:DropDownList> <asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="True"></asp:DropDownList> <asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="True"></asp:DropDownList><br /> </form>
CS:※SelectedIndexChanged事件:當列表控制項的選定項在信息發往伺服器之間變化時發生
public partial class Default3 : System.Web.UI.Page { ChinaDA da = new ChinaDA(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { Bind(DropDownList1 ,new ChinaDA().Select( "0001"));//填充省 Bind(DropDownList2, new ChinaDA().Select(DropDownList1.SelectedValue));//填充市 Bind(DropDownList3, new ChinaDA().Select(DropDownList2.SelectedValue));//填充區 } DropDownList1.SelectedIndexChanged += DropDownList1_SelectedIndexChanged;//省改變事件 DropDownList2.SelectedIndexChanged += DropDownList2_SelectedIndexChanged;//市改變事件 } public void DropDownList2_SelectedIndexChanged(object sender, EventArgs e) { Bind(DropDownList3, new ChinaDA().Select(DropDownList2.SelectedValue)); } public void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { Bind(DropDownList2, new ChinaDA().Select(DropDownList1.SelectedValue)); Bind(DropDownList3, new ChinaDA().Select(DropDownList2.SelectedValue)); } private void Bind(DropDownList dd, List<China> list) { dd.DataSource = list; dd.DataTextField = "Name"; dd.DataValueField = "Code"; dd.DataBind(); } }
法二:
創建三個下拉列表框(DropDownList)
省:<asp:DropDownList ID="DropDownListsheng" runat="server" OnSelectedIndexChanged="DropDownListsheng_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList> 市:<asp:DropDownList ID="DropDownListshi" runat="server" OnSelectedIndexChanged="DropDownListshi_SelectedIndexChanged" AutoPostBack="True"></asp:DropDownList> 區:<asp:DropDownList ID="DropDownListqu" runat="server" AutoPostBack="True"></asp:DropDownList>
CS:※DropDownList.Items.Clear(); 每調用一次填充方法就需要請空一下,否則數據會追加
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { sheng(); shi(); qu(); } } public void sheng()//填充省 { List<ChinaStates> listsheng = new ChinaStatesDA().Select("0001"); foreach (ChinaStates cssheng in listsheng) { ListItem lisheng = new ListItem(cssheng.AreaName, cssheng.AreaCode); DropDownListsheng.Items.Add(lisheng); } } public void shi()//填充市 { List<ChinaStates> listshi = new ChinaStatesDA().Select(DropDownListsheng.SelectedValue); foreach (ChinaStates csshi in listshi) { ListItem lishi = new ListItem(csshi.AreaName, csshi.AreaCode); DropDownListshi.Items.Add(lishi); } } public void qu()//填充區 { List<ChinaStates> listqu = new ChinaStatesDA().Select(DropDownListshi.SelectedValue); foreach (ChinaStates csqu in listqu) { ListItem liqu = new ListItem(csqu.AreaName, csqu.AreaCode); DropDownListqu.Items.Add(liqu); } } protected void DropDownListsheng_SelectedIndexChanged(object sender, EventArgs e) { DropDownListshi.Items.Clear(); DropDownListqu.Items.Clear(); shi(); qu(); } protected void DropDownListshi_SelectedIndexChanged(object sender, EventArgs e) { DropDownListqu.Items.Clear(); qu(); }