CheckBoxList检查依赖dropdownlist asp.net VB

时间:2017-03-14 12:07:16

标签: asp.net vb.net entity-framework

我试图从数据库中获取值(true或false),但我想从角色(DropDownList)中选择以显示是否在部分中有权限

  

使用entitdatasource

的下拉列表和复选框列表

我试试这个(DropdownList(ddlRole))

 Protected Sub ddlRole_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlRole.SelectedIndexChanged
    Using context As New AGIP_dbModel.AGIP_dbEntities()
        For Each oItem As ListItem In ckSection.Items()
            Dim objPerm As tbl_permission = New tbl_permission()
            oItem.Value = objPerm.pre_status
        Next

    End Using
End Sub

这个submite按钮如何工作(存储在数据库中)

Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    Using context As New AGIP_dbModel.AGIP_dbEntities()

        Dim id As Integer = ddlRole.SelectedValue

        Try

            Dim obj = context.tbl_permission.Where(Function(u) u.role_id = id)
            For Each permission As tbl_permission In obj.ToList
                context.tbl_permission.DeleteObject(permission)
                context.SaveChanges()
            Next


        Catch ex As Exception


        End Try
        For Each oItem As ListItem In ckSection.Items()
            Dim objPerm As tbl_permission = New tbl_permission()
            objPerm.role_id = ddlRole.SelectedValue
            objPerm.pre_status = oItem.Selected
            objPerm.section_id = oItem.Value
            context.tbl_permission.AddObject(objPerm)
            context.SaveChanges()

        Next
        Response.Redirect("permission.aspx")
    End Using
End Sub

permission.aspx

tbl_role

tbl_role

tbl_section

tbl_section

与角色和部分

的tbl_permission关系

tbl_permission

enter image description here

2 个答案:

答案 0 :(得分:0)

我对实体的使用还不熟悉所以我希望你能得到我在这里尝试做的事情。

也不确定复选框是否有标题或文字属性,请检查。

Protected Sub ddlRole_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlRole.SelectedIndexChanged
    Using context As New AGIP_dbModel.AGIP_dbEntities()
        For Each oItem As ListItem In ckSection.Items()
            Dim objPerm As tbl_permission = New tbl_permission()
            If objPerm.section_name = oItem.Caption Then 'Check if the permission record is same with the caption of the current checkbox in iteration
                oItem.Checked = objPerm.pre_status
            End If
        Next oItem
    End Using
End Sub

如果您还没有,则还需要在返回权限记录时修改SQL查询以包含section_name。

答案 1 :(得分:0)

终于找到了答案:

Protected Sub ddlRole_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ddlRole.SelectedIndexChanged
        Using context As New BWJO_dbModel.BWJO_dbEntities


            Try
                For Each secTionItem As ListItem In ckSection.Items


                    secTionItem.Selected = False
                    For Each oItem As ListItem In ckSection.Items
                        Dim PermObj = context.tbl_permission.Any(Function(u) u.role_id = ddlRole.SelectedValue And u.permission_status = True And u.section_id = oItem.Value)
                        If PermObj = True Then
                            oItem.Selected = True
                        End If

                    Next


                Next
            Catch ex As Exception

            End Try


        End Using


    End Sub
End Class