问题描述
有3个listboxlistbox1listbox2listbox3listbox1是省的信息listbox2是对应市的信息现在前面2个联动好用但是我想点button实现把listbox2的值添到listbox3中加了如下代码不好用只添加第一个省的市的信息请高手指点下应该怎么做才实现选完省信息对应的市添到listbox3中呢PrivateSubbtn_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handlesbtn.ClickDimiAsIntegerFori=0To(Me.listbox2.Items.Count)-1IfMe.listbox2.Items(i).Selected=TrueThenIfMe.Listbox3.Items.Contains(Me.listbox2.Items(i))=FalseThenListbox3.Items.Add(Me.listbox2.Items(i))EndIfEndIfNextiEndSub
解决方案
解决方案二:
循环倒着写看看PrivateSubbtn_Click(ByValsenderAsSystem.Object,ByValeAsSystem.EventArgs)Handlesbtn.ClickDimiAsIntegerFori=Me.listbox2.Items.Count-1To0Step-1IfMe.listbox2.Items(i).Selected=TrueThenIfMe.Listbox3.Items.Contains(Me.listbox2.Items(i))=FalseThenListbox3.Items.Add(Me.listbox2.Items(i))EndIfEndIfNextiEndSub
解决方案三:
LS的不好用哦。为什么我选listbox1里的值点击buttonlistbox2都是默认显示listbox1是1的值?
解决方案四:
你的联动有问题,发代码上来看.
解决方案五:
哦联动是着样的也是在网上找的'建立数据库连接DimconnAsSqlConnection=NewSqlConnection(ConfigurationSettings.AppSettings("connectionString"))'如果设置了web.config,也可以从web.config中读取数据库连接字符串conn.Open()DimcmdAsSqlCommand=NewSqlCommandcmd.Connection=conn'====================================================================================='生成客户端脚本控制的联动菜单'查询Tb_Province表获得所有的省份DimsqlstrAsString="SELECTzhuid,zhunameFROMzhulmorderbyzhuid"cmd.CommandText=sqlstrDimsqldaAsSqlDataAdapter=NewSqlDataAdapter(cmd)'建立临时表存储省份数据DimProvinceAsDataTable=NewDataTablesqlda.Fill(Province)'Response.Write("将省份数据绑定到DropDownList1控件")'Response.End()'将省份数据绑定到DropDownList1控件listbox1.DataSource=Provincelistbox1.DataValueField="zhuid"listbox1.DataTextField="zhuname"IfNotIsPostBackThenlistbox1.DataBind()'获取省份的个数DimProvinceNumAsInteger=Province.Rows.Count()'使用StringBuilder构造含客户端控制脚本的字符串DimsbscriptAsNewSystem.Text.StringBuildersbscript.Append("<script><!--")sbscript.Append(Environment.NewLine&"vargroup=newArray("&ProvinceNum&");"&Environment.NewLine&"for(i=0;i<"&ProvinceNum&";i++){group[i]=newArray()}"&Environment.NewLine)'循环从省份临时表中取出一个省份,然后在数据库中查询此省份下的城市DimiAsIntegerDimjAsIntegerDimtempclassAsDataTableDimCityNumAsIntegerFori=0ToProvinceNum-1sqlstr="SELECTfuid,funameFROMfulmWHERELeft(zhuid,2)='"&Province.Rows(i)(0)&"'orderbyfuid"cmd.CommandText=sqlstrsqlda=NewSqlDataAdapter(cmd)tempclass=NewDataTablesqlda.Fill(tempclass)CityNum=tempclass.Rows.Count()'Response.Write("省份:"&Province.Rows(i)(0)&"有市"&CityNum&"个<br>")'将读取到的城市数据放入客户端脚本中Forj=0ToCityNum-1sbscript.Append("group["&i&"]["&j&"]=newOption("""&tempclass.Rows(j)(1)&""","""&tempclass.Rows(j)(0)&""");"&Environment.NewLine)NextNext'加入客户端脚本控制代码sbscript.Append(Environment.NewLine&"functionRedirect(x){"&Environment.NewLine&"for(m=Form1.listbox2.options.length-1;m>0;m--){Form1.listbox2.options[m]=null}")sbscript.Append(Environment.NewLine&"if(group[x].length==0){")sbscript.Append(Environment.NewLine&"Form1.listbox2.options[0]=newOption('---请选择---','无')}")sbscript.Append(Environment.NewLine&"for(i=0;i<group[x].length;i++){"&Environment.NewLine&"Form1.listbox2.options[i]=newOption(group[x][i].text,group[x][i].value)}")sbscript.Append(Environment.NewLine&"Form1.listbox2.options[0].selected=true;Form1.classid.value=Form1.listbox2.options[0].value;}")sbscript.Append(Environment.NewLine&"functionchoose(x){Form1.classid.value=Form1.listbox2.options[x].value;}")sbscript.Append(Environment.NewLine&"-->")sbscript.Append("<")sbscript.Append("/")sbscript.Append("script>")'注册客户端脚本,将脚本内容写入客户端输出流DimstrscriptAsString=sbscript.ToString()If(NotIsClientScriptBlockRegistered("clientScript"))ThenRegisterClientScriptBlock("clientScript",strscript)EndIf'====================================================================================='初始化DropDownList2中的内容sqlstr="SELECTfuid,funameFROMfulmWHERE(Left(zhuid,2)='"&Province.Rows(0)(0)&"')"cmd.CommandText=sqlstrsqlda=NewSqlDataAdapter(cmd)DimdtCityAsDataTable=NewDataTablesqlda.Fill(dtCity)listbox2.DataSource=dtCitylistbox2.DataValueField="fuid"listbox2.DataTextField="funame"IfNotIsPostBackThenlistbox2.DataBind()sqlda.Dispose()cmd.Dispose()conn.Close()
解决方案六:
你是用的客户端的连动,不能保存listbox2的状态.所以出现这种情况,简单的做法是在服务器端连动,将listbox1的autopostback设为true,在selectedindexchanged事件中联动,这样才能保存状态,否则用上面的方法还要在Page_load中重新根据listbox1的选项再生成listbox2的选项,然后根据Request.Form["listobx2的值来设置listbox2的选中的项.
解决方案七:
直接绑定的话把值取出来然后赋值给list3
解决方案八:
selectedindexchanged是什么事件没用过
解决方案九:
大哥们详细给讲讲把不会写
解决方案十:
selectedindexchanged就是选择listbox1时触发的服务器事件,将litbox1设成autopostback="true"就会触发这个事件。
解决方案十一:
HTML代码:<asp:ListboxID="Listbox1"runat="server"OnSelectedIndexChanged="Listbox1_SelectedIndexChanged"AutoPostBack="True">CS代码:protectedvoidListbox1_SelectedIndexChanged(objectsender,EventArgse){这里根据listbox1的selectedvalue来绑定listbox2}
解决方案十二:
引用10楼cpp2017的回复:
HTML代码:<asp:ListboxID="Listbox1"runat="server"OnSelectedIndexChanged="Listbox1_SelectedIndexChanged"AutoPostBack="True">CS代码:protectedvoidListbox1_SelectedIndexChanged(objectsender,EventArgse){这里根据listbox1的selectedvalue来绑定listbox2}
要是着样联动的话就不用我上面的代码了把?怎么联动。。。
解决方案十三:
不用上面的代码了。怎么联动?查数据库,绑定listbox2条件就是省=listbox1的选中的值,这里应该是单选吧?考虑用dropdownlsit
解决方案十四:
不知道要怎么写
解决方案十五:
谁会给个例子什么的可以么不知道应该怎么做
解决方案:
好久了大家帮帮忙把