DropDownList的多级联动的问题最典型的案例就是实现省市级三级联动的案例,对这个问题的描述是当选中山东省之后,在选择市的下拉菜单时,市一栏只出现山东省下面的市。对于县也是同样的道理。
我也做的事情和这个也很相似,只不过我要做的是六级联动,我觉得原理肯定是一样的,只要能做出来三级联动,那按照同样的道理,一定可以做出刘级联动。在网上搜索一番后发现有两种说法,在这里给大家一一阐述。第一:用ajax完成无刷新的方案,第二:使用.netdropdownlist自带的事件SelectedIndexChanged。 对于第一个方法,我是非常想用的,但是要做的东西非常急,新学一门技术时间可能不够(可能是自己懒),对于第二种,网上大多数的会出现不能触发的情况。 两种都是两难的选择,最终,我选择了第二种方式,我觉得不能触发的原因一定是某些设置没有做好。
方法如下:
第一:将页面的ViewState设置为true
第二:将所有的DropDownList的AutoPostBack设置为true
这个时候就可以在每个SelectedIndexChanged函数中编写想要的效果。我的是实现方言名的选择:1.首先选择方言类别(闽南话,官话...等等),2.接下来是这个方言类别的地域(华中,华东,东北.....),3.是这个方言的小片区域如(桂柳,江泽,。。。)4.是城市名,5.是方言的名字,6是省份。
以下我的代码
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { //将大区绑定到DaQu控件 string sqlDaQu = "select distinct BigArea from Reference "; DropListBind(DropDownListDaQu, TBigArea, sqlDaQu, "BigArea"); DropDownListDaQu.Items.Insert(0, new ListItem("大区")); } } protected void DropDownListDaQu_SelectedIndexChanged(object sender, EventArgs e) //当选择大区后触发 { BigArea = DropDownListDaQu.SelectedValue.ToString(); string sqlQu = "select distinct Area from Reference where BigArea='" + BigArea + "' "; DropListBind(DropDownListQu, TArea, sqlQu, "Area"); DropDownListQu.Items.Insert(0, new ListItem("区")); // Label1.Text = BigArea; } protected void DropDownListQu_SelectedIndexChanged(object sender, EventArgs e) //当选择区后触发 { Area = DropDownListQu.SelectedValue.ToString(); string sqlPian = "select distinct Piece from Reference where BigArea='" + BigArea + "' and Area='" + Area + "'"; DropListBind(DropDownListDaPian, TPiece, sqlPian, "Piece"); DropDownListDaPian.Items.Insert(0, new ListItem("片")); } protected void DropDownListDaPian_SelectedIndexChanged(object sender, EventArgs e) { Piece = DropDownListDaPian.SelectedValue.ToString(); string sqlXiaoPian = "select distinct SmallPiece from Reference where BigArea='" + BigArea + "' and Area='" + Area + "' and Piece='" + Piece + "'"; DropListBind(DropDownListXiaoPian, TSmallPiece, sqlXiaoPian, "SmallPiece"); DropDownListXiaoPian.Items.Insert(0, new ListItem("小片")); } protected void DropDownListXiaoPian_SelectedIndexChanged(object sender, EventArgs e)//当选择片后触发 { SmallPiece = DropDownListXiaoPian.SelectedValue.ToString(); string sqlDian = "select distinct Point from Reference where BigArea='" + BigArea + "' and Area='" + Area + "' and Piece='" + Piece + "'and SmallPiece='" + SmallPiece + "'"; DropListBind(DropDownListDian, TPoint, sqlDian, "Point"); DropDownListDian.Items.Insert(0, new ListItem("点")); } protected void DropDownListDian_SelectedIndexChanged(object sender, EventArgs e)//当选择小片后触发 { Point = DropDownListDian.SelectedValue.ToString(); string sqlProvince = "select distinct State from Reference where BigArea='" + BigArea + "' and Area='" + Area + "' and Piece='" + Piece + "'" + "and SmallPiece='" + SmallPiece + "' and Point='" + Point + "'"; DropListBind(DropDownListProvince, TState, sqlProvince, "State"); DropDownListProvince.Items.Insert(0, new ListItem("省")); }
这样就完毕了。