【问题标题】:passing checkboxlist selected items as a concatenated string将 checkboxlist 所选项目作为连接字符串传递
【发布时间】:2012-10-23 18:17:59
【问题描述】:

我正在尝试将 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

【问题讨论】:

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


    【解决方案1】:

    我认为您没有像您描述的那样分配给您要返回的变量。

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

    上面一行应该怎么写。

    在 C# 中使用 linq,我会写

    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())
    

    【讨论】:

    • 我认为最后一行应该是: var result = string.Join(",",list.ToArray());为我工作!
    • 谢谢,我现在改了。
    【解决方案2】:

    这是一个小例子

    标记:

    <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
    

    希望这会有所帮助。

    【讨论】:

    • 我试过你的方法,它发送的是第一个被选中的项目,而不是一个字符串,所有被选中的项目。有什么想法吗?
    • 我已经更新了代码,假设您有一个名为 CheckItems 的表,其中包含一个字段来保存每个项目。您还可以保存所有构建的字符串“1,2,3”,但您需要删除“for each”语句,以便 cmd.Parameters("@ItemId").Value 仅采用文字中的整个文本并执行节省一次。让我知道它是否有效。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2012-07-11
    • 1970-01-01
    • 2017-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多