在编辑模式下未为Gridview CheckboxList设置的值

时间:2018-10-31 16:28:56

标签: asp.net vb.net gridview checkboxlist

我已经反复研究这个问题了几个星期,包括许多互联网搜索。似乎没有什么与我要做的事情保持一致。我在SQL Server中有五个布尔字段。我可以更新它们并将其显示在标签字段中,而不会出现问题。编辑模式在checkBoxList中构造它们,但我没有显示数据库值。这是OnRowEditing代码:

     Protected Sub EmplSumm_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) Handles EmplSummGridView.RowEditing
    WriteToFile.WTF("EmployeeSummary.EmplSumm_RowEditing:" & e.NewEditIndex.ToString, Gbl.LogPath)
    Try
        UpdateIndex = e.NewEditIndex
        EmplSummGridView.EditIndex = e.NewEditIndex
        Dim SQLStm As String = "SELECT W4State, W4Allowances, W4Exempt, " &
            "IdSocialSecurity, IdBirthCert, IdDriverLic, IdPassport, IdPermit " &
            "From EmploymentInfo " &
            "Where emplid = '" & EmplSummGridView.DataKeys(e.NewEditIndex).Values(0).ToString() & "'; "
        Dim connection As New SqlConnection(connectionString)
        Dim adapter As New SqlDataAdapter(SQLStm, connection)
        Dim dt1 As New DataTable()
        adapter.Fill(dt1)

        'Dim row As GridViewRow = EmplSummGridView.Rows(UpdateIndex)

        WriteToFile.WTF("EmployeeSummary.EmplSumm_RowEditing:" & "|" & UpdateIndex & "|" & dt1.Rows(0).Item("IdSocialSecurity").ToString, Gbl.LogPath)
        Dim cbSecBirth As CheckBoxList = TryCast(EmplSummGridView.Rows(e.NewEditIndex).FindControl("cbSecBirthCertE"), CheckBoxList)

        cbSecBirth.Items(0).Selected = Convert.ToBoolean(dt1.Rows(0).Item("IdSocialSecurity"))
        cbSecBirth.Items(1).Selected = Convert.ToBoolean(dt1.Rows(0).Item("IdBirthCert"))
        cbSecBirth.Items(2).Selected = Convert.ToBoolean(dt1.Rows(0).Item("IdDriverLic"))
        cbSecBirth.Items(3).Selected = Convert.ToBoolean(dt1.Rows(0).Item("IdPassport"))
        cbSecBirth.Items(4).Selected = Convert.ToBoolean(dt1.Rows(0).Item("IdPermit"))
        cbSecBirth.DataBind()
        WriteToFile.WTF("EmployeeSummary.EmplSumm_RowEditing cbSecBirth:" & "|" & cbSecBirth.Items(0).Selected _
            & "|" & cbSecBirth.Items(1).Selected & "|" & cbSecBirth.Items(2).Selected _
            & "|" & cbSecBirth.Items(3).Selected & "|" & cbSecBirth.Items(4).Selected, Gbl.LogPath)
    Catch ex As Exception
        Dim S As String = ex.Message + vbCrLf
        S = S + "Help Link:" + ex.HelpLink + vbCrLf
        S = S + "Source:" + ex.Source + vbCrLf
        S = S + "Stack Trace:" + ex.StackTrace + vbCrLf
        'lblerror.Text = "System Error - System Error - Employee_ItemUpdating: " & S
        WriteToFile.WTF("Exception in EmployeeSummary.EmplSumm_RowEditing:" & Now() & "|" & S, Gbl.LogPath)
    End Try
End Sub

和ASP:

                <asp:Templatefield HeaderText="SecBirthCertDL" SortExpression="SecBirthCertDL" ItemStyle-CssClass="tableitem1">
            <ItemTemplate>
                <asp:Label ID="lblId" runat="server" readonly ="True"></asp:Label>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:CheckBoxList ID="cbSecBirthCertE" runat="server" autopostback="false" >
                <asp:ListItem  Text="SS Card" ></asp:ListItem>
                <asp:ListItem  Text="Birth Certificate" ></asp:ListItem>
                <asp:ListItem  Text="Drivers License" ></asp:ListItem>
                <asp:ListItem  Text="Passport" ></asp:ListItem>
                <asp:ListItem  Text="Permit" ></asp:ListItem>
                </asp:CheckBoxList>                
            </EditItemTemplate>
        </asp:Templatefield>

从数据库到CheckBoxList的值设置正确,例如:cbSecBirth.Items(0)。已选择但在呈现表单时不会显示。这是WriteToFile函数的输出: EmployeeSummary.EmplSumm_RowEditing cbSecBirth:| True | True | False | False | False

1 个答案:

答案 0 :(得分:0)

我通过在RowDataBound事件中设置值并排除在更新行上设置标签值来使其工作。相关代码:

                Dim cbSecBirth As CheckBoxList = TryCast(e.Row.Cells(9).Controls(0).FindControl("cbSecBirthCertE"), CheckBoxList)
                If cbSecBirth Is Nothing Then
                    WriteToFile.WTF("EmployeeSummary.EmplSumm_RowDataBound:lblId Error:" & lblId.Text, Gbl.LogPath)
                    Return
                End If
                Dim ix As Int16 = 0

                ' Values for List item elements are 0 thru 4
                For Each Li As ListItem In cbSecBirth.Items
                     Li.Selected = Convert.ToBoolean(dt1.Rows(0).Item(ix + 3))
                    ix += 1
                Next