我正在构建一个多选用户控件 vb.net我在填充我创建的多选项时遇到以下问题。示例代码如下
SumoSelect() 的 Javascript 是客户端,一切都在工作,我唯一需要的是以编程方式设置选定的值或不:)
https://hemantnegi.github.io/jquery.sumoselect/
Multiselect.ascx
Property SelectedValues() As String()
Get
Return selValues.Value.Split(",")
End Get
Set(value As String())
For i As Integer = 0 To value.Length - 1
If i = 0 Then
selValues.Value = value(i)
Else
selValues.Value += "," + value(i)
End If
Next
End Set
End Property
Sub New()
Items = New Dictionary(Of Object, Object)
End Sub
Public Items As Dictionary(Of Object, Object)
Private Function CreateItems() As String
Dim sb As New StringBuilder()
For Each item In Items
sb.AppendFormat("<option value=""{0}"">{0}</option>", item.Key, item.Value)
Next
Return sb.ToString()
End Function
'' selValues is a HiddenField clientside
设置所选值:
MultiSelect1.SelectedValues() = New String() {"Banana Color", "Green Color", "Juice Color"}
人口:
For Each item In PjtColor.Fetch(Nothing, Nothing, Nothing, Nothing)
MultiSelect1.Items.Add(item.ColorName, item.ColorName)
Next
我需要比较选定值(字符串数组)和项目(字典)将sb.AppendFormat("<option value=""{0}"" selected=""selected"">{0}</option>", item.Key, item.Value)
设置为"已选中",当不匹配时
sb.AppendFormat("<option value=""{0}"">{0}</option>", item.Key, item.Value)
基于: http://hemantnegi.github.io/jquery.sumoselect/sumoselect_demo.html
我将其渲染为文本,该文本输出<select></select>
内的<options></option>
提前谢谢。
这是我的问题
For i As Integer = 0 To SelectedValues.Count - 1
SelectedItems.Add(SelectedValues(i), SelectedValues(i))
Next
For Each item In Items
If SelectedItems.ContainsKey(item.Key) Then
sb.AppendFormat("<option value=""{0}"" selected=""selected"">{0}</option>", item.Key, item.Value)
Else
sb.AppendFormat("<option value=""{0}"">{0}</option>", item.Key, item.Value)
End If
Next