ListBox仅显示所选的第一个项目

时间:2014-06-09 22:24:41

标签: asp.net vb.net visual-studio-2010 listbox

我正在ASPX/VB网站上工作。我有一个ListBox但是当你选择多个选项时,只有一个项目被传递到下一页,它将项目放入一个无序列表中。

我似乎无法显示多个所选项目。

ASPX:

<asp:ListBox runat="server" id="dlcenter1" SelectionMode="Multiple" >
    <asp:ListItem name="option0[]" Value="0" multiple="multiple">None</asp:ListItem>
    <asp:ListItem name="option1[]" Value="1" multiple="multiple">Assistant managers</asp:ListItem>
    <asp:ListItem name="option2[]" Value="2" multiple="multiple">Customer service representatives</asp:ListItem>
    <asp:ListItem name="option3[]" Value="3" multiple="multiple">General managers</asp:ListItem>
    <asp:ListItem name="option4[]" Value="4" multiple="multiple">Hitch installers</asp:ListItem>
    <asp:ListItem name="option5[]" Value="5" multiple="multiple">Manager trainee</asp:ListItem>
    <asp:ListItem name="option6[]" Value="6" multiple="multiple">MOONLIGHTERS WANTED</asp:ListItem>
    <asp:ListItem name="option7[]" Value="7" multiple="multiple">Store managers</asp:ListItem>
    <asp:ListItem name="option8[]" id="option8" Value="8" multiple="multiple">U-Box Customer Care Rep</asp:ListItem>
 </asp:ListBox>

VB

If Not Page.IsPostBack Then
        dlCenter1.DataBind()
        'dlCenter2.DataBind()
        'dlCenter3.DataBind()
        'dlCenter4.DataBind()
        'dlCenter5.DataBind()
        GetEntityInfo()
    End If
End Sub


Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

    Session.Clear()
    lblError.Visible = False

    'Declare variables to place into session
    Dim dl1 As String = dlcenter1.SelectedItem.Text
    'Dim dl2 As String = dlCenter2.SelectedItem.Text
    'Dim dl3 As String = dlCenter3.SelectedItem.Text
    'Dim dl4 As String = dlCenter4.SelectedItem.Text
    'Dim dl5 As String = dlCenter5.SelectedItem.Text

    Dim name As String = txtName.Text.ToString.Trim()
    Dim address As String = txtAddress.Text.ToString.Trim()
    Dim city As String = txtCity.Text.ToString.Trim()
    Dim state As String = ddlState.SelectedItem.Value.ToString.Trim()
    Dim zipcode As String = txtZipCode.Text.ToString.Trim()
    Dim phone As String = txtPhone.Text.ToString.Trim()
    Dim fax As String = txtFax.Text.ToString.Trim()

    'Session validation
    If Not dlCenter1.SelectedIndex = 0 Then
        Session.Add("dl1", dl1.ToString)
    End If

2 个答案:

答案 0 :(得分:1)

首先,您需要知道ListItem是一个集合,您需要逐个浏览项目,否则您无法将整个集合分配到一个变量中。

所以要做到这一点你需要在你的函数中创建一个循环,这里是C#的答案,希望你能把它转换成VB。

        string selectedItems = string.Empty;

        foreach (ListItem item in dlcenter1.Items)
        {
            if (item.Selected)
            {
                //I used ',' to separate the selected values you can separate it with other characters or even use a list to save the selected items 
                selectedItems += item.value + ", ";
            }
        }

        Session.Add("dl1", selectedItems);

答案 1 :(得分:0)

Dim selectedItems As String = String.Empty

    For Each item As ListItem In dlCenter1.Items
        If item.Selected Then
            selectedItems += "<li>" & item.Value & "</li>"
        End If
    Next

    Session.Add("dl1", selectedItems)