构建多选用户控件的问题

Issue with building multiselect user control

我正在 vb.net 中构建一个 MultiSelect UserControl 我有以下问题来填充我创建的多选。 示例代码如下

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

我需要比较 SelectedValues(String Array) 和 Items(Dict) 将 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

我将它渲染成文字输出 <options></option><select></select>

提前致谢。

这是我Answhere的问题

    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