Session-Cookie Handout

advertisement
Session Example:
Default.aspx
using
using
using
using
using
using
using
System;
System.Data;
System.Configuration;
System.Web;
System.Web.Security;
System.Web.UI;
System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
// Create the ArrayList to hold the Person objects. It has to be static so it doesn't
// get cleared out when a postback occurs.
static ArrayList personList = new ArrayList();
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// Create a new Person object and populate it with data from the Textboxes
Person p = new Person();
p.FirstName = TextBox1.Text;
p.LastName = TextBox2.Text;
// Add the Person p to the personList ArrayList
personList.Add(p);
// Also add it to the ListBox - for display purposes only. The session
// variable will be based on the ArrayList (personList)
ListBox1.Items.Add(new ListItem(TextBox2.Text + ", " + TextBox1.Text));
}
protected void Button2_Click(object sender, EventArgs e)
{
// Add the ArrayList (personList) as an item to the Session state variable
Session.Add("PersonList", personList);
// Add the number of items in personList as another item to the Session state variable
Session.Add("Count", personList.Count);
// Load the printList.aspx page
// (You can also redirect to printList2.aspx, which does the same thing only with
// better formatting.)
Response.Redirect("printList.aspx");
}
}
printList.aspx
using
using
using
using
using
using
using
using
using
using
System;
System.Data;
System.Configuration;
System.Collections;
System.Web;
System.Web.Security;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.WebControls.WebParts;
System.Web.UI.HtmlControls;
public partial class printList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Retrieve the list of Persons and the count from the Session variable
// This Session variable has two items: PersonList and Count
// PersonList is an ArrayList object, Count is an int: both come from the previous page
ArrayList list = (ArrayList)Session["PersonList"];
int count = (int)Session["Count"];
string output = "";
// Loop through the ArrayList of Persons
for (int i = 0; i < count; i++)
{
// Retrieve the next object from the ArrayList and cast it as a Person
Person p = (Person)list[i];
// Construct the output string for a Person
string lineOutput = "Customer " + (i+1).ToString() + ": " +
p.FirstName + " " + p.LastName + "<BR>";
// Add that line of output to the total output
output = output + lineOutput;
}
Label1.Text = output;
}
}
Cookie Example:
Default.aspx
using
using
using
using
using
using
using
using
using
System;
System.Data;
System.Configuration;
System.Web;
System.Web.Security;
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.WebControls.WebParts;
System.Web.UI.HtmlControls;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
// Create the ArrayList to hold the Person objects. It has to be static so it doesn't
// get cleared out when a postback occurs.
static ArrayList personList = new ArrayList();
// Declare an HttpCookie object
HttpCookie cookie;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
// Create a new Person object and populate it with data from the Textboxes
Person p = new Person();
p.FirstName = TextBox1.Text;
p.LastName = TextBox2.Text;
// Add the Person p to the personList ArrayList
personList.Add(p);
// Also add it to the ListBox - for display purposes only. The session
// variable will be based on the ArrayList (personList)
ListBox1.Items.Add(new ListItem(TextBox2.Text + ", " + TextBox1.Text));
}
protected void Button2_Click(object sender, EventArgs e)
{
// A cookie can only have a string value, so we need to convert the ArrayList to a
// string. This will hold that string, which will be a delimited list of names.
string cookievalue = "";
// Loop through the ArrayList personList
for(int i = 0; i < personList.Count; i++)
{
// Build the string by adding on each additional person:
// Format: FirstName1 LastName1, FirstName2 LastName2, FirstName3 LastName3, etc.
// Note: There will be an extra comma at the end. Don't worry about it.
cookievalue = cookievalue +
((Person)personList[i]).FirstName + " " + ((Person)personList[i]).LastName + ",";
}
// The fun begins. Create the cookie.
cookie = new HttpCookie("PersonListCookie");
// Set the value of the cookie equal to the concatenated string
cookie.Value = cookievalue;
// Set the expiration date to one year from now
cookie.Expires = DateTime.Now.AddYears(1);
// Add the cookie to the collection of cookies and write it out to the hard disk
Response.Cookies.Add(cookie);
// Load the printList.aspx page
// (You can also redirect to printList2.aspx, which does the same thing only with
// better formatting.)
Response.Redirect("printList.aspx");
}
}
printList.aspx
using
using
using
using
using
using
System;
System.Data;
System.Configuration;
System.Collections;
System.Web;
System.Web.Security;
using
using
using
using
System.Web.UI;
System.Web.UI.WebControls;
System.Web.UI.WebControls.WebParts;
System.Web.UI.HtmlControls;
public partial class printList : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Create an ArrayList that will be populate by the data from the cookie
ArrayList personList = new ArrayList();
// Go get the cookie. I hope it's there...
HttpCookie cookie = Request.Cookies["PersonListCookie"];
// Get the value (string) associated with that cookie
string cookievalue = cookie.Value;
// Create an array of strings by splitting the cookie's value based on the comma
// So: FirstName1 LastName1, FirstName2 LastName2, FirstName3 LastName3, etc. will be
// names[0] = FirstName1 LastName1
// names[1] = FirstName2 LastName2
// names[2] = FirstName3 LastName3
// names[3] = blank (the extra comma fools it into thinking there is a fourth item)
string[] names = cookievalue.Split(',');
// Loop through the list of names, but stop at the blank one (n-1)
for (int x = 0; x < names.Length-1; x++)
{
// Create another array, splitting each element in names into separate strings
// So if names[0] = FirstName1 LastName1, then
// singlename[0] = FirstName1
// singlename[1] = LastName1
string[] singlename = names[x].Split(' ');
// Now we create our Person object and populate it with the first name and last name
Person p = new Person();
p.FirstName = singlename[0];
p.LastName = singlename[1];
// The add it to the ArrayList
personList.Add(p);
}
string output = "";
for (int i = 0; i < personList.Count; i++)
{
// Get the next item from the personList ArrayList and cast it as a Person
Person p = (Person)personList[i];
// Construct the output string for a Person
string lineOutput = "Customer " + (i + 1).ToString() + ": " +
p.FirstName + " " + p.LastName + "<BR>";
// Add that line of output to the total output
output = output + lineOutput;
}
Label1.Text = output;
}
}
Download