L21 - 5. Web Session States and files

L21 - 5. Web Session States and files - Y. Chen Session...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Y. Chen Session State Both View State and Cookie can store string data only; Session State allows you to store structured objects in the server; The scope is within all pages of the session, but not cross sessions; The syntax is similar to View State and Cookie The information in session state is secure The information is linked to the session. Other sessions of the same application cannot access the information; A unique 120-bit number is generated to identify the session; 1 Y. Chen Use Session State To Store Objects 2 http://asusrl.eas.asu.edu/WSRepository/SessionStateApp/Default.aspx Y. Chen Seller Page for Entering Information 3 Seller.aspx Y. Chen Returns to Default Page 4 Y. Chen public partial class _Default : System.Web.UI.Page { Book aBook1, aBook2, aBook3; string indexKey; protected void Page_Load (object sender, EventArgs e) { if (( Session.Count != 0 ) && ( ListBox1.Items.Count == 0 )) { Book aBook1 = (Book)Session["sBook1"]; ListBox1.Items. Add (aBook1._Title); Book aBook2 = (Book)Session["sBook2"]; ListBox1.Items. Add (aBook2._Title); Book aBook3 = (Book)Session["sBook3"]; ListBox1.Items. Add (aBook3._Title); } } // Continued next page There is information available in session state The ListBox is empty Default.aspx.cs 5 Code to be executed every time the page is loaded/reloaded. Y. Chen Default.aspx.cs (Contd.) protected void btnSeller_Click(object sender, EventArgs e) { Response.Redirect("Seller.aspx"); } 6 Jump to Seller page protected void btnViewBook_Click(object sender, EventArgs e) { if (ListBoxCatelog.SelectedIndex < 0 ) lblTitle.Text = "Please select a book in the list above"; else { string num = Convert.ToString(ListBoxCatelog.SelectedIndex + 1); indexKey = "sBook" + num; // Find selected book Book aBook = (Book)Session[indexKey]; lblTitle.Text = "<br />Title: " + aBook._Title; lblIsbn.Text = "<br />ISBN: " + aBook._Isbn; lblPrice.Text = "<br />Price: " + aBook._Price; } } View Book Detail Button No item selected Y. Chen Default.aspx.cs: Add to Cart Button protected void btnAddToCart_Click(object sender, EventArgs e) { string num = Convert.ToString(ListBoxCatelog.SelectedIndex + 1); indexKey = "sBook" + num; // Find selected book Book sBook = (Book)Session[indexKey]; // read from state variable sBook._InCart = true; // add information Session[indexKey] = sBook; // Write back Response.Redirect("MyCart.aspx"); } 7 Jump to MyCart page Y. Chen Default.aspx.cs -- Book Class Definition public class Book public string _Title; public string _Isbn; public double _Price; public bool _InCart; public Book(string title, string isbn, double price){ _Title = title; _Isbn = isbn; _Price = price; _InCart = false; } } 8 The constructor loads the parameter values into the class variables Y. Chen Seller.aspx.cs public partial class Seller : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { string title = txtTitle.Text; string isbn = txtIsbn.Text;string isbn = txtIsbn....
View Full Document

This note was uploaded on 05/12/2010 for the course CSE 41412 taught by Professor Hassan during the Fall '10 term at University of Arizona- Tucson.

Page1 / 30

L21 - 5. Web Session States and files - Y. Chen Session...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online