.net给用户分配角色

来源:百度知道 编辑:UC知道 时间:2024/05/28 06:03:04
protected void Page_Load(object sender, EventArgs e)
{

TextBox1.Text = "";
string[] rolesArray;
MembershipUserCollection users;
if (!IsPostBack)
{
users = Membership.GetAllUsers();
DropDownList1.DataSource = users;
DropDownList1.DataBind();

rolesArray = Roles.GetAllRoles();
DropDownList2.DataSource = rolesArray;
DropDownList2.DataBind();
}
}

protected void Button1_Click(object sender, EventArgs e)
{
if (DropDownList1.SelectedItem == null)
{
TextBox1.Text = "请选择用户";
return;
}
if (DropDownList2.SelectedItem == null)
{
TextBox1.Text = "请选择角色";
return;
}
try
{

错误提示已经说清楚了。
public static void AddUsersToRole(
string[] usernames,
string roleName
)

DropDownList1.SelectedItem.ToString() 这个返回一个String, 而不是System.Web.Security.Roles.AddUsersToRole(string[], string)中第一个参数的数组。

下面的例子你看下就明白了
-------------------------------
string[] rolesArray;
MembershipUserCollection users;
string[] usersInRole;

public void Page_Load()
{
Msg.Text = "";

if (!IsPostBack)
{
// Bind roles to ListBox.

rolesArray = Roles.GetAllRoles();
RolesListBox.DataSource = rolesArray;
RolesListBox.DataBind();

// Bind users to ListBox.

users = Membership.GetAllUsers();
UsersListBox.DataSource = users;
UsersListBox.DataBind();
}

if (RolesListBox.SelectedItem != null)
{
// Show users in role. Bind user list to G