[ASP.NET]使用.NET的會員系統管理Role

會員系統的Role可視為群組或公司的部門,也可以一個使用者有多重群組,Role也可以做權限的控制。

會員系統的Role可視為群組或公司的部門,也可以一個使用者有多重群組,Role也可以做權限的控制。

 

建立管理頁面

 

下列圖為頁面原始碼,主要是Container.DataItem在取資料後顯示在所在的位置上。

 

Partial Public Class RoleMain
	Inherits System.Web.UI.Page


#Region "Private Methods"
    Private Sub LoadRole()
        gvRole.DataSource = Roles.GetAllRoles
        gvRole.DataBind()
    End Sub
#End Region

#Region "Event Handles"
	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then
            LoadRole()
        End If
	End Sub

    Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnSubmit.Click
        Roles.CreateRole(txtRole.Text.Trim)
        txtRole.Text = Nothing
        LoadRole()
    End Sub

    Private Sub gvRole_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvRole.RowCommand
        Select Case e.CommandName
            Case "ManageRole"
                labErrMsg.Text = ""
                Response.Redirect("~/Admin/Roles/EditRole.aspx?r=" & Server.UrlEncode(e.CommandArgument.ToString))
            Case "DeleteRole"
                If Roles.RoleExists(e.CommandArgument.ToString) Then
                    Try
                        dsAdminTabPermission.DeleteAdminTabPermissionByRoleName(e.CommandArgument.ToString)
                        dsAdminPermission.DeleteAdminPermissionByRoleName(e.CommandArgument.ToString)
                        Roles.DeleteRole(e.CommandArgument.ToString)
                    Catch ex As Exception
                        labErrMsg.Text = "此群組還有使用者在!請先移除…"
                    End Try
                End If
            Case Else
        End Select
        LoadRole()
	End Sub

	Private Sub gvRole_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvRole.RowDataBound
          If e.Row.RowType = DataControlRowType.DataRow Then
            '可加入控制權限判斷
            CType(e.Row.FindControl("lbtnManage"), LinkButton).Enabled = True
         
            '可加入控制權限判斷
            CType(e.Row.FindControl("lbtnDelete"), LinkButton).Enabled = True
           
           End If
	End Sub

#End Region


End Class