角色成員管理(1)
這一節(jié)要討論的是關(guān)于角色成員的設(shè)置與會(huì)員數(shù)據(jù)查看的相關(guān)網(wǎng)頁。首先要來看的是Profile文件夾里面的兩個(gè)網(wǎng)頁文件:RoleM.aspx與RoleMemberM.aspx。這兩個(gè)網(wǎng)頁在前一章討論角色功能設(shè)置的過程中,已經(jīng)完成了示例的實(shí)現(xiàn),這里的網(wǎng)頁直接移植其中的功能。先來看看角色管理功能網(wǎng)頁RoleM.aspx,配置如下圖所示:
這個(gè)網(wǎng)頁的內(nèi)容相信讀者已經(jīng)有了概念,我們直接來看看它的后置程序代碼,如下圖所示:
public partial class Admin_Profile_RoleM : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { this.DisplayRoles(); } protected void CreateRoleButton_Click(object sender, EventArgs e) { String roleName = RoleTextBox.Text; if (roleName.Length > 0) { Roles.CreateRole(roleName); } this.DisplayRoles(); } private void DisplayRoles() { string[] roleNames = Roles.GetAllRoles(); RolesListBox.DataSource = roleNames; RolesListBox.DataBind(); } protected void DeleteRoleButton_Click(object sender, EventArgs e) { String roleName = RoleTextBox.Text ; if (roleName.Length > 0) { Roles.DeleteRole(roleName); } this.DisplayRoles(); } } | 其中的DisplayRoles方法取得所有的角色,并且將其綁定到頁面上的控件中,另外兩個(gè)按鈕的Click事件處理程序分別引用CreateRole與DeleteRole方法,執(zhí)行角色的刪除及建立工作。另外一個(gè)網(wǎng)頁RoleMemberM.aspx提供會(huì)員與角色的管理功能,同樣,這個(gè)網(wǎng)頁在我們前一章討論相關(guān)功能時(shí)候,已經(jīng)針對(duì)其內(nèi)容進(jìn)行了探討,為了令其功能更加完整,這里添加了一個(gè)GridView控件,用來顯示所有的會(huì)員,網(wǎng)頁配置如下圖所示:
角色成員管理(2)
除了GridView之外,頁面上方的清單與文本框字段分別顯示所有的角色及特定成員所屬的角色。切換到后置程序代碼,其中建立了一個(gè)內(nèi)部類,這個(gè)類的實(shí)體用來儲(chǔ)存會(huì)員的數(shù)據(jù)內(nèi)容,代碼如下:
public partial class Admin_Profile_RoleMemberM : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.DisplayRoles(); UserGridView.DataSource = this.AllUserAL(); UserGridView.DataBind(); } } private void DisplayRoles() { string[] roleNames = Roles.GetAllRoles(); RolesListBox.DataSource = roleNames; RolesListBox.DataBind(); if (roleNames.Length > 0) { RolesListBox.SelectedIndex = 0; this.DisplayUsers(roleNames[0]); } } private void DisplayUsers(string roleName) { string[] users = Roles.GetUsersInRole(roleName); UsersListBox.DataSource = users; UsersListBox.DataBind(); } protected void CreateRoleButton_Click(object sender, EventArgs e) { if (RolesListBox.SelectedItem != null) { if (UserTextBox.Text.Length > 0) { if (CheckUser(UserTextBox.Text)) { Roles.AddUserToRole(UserTextBox.Text, RolesListBox.SelectedItem.Value); DisplayUsers(RolesListBox.SelectedItem.Value); } else return; } } } protected void DeleteRoleButton_Click(object sender, EventArgs e) { if (RolesListBox.SelectedItem != null) { if (UserTextBox.Text.Length > 0) { if (CheckUser(UserTextBox.Text)) { Roles.RemoveUserFromRole(UserTextBox.Text, RolesListBox.SelectedItem.Value); DisplayUsers(RolesListBox.SelectedItem.Value); } else return; } } } protected void RolesListBox_SelectedIndexChanged( object sender, EventArgs e) { string roleName = RolesListBox.SelectedItem.Value; this.DisplayUsers(roleName); } private ArrayList AllUserAL() { ArrayList al = new ArrayList(); MembershipUserCollection muc = Membership.GetAllUsers(); foreach (MembershipUser mu in muc) { UserInfo ui = new UserInfo(); ui.UserNamep = mu.UserName ; ui.UserEMailp = mu.Email; ui.CreationDatep = mu.CreationDate; al.Add(ui); } return al; } private class UserInfo { private string userName = ""; private string userEMail = ""; private DateTime creationDate = new DateTime() ; public string UserEMailp { get { return userEMail; } set { userEMail = value; } } public string UserNamep { get { return userName; } set { userName = value; } } public DateTime CreationDatep { get { return creationDate; } set { creationDate = value; } } } private bool CheckUser(string userName) { MembershipUserCollection muc = Membership.FindUsersByName(userName); if (muc.Count > 0) return true; else return false; } } |
陰影區(qū)域的代碼為UserInfo類的相關(guān)代碼,這個(gè)類定義了用來儲(chǔ)存會(huì)員數(shù)據(jù)的相關(guān)屬性,分別是會(huì)員名稱、電子郵件及建立日期等。AllUserAL方法首先引用Membership類的GetAllUsers方法成員,取得所有的會(huì)員數(shù)據(jù),然后通過一個(gè)循環(huán),逐一建立UserInfo類實(shí)體,并且將會(huì)員的數(shù)據(jù)儲(chǔ)存到類對(duì)象的屬性中,所有的UserInfo最后均被儲(chǔ)存到ArrayList類的對(duì)象集合中。這個(gè)集合在網(wǎng)頁加載時(shí),在Page_Load中被指定給頁面上的GridView控件,發(fā)布會(huì)員的數(shù)據(jù)內(nèi)容。
除了會(huì)員數(shù)據(jù)以外,其他的方法函數(shù)則響應(yīng)頁面上按鈕的Click事件,這些內(nèi)容在前面的章節(jié)已做了說明。另外,最下方的CheckUser函數(shù)查看指定的會(huì)員是否存在,回傳一個(gè)布爾類型的結(jié)果值,這個(gè)方法由相關(guān)的事件處理程序調(diào)用,避免用戶隨意指定不存在的會(huì)員
|