日期:2014-05-17 浏览次数:20406 次
using System; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; public partial class _Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { //取所有用户 this.ListBox1.DataSource = Membership.GetAllUsers(); this.ListBox1.DataBind(); //取所有角色 this.CheckBoxList1.DataSource = Roles.GetAllRoles(); this.CheckBoxList1.DataBind(); this.Label1.Text = ""; foreach (string s in Roles.GetRolesForUser()) { Label1.Text += s + ";"; } // this.Label1.Text = Roles.GetRolesForUser().ToString(); } } protected void Button1_Click(object sender, EventArgs e) { //Membership.DeleteUser(username);//删除用户 try { Roles.CreateRole(this.TextBox1.Text); Label1.Text = "成功"; } catch (Exception ex) { this.Label1.Text = ex.Message; } } protected void ListBox1_SelectedIndexChanged(object sender, EventArgs e) { //根据用户看他有什么角色 //取当前要查看的用户 string username = this.ListBox1.SelectedItem.Text; //看用户有什么角色 //string[] hasroles = Roles.GetRolesForUser(username); foreach (ListItem li in CheckBoxList1.Items) { //取当前判断的角色 string role = li.Text; //用户是否在这个角色里? if (Roles.IsUserInRole(username, role)) { li.Selected = true; } else { li.Selected = false; } } //显示用户的email MembershipUser user = Membership.GetUser(username); this.TextBox2.Text = user.Email; } protected void CheckBoxList1_SelectedIndexChanged(object sender, EventArgs e) { //取当前用户 string username = this.ListBox1.SelectedItem.Text; foreach (ListItem li in CheckBoxList1.Items) { //取当前判断的角色 string role = li.Text; //是否选中 bool isinrole = li.Selected; if (isinrole) { //添加到角色中 //先判断是否已在角色中 if (Roles.IsUserInRole(username, role)) { } else { Roles.AddUserToRole(username, role); } } else { if (Roles.IsUserInRole(username, role)) { Roles.RemoveUserFromRole(username, role); } } } } }
------解决方案--------------------
http://cache.baidu.com/c?m=9d78d513d99c16f008bad43f574d9571182497153ac0a01368a5935fe4144f060738ece161645213d2b6617a44ea0c4bea873171644262fd8cc8ff1b80e48f4e7cdd662973168c5c&p=882a921893904eaa40b6cf391b&user=baidu
看看这个!~
------解决方案--------------------
http://www.51aspx.com/Tags/188/