将checkboxlist选中的项目作为连接字符串传递

时间:2012-08-01 17:38:25

标签: asp.net vb.net .net-2.0 concatenation checkbox

我正在尝试将asp.net中的复选框列表中的所选项目(vs 2005 / .net 2.0)作为连接字符串传递。

目前我的.aspx是

        <asp:CheckBoxList id="checkbox1" AutoPostBack="False" AppendDataBoundItems="true" CellPadding="5" CellSpacing="5" RepeatColumns="1" RepeatDirection="Vertical" RepeatLayout="Flow" TextAlign="Right" runat="server">
            <asp:ListItem Value="1">Carrots</asp:ListItem>
            <asp:ListItem Value="2">Lettuce</asp:ListItem>
            <asp:ListItem Value="3">Olives</asp:ListItem>
            <asp:ListItem Value="4">Onions</asp:ListItem>
            <asp:ListItem Value="5">Tomato</asp:ListItem>
            <asp:ListItem Value="6">Pickles</asp:ListItem>
        </asp:CheckBoxList>

.aspx.vb是(在Protected Sub中提交)

    For Each li As ListItem In checkbox1.Items
        If li.Selected = True Then
            checkbox1.Text = checkbox1.Text + "," + li.Text
        End If
    Next

通过

写入数据库
checkbox1.Text = dv(0)("Salad").ToString()

当我选择并保存时,我目前收到错误

'/'应用程序中的服务器错误。

'checkbox1'的SelectedValue无效,因为它在项目列表中不存在。

参数名称:值

有关如何连接所选复选框项目的任何想法

例如,如果有人选择胡萝卜,生菜和番茄;

checkbox1 = 1,2,5

2 个答案:

答案 0 :(得分:4)

我认为你没有像你描述的那样分配一个变量。

string list = "";
For Each li As ListItem In chkQ4.Items
        If li.Selected = True Then
            list = list + "," + li.Text
        End If
    Next

是你应该如何写上面这一行。

在使用linq的C#中,我会写

var list =  checkbox1.Items
.Cast<ListItem>()
.Where(item => item.Selected == true)
.Select(item => item.Value);
var result = string.Join(",",list);

我相信VB中的以下内容

Dim list = checkbox1.Items.Cast(Of ListItem)().Where(Function(item) item.Selected = True).[Select](Function(item) item.Value)
Dim result = String.Join(",", list.ToArray())

答案 1 :(得分:0)

这是一个小例子

标记:

<asp:CheckBoxList ID="CheckBoxList1" runat="server">
<asp:ListItem Value="1">Carrots</asp:ListItem>
<asp:ListItem Value="2">Apples</asp:ListItem>
<asp:ListItem Value="3">Lettuce</asp:ListItem>        
</asp:CheckBoxList>
<br />
<asp:Literal ID="Literal1" runat="server"></asp:Literal><br />
<br />
<asp:Button ID="Button1" runat="server" Text="Concatenate" /><br />
<asp:Button ID="Button2" runat="server" Text="Save" />

代码隐藏:

 Private Sub SaveItems(ByVal strItems As String)

    Dim cn As New SqlConnection("user id=sa;password=abcd;database=BD_Test;server=SERVNAME")
    Dim cmd As New SqlCommand("Insert into CheckItems values (@ItemId)", cn)
    Dim cmdPar As New SqlParameter("@ItemId", SqlDbType.Char, 1)

    If strItems.Split(",").Length > 0 Then

        cmd.Parameters.Add(cmdPar)
        Using cn
            cn.Open()
            Using cmd 
            ''Split the existing selected values, store it in an array 
            ''and iterate to get each element of it to save it in the DB
            For Each strItem As String In strItems.Split(",")                    
                    cmd.Parameters("@ItemId").Value = strItem
                    cmd.ExecuteNonQuery()                    
            Next
            End Using  
            Me.Literal1.Text = "Items saved"
        End Using

    End If

End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Me.Literal1.Text = ""

    For Each l As ListItem In Me.CheckBoxList1.Items

        ''Concatenate keeping the order of the items in your CheckboxList
        If l.Selected Then
            Me.Literal1.Text = Me.Literal1.Text & l.Value & ","
        End If

    Next

    ''Remove the final "," in case its at the end of the string 
    ''to avoid db issues and selected items issues
    If Right(Me.Literal1.Text, 1) = "," Then
        Me.Literal1.Text = Left(Me.Literal1.Text, Me.Literal1.Text.Length - 1)
    End If

    ''You can also save the items directly after concatenating
    ''Me.SaveItems(Me.Literal1.Text)

End Sub

Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click

    Me.SaveItems(Me.Literal1.Text)

End Sub

预期结果是在页面文字上选择项目的值:

1,3

希望这有帮助。