AddUserToRoleExample.aspx
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void Page_Load(object sender, System.EventArgs e) {
if(!Page.IsPostBack){
ListBox1.DataSource = Roles.GetAllRoles();
ListBox1.DataBind();
ListBox2.DataSource = Membership.GetAllUsers();
ListBox2.DataBind();
}
}
protected void ListBox1_SelectedIndexChanged(object sender, System.EventArgs e) {
ListBox3.DataSource = Roles.GetUsersInRole(ListBox1.SelectedItem.Text.ToString());
ListBox3.DataBind();
}
protected void Button1_Click(object sender, System.EventArgs e) {
Roles.AddUserToRole(ListBox2.SelectedItem.Text.ToString(), ListBox1.SelectedItem.Text.ToString());
ListBox3.DataSource = Roles.GetUsersInRole(ListBox1.SelectedItem.Text.ToString());
ListBox3.DataBind();
Label1.Text = ListBox2.SelectedItem.Text.ToString() +
" User Added in " +
ListBox1.SelectedItem.Text.ToString() +
" Role successfully!";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>AddUserToRole method example: programmatically adding user to role in asp.net</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>Add user to role example</h2>
<asp:Label ID="Label1" runat="server" Font-Size="Larger" ForeColor="Crimson"></asp:Label>
<br /><br />
<b>All Roles</b>
<br />
<asp:ListBox ID="ListBox1" runat="server" OnSelectedIndexChanged="ListBox1_SelectedIndexChanged" AutoPostBack="true"></asp:ListBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="ListBox1" Text="*"></asp:RequiredFieldValidator>
<br /><br />
<b>All Users</b>
<br />
<asp:ListBox ID="ListBox2" runat="server"></asp:ListBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="ListBox2" Text="*"></asp:RequiredFieldValidator>
<hr />
<b style="color:Fuchsia">All users in selected role</b>
<br />
<asp:ListBox ID="ListBox3" runat="server"></asp:ListBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Add selected user in selected role" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
No comments:
Post a Comment