SessionID (unique 120-bit identifier)Everytime you make a request you get a new sessionID. It is not saved until you store some information in the session collection.(slight performance enhancement)Common usage in shopping cartsUsing Cookies a cookie named ASP.NET_SessionIdUsing Modified URL for clients that don’t support cookies, session ID inserted in URLhttp://localhost/lect2/(S(bn3gnc55vu3sqffghdf))/Cookieless.aspx
Add a logout button which cancels session using Session.Abandon()
SessionState pageSessionState2 page
State Provider is any class that implements IHttpSessionState interface
Furniture session example
acts as a static variable conceptCan be used in counting number of visitors with the global.asaxHeavy traffic can cause inaccuracy and in consistence of data, using Lock() and UnLock() can solve the problem but cause a slow down Can count number of created sessions or how many requests received
ApplicationState pageCount number of times an operation is executed
ASP.NET creates a pool of application objects (1 to 100 objects, acc. To system and available threads) when the application domain is first loaded and uses one to serve each request. Each request gets exclusive access to one of these objects and when the request ends, the object is reused.
ASP.NET Lecture 2
ASP.NET 4.0Julie IskanderMSC. Communication and Electronics
Course Outlines User Controls Validation Controls State Management
User Controls (.ascx) A small section of page that can include static HTML code, web server controls. No <html>,<head>,<body>,<form> tag Can be reused in multiple pages Can have properties and events Have same Page events, and same Page properties Session, Application,……,etc
User Control Step: 1. Create a .acsx file, add your controls 2. Write your code-behind logic 3. On your .aspx a. Register the user control b. Add it to the design
Validation Group (BONUSQuestion) What if I added a new button with no validation group? How to make sure a control is always validated, regardless of the validation group of the clicked button? 1- The button will validate controls with no validation group only, if none is found the page is postback as valid 2- Create multiple validators for the control, one for each validation group and one with no validation group
State Management“The art of retaininginformation betweenrequests”
To Windows Form developers, ASP. NET seems like continuous running applications!!!A Clever ILLUSION
State Management Solution to stateless nature of HTTP To persist information between requests. Store information over the lifetime of the application Methods used ◦ Cross-Page Posting ◦ View state ◦ Query String ◦ Cookies ◦ Session State
ViewState Uses a hidden field Property of Page object, a dictionary collection of name/value pairs Store information for multiple postback within same page. Stores serializable objects only
ViewState Collection To save value ◦ this.ViewState[“ButtonClicked”]=1; To retrieve value ◦ int count = (int)this.ViewState[“ButtonClicked”];
ViewState To use ViewState to retain member variables. It is recommended to ◦ Save values in Page.PreRender ◦ Retrieve values in Page.Load Preserves the illusions of continous application cost?? Another limitation bound to one specific page
Cross-Page Posting To transfer information from one page to another using postback mechanism Can lead you to create pages that are tightly coupled to one another and difficult to enhance and debug. The page will be posted with all control values, even the hidden viewstate values.
Page1 Page2 In an IButtonControl Access Page1 through ◦ PostBackUrl Property = “url PreviousPage Property of page2” Cross-Page Posting
Query String Is the portion of the URL after the question mark. Limitations: ◦ Strings, which must contain URL-legal characters ONLY. ◦ Information is clearly visible . ◦ Can be modified by the user. ◦ Many browsers impose a limit on the length of a URL (usually from 1 to 2 KB). Must be placed in the URL by you, using a special HyperLink control, or you can use a Response.Redirect() ◦ “MyPage.aspx?ID=3” To retrieve the values sent use QueryString dictionary collection in the Request object, ◦ string ID = Request.QueryString["ID"];
Cookies Small files that are created in the web browser’s memory or the clients hard drive. Stores only strings. Accessible and readable Can be disabled in the browser. System.Net If no expiry date, the cookie is not permenant.
Cookies Creating cookies and storing information
Session State User-specific Heavyweight state management method Support any type of objects Stored in the server memory in a dictionary collection of name/value pair Instance of System.Web.SessionState.HttpSessionState class Adv. never stored on client (Secure) global over the entire application Unique SessionID for each user stored ◦ Using Cookies ◦ Using modified URLs ◦ Only part of the session sent to the client
Session State Session is lost when: 1. Close or restart browser 2. Use a different browser 3. Timeout due to inactivity 4. Call Session.Abandon(); Session.Timeout default 20 minutes Disadvantages Information is stored in the server memory Affect performance with increased usage
SQL Server Data Store forSessionState Slowest SessionState storage Command to create Database Store Configure the web.config to use it
Application State Application object Instance of System.Web.HttpApplicationState class Store global objects that any client can access, Stored in a dictionary of name/value pairs Never timeout, unless the application restarts Support any type of objects Retains information on the server.
Application Events Global.asax ◦ event handling global events events of Application class. ◦ Can access all properties and methods of Application object ◦ No HTML or ASP.NET tags. ◦ ONE AND ONLY ONE for each application
Reading Assignment #2 Read ASP.NET Application Life Cycle Overview for IIS 7.0 http://msdn.microsoft.com/en- us/library/bb470252.aspx#GlobalAsax Custom Validators Rich Controls ◦ Calendars ◦ Sitemaps ◦ menus
Lab #2 Add validations to the registration form, Name(Required) and Age(between 18 and 120) at least. Add site map to the site (Self Study) In book.aspx, ◦ Create a UserControl (DateTimeControl.aspx) to display the current date and time on the top-right of the page. It must be refreshed every second without postback. ◦ The checked books are added to the Shopping Cart when checkout is pressed the chosen books are displayed in a new checkout.aspx page . ◦ Create a user control to count number of visitors and add it to the footer of all pages
Bonus In book.aspx ◦ When next and back is clicked the previously checked books must stay checked so as the user can know what he already chose.
REFERENCES  Beginning ASP.NET 4 In C# 2010, Matthew Macdonald, Apress  Web Application Architecture Principles, Protocols And Practices, Leon Shklar And Richard Rosen, Wiley  Professional AS P.NE T 4 In C# And VB, Bill Evjen, Scott Hanselman And Devin Rader, Wiley  Pro ASP.NET In C# 2010, Fourth Edition,matthew Macdonald, Adam Freeman, And Mario Szpuszta, Apress