listbox添加的问题?
有3个listbox listbox1 listbox2 listbox3listbox1是省的信息 listbox2是对应市的信息 现在前面2个联动好用 但是 我想点button实现把listbox2的值添到listbox3 中 加了如下代码 不好用 只添加 第一个省的市的信息 请高手指点下 应该怎么做才实现选完省信息对应的市添到listbox3中呢
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
Dim i As Integer
For i = 0 To (Me.listbox2.Items.Count) - 1
If Me.listbox2.Items(i).Selected = True Then
If Me.Listbox3.Items.Contains(Me.listbox2.Items(i)) = False Then
Listbox3.Items.Add(Me.listbox2.Items(i))
End If
End If
Next i
End Sub --------------------编程问答-------------------- 循环倒着写看看
Private Sub btn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn.Click
Dim i As Integer
For i = Me.listbox2.Items.Count - 1 To 0 Step -1
If Me.listbox2.Items(i).Selected = True Then
If Me.Listbox3.Items.Contains(Me.listbox2.Items(i)) = False Then
Listbox3.Items.Add(Me.listbox2.Items(i))
End If
End If
Next i
End Sub --------------------编程问答-------------------- LS的不好用哦。为什么我选listbox1里的值 点击button listbox2都是默认显示listbox1是1的值? --------------------编程问答-------------------- 你的联动有问题,发代码上来看. --------------------编程问答-------------------- 哦 联动是着样的 也是在网上找的
'建立数据库连接
Dim conn As SqlConnection = New SqlConnection(ConfigurationSettings.AppSettings("connectionString")) '如果设置了web.config,也可以从web.config中读取数据库连接字符串
conn.Open()
Dim cmd As SqlCommand = New SqlCommand
cmd.Connection = conn
'=====================================================================================
'生成客户端脚本控制的联动菜单
'查询Tb_Province表获得所有的省份
Dim sqlstr As String = "SELECT zhuid, zhuname FROM zhulm order by zhuid"
cmd.CommandText = sqlstr
Dim sqlda As SqlDataAdapter = New SqlDataAdapter(cmd)
'建立临时表存储省份数据
Dim Province As DataTable = New DataTable
sqlda.Fill(Province)
'Response.Write("将省份数据绑定到DropDownList1控件")
'Response.End()
'将省份数据绑定到DropDownList1控件
listbox1.DataSource = Province
listbox1.DataValueField = "zhuid"
listbox1.DataTextField = "zhuname"
If Not IsPostBack Then listbox1.DataBind()
'获取省份的个数
Dim ProvinceNum As Integer = Province.Rows.Count()
'使用StringBuilder构造含客户端控制脚本的字符串
Dim sbscript As New System.Text.StringBuilder
sbscript.Append("<script><!--")
sbscript.Append(Environment.NewLine & "var group=new Array(" & ProvinceNum & ");" & Environment.NewLine & "for (i=0; i<" & ProvinceNum & "; i++){group[i] = new Array()}" & Environment.NewLine)
'循环从省份临时表中取出一个省份,然后在数据库中查询此省份下的城市
Dim i As Integer
Dim j As Integer
Dim tempclass As DataTable
Dim CityNum As Integer
For i = 0 To ProvinceNum - 1
sqlstr = "SELECT fuid, funame FROM fulm WHERE Left(zhuid,2) = '" & Province.Rows(i)(0) & "' order by fuid"
cmd.CommandText = sqlstr
sqlda = New SqlDataAdapter(cmd)
tempclass = New DataTable
sqlda.Fill(tempclass)
CityNum = tempclass.Rows.Count()
'Response.Write("省份:" & Province.Rows(i)(0) & "有市" & CityNum & "个<br>")
'将读取到的城市数据放入客户端脚本中
For j = 0 To CityNum - 1
sbscript.Append("group[" & i & "][" & j & "]=new Option(""" & tempclass.Rows(j)(1) & """,""" & tempclass.Rows(j)(0) & """);" & Environment.NewLine)
Next
Next
'加入客户端脚本控制代码
sbscript.Append(Environment.NewLine & "function Redirect(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]=new Option('---请选择---','无')}")
sbscript.Append(Environment.NewLine & "for (i=0;i<group[x].length;i++){" & Environment.NewLine & "Form1.listbox2.options[i]=new Option(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 & "function choose(x){Form1.classid.value=Form1.listbox2.options[x].value;}")
sbscript.Append(Environment.NewLine & "-->")
sbscript.Append("<")
sbscript.Append("/")
sbscript.Append("script>")
'注册客户端脚本,将脚本内容写入客户端输出流
Dim strscript As String = sbscript.ToString()
If (Not IsClientScriptBlockRegistered("clientScript")) Then
RegisterClientScriptBlock("clientScript", strscript)
End If
'=====================================================================================
'初始化DropDownList2中的内容
sqlstr = "SELECT fuid,funame FROM fulm WHERE (Left(zhuid,2) = '" & Province.Rows(0)(0) & "')"
cmd.CommandText = sqlstr
sqlda = New SqlDataAdapter(cmd)
Dim dtCity As DataTable = New DataTable
sqlda.Fill(dtCity)
listbox2.DataSource = dtCity
listbox2.DataValueField = "fuid"
listbox2.DataTextField = "funame"
If Not IsPostBack Then listbox2.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:Listbox ID="Listbox1" runat="server" OnSelectedIndexChanged="Listbox 1_SelectedIndexChanged" AutoPostBack="True">
CS代码:
protected void Listbox1_SelectedIndexChanged(object sender, EventArgs e)
{
这里根据listbox1的selectedvalue来绑定listbox2
}
--------------------编程问答--------------------
要是着样联动的话 就不用我上面的代码了把? 怎么联动。。。 --------------------编程问答-------------------- 不用上面的代码了。怎么联动?
查数据库,绑定listbox2
条件就是 省=listbox1的选中的值,这里应该是单选吧?考虑用dropdownlsit --------------------编程问答-------------------- 不知道要怎么写 --------------------编程问答-------------------- 谁会给个例子什么的可以么 不知道应该怎么做 --------------------编程问答-------------------- 好久了 大家帮帮忙把
补充:.NET技术 , ASP.NET