构建多选用户控件的问题

时间:2015-06-18 12:22:46

标签: javascript jquery asp.net linq sumoselect.js

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

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

设置SelectedValues:

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

我将其渲染为Literal,在<options></option>

中输出<select></select>

提前致谢。

1 个答案:

答案 0 :(得分:0)

这是我的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