实名认证用户熊川湘 身份证号码430811198506290914

[转载内容]C# win程序中主窗体菜单的权限控制

准备:
操作员表UserInfo(UserID,UserName)
权限项表MenuPower(MenuName,UserID)

使用:
  1、读取当前操作员所具有的权限项:
  //DataTable tbP ="SELECT MenuName FROM MenuPower WHERE UserID="&当前登录的操作员ID
  string ConnectionString="连接字符串";
  string sql="SELECT MenuName FROM MenuPower WHERE UserID="&当前登录的操作员ID;
  SqlDataAdapter adp=new SqlDataAdapter(sql,ConnectionString);
  DataTable tbP=new DataTable();
  adp.Fill(tbP);   2、根据权限项处理菜单的可用状态:
     Call InitPower(Me.MainMenu)'在Form_Load中调用
    '以下是方法实现
    Private Sub InitPower(ByVal parent As Menu)
        For Each item As MenuItem In parent.MenuItems
            If item.Text <> "-" Then '不处理分隔条
                Dim Rows As DataRow() = tbP.Select("MenuName='" & item.Text & "'")
                If Rows.Length Then
                    item.Enabled = True
                Else
                    item.Enabled = False
                End If
                InitPower(item) '处理该菜单的子菜单
            End If
        Next
    End Sub
权限管理:
    TreeView:tvwMenu CheckBoxes=true
    '创建菜单树节点
    Private Sub CreateTree(ByVal parentMenu As Menu, ByVal parentTree As Object)
        For Each item As MenuItem In parentMenu.MenuItems
            If item.Text <> "-" Then
                Dim Node As New TreeNode(item.Text)
                Dim Rows As DataRow() = tbP.Select("MenuName='" & item.Text & "'")
                If Rows.Length Then
                    item.Checked = True
                Else
                    item.Checked = False
                End If
                If TypeOf parentTree Is TreeView Then
                    CType(parentTree, TreeView).Nodes.Add(Node)
                Else
                    CType(parentTree, TreeNode).Nodes.Add(Node)
                End If
                CreateTree(item, Node)
            End If
        Next
    End Sub
    Private Sub tvwMenu_AfterCheck(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvwMenu.AfterCheck
        '处理保存当前项的权限
        '如果是选中则在MenuPower表中插入这条菜单记录
        '否则删除这条菜单记录
        '记得使用UserID做条件
    End Sub 

posted @ 2008-12-06 18:53  浪达短信群发  阅读(559)  评论(0编辑  收藏  举报