1

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

2 回答 2

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())
于 2012-08-01T17:59:06.153 回答
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

希望这可以帮助。

于 2012-08-01T18:13:45.197 回答