小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

角色成員管理

 悟靜 2012-02-16

角色成員管理(1)

這一節(jié)要討論的是關(guān)于角色成員的設(shè)置與會員數(shù)據(jù)查看的相關(guān)網(wǎng)頁。首先要來看的是Profile文件夾里面的兩個網(wǎng)頁文件:RoleM.aspx與RoleMemberM.aspx。這兩個網(wǎng)頁在前一章討論角色功能設(shè)置的過程中,已經(jīng)完成了示例的實現(xiàn),這里的網(wǎng)頁直接移植其中的功能。先來看看角色管理功能網(wǎng)頁RoleM.aspx,配置如下圖所示:

 
這個網(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方法取得所有的角色,并且將其綁定到頁面上的控件中,另外兩個按鈕的Click事件處理程序分別引用CreateRole與DeleteRole方法,執(zhí)行角色的刪除及建立工作。另外一個網(wǎng)頁RoleMemberM.aspx提供會員與角色的管理功能,同樣,這個網(wǎng)頁在我們前一章討論相關(guān)功能時候,已經(jīng)針對其內(nèi)容進行了探討,為了令其功能更加完整,這里添加了一個GridView控件,用來顯示所有的會員,網(wǎng)頁配置如下圖所示:
 


角色成員管理(2)

除了GridView之外,頁面上方的清單與文本框字段分別顯示所有的角色及特定成員所屬的角色。切換到后置程序代碼,其中建立了一個內(nèi)部類,這個類的實體用來儲存會員的數(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)代碼,這個類定義了用來儲存會員數(shù)據(jù)的相關(guān)屬性,分別是會員名稱、電子郵件及建立日期等。AllUserAL方法首先引用Membership類的GetAllUsers方法成員,取得所有的會員數(shù)據(jù),然后通過一個循環(huán),逐一建立UserInfo類實體,并且將會員的數(shù)據(jù)儲存到類對象的屬性中,所有的UserInfo最后均被儲存到ArrayList類的對象集合中。這個集合在網(wǎng)頁加載時,在Page_Load中被指定給頁面上的GridView控件,發(fā)布會員的數(shù)據(jù)內(nèi)容。

除了會員數(shù)據(jù)以外,其他的方法函數(shù)則響應(yīng)頁面上按鈕的Click事件,這些內(nèi)容在前面的章節(jié)已做了說明。另外,最下方的CheckUser函數(shù)查看指定的會員是否存在,回傳一個布爾類型的結(jié)果值,這個方法由相關(guān)的事件處理程序調(diào)用,避免用戶隨意指定不存在的會員

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多