Monday, August 23, 2010

AddUserToRole method example: programmatically adding user to role in asp.net



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>


Data Transfer Using Cookies Session Variables Data Transfer Using Cookies Session Variables in ASP.NET C# VB.NET

Data Transfer Using Cookies

Cookies are stored in client machine and can have max size of 4kb
To store value from textbox.

protected void btnGo_Click(object sender, EventArgs e)
{
HttpCookie MyCookie = new HttpCookie("Data");
MyCookie.Value = txtData.Text;
MyCookie.Expires = DateTime.Now.AddDays(1);
Response.Cookies.Add(MyCookie);
Response.Redirect("Deault2.aspx");
}
And to retrieve data from cookie in Deault2.aspx page
In the Page_Load event write
String strData = Request.Cookies["Data"].Value);

Data Transfer using Session Variables

Storing and retrieving Data to and from
session variables on the page we want.

To store a value in session we need to write
protected void btnGo_Click(object sender, EventArgs e)
{
Session["VariableName"] = txtData.Text;
Response.Redirect("Deault2.aspx");
}
To retrieve value from Session on other page
string strData = Session["VariableName"].ToString();
Don't forget to use proper typecasting as session stores data as Object and we need
to change it to desired type
To remove Session Variables use
Session.Contents.Remove("VariableName"); or
Session.Contents.RemoveAll(); or
Session.Abandon();