CheckBoxList数据绑定错误

时间:2012-10-24 00:03:09

标签: asp.net vb.net data-binding

我在使用数据加载CheckBoxList时遇到了一些麻烦。我在DisplayMember和ValueMember上遇到了一些错误。

这是我的鳕鱼:。

Protected Sub LoadCBMembers(ByVal objDeptGUID As String)
    Try
        Dim myCBTable As New DataTable()
        With myCBTable.Columns
            .Add("DisplayName", GetType(String))
            .Add("TextValue", GetType(String))   '<<<< change the type of this column to what you actually need instead of integer.
        End With
        Dim lqMembers As New lqFireFighterConnectDataContext
        Dim GetMembers = From r In lqMembers.tbUsers
                         Where r.DeptGUID = objDeptGUID And r.TextFLag = True
                         Select r
        If GetMembers.Count = 0 Then
            Exit Sub
        End If
        For Each foundMember In GetMembers
            myCBTable.Rows.Add(foundMember.FirstName & " " & foundMember.LastName, foundMember.CellPhone & "@" & foundMember.PhoneCarriers)
            '  cbMembers.Items.Add(foundMember.FirstName & " " & foundMember.LastName)
        Next
        With cbMembers
            .DataSource = myCBTable
            .DisplayMember = "DisplayName"
            .ValueMember = "TextValue"
        End With

    Catch ex As Exception

    End Try
End Sub

1 个答案:

答案 0 :(得分:0)

试试这个:

    With cbMembers
        .DataSource = myCBTable
        .DisplayMember = "DisplayName"
        .ValueMember = "TextValue"
        .DataBind()
    End With

根据documentation,必须明确调用此方法。并非所有控件都要求您显式调用DataBind()(对于许多控件而言,它是隐式调用的),但CheckBoxList是一个例外。