1 / 30

ASP.NET Part II Dr. Awad Khalil Computer Science Department AUC

ASP.NET Part II Dr. Awad Khalil Computer Science Department AUC. ASP.NET. Content Introduction .NET Overview .NET Framework ASP (Active Server Pages) .NET Setup JScript .NET A Simple ASP.NET Example Web Forms Session Tracking Cookies

lhoward
Download Presentation

ASP.NET Part II Dr. Awad Khalil Computer Science Department AUC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ASP.NET Part II Dr. Awad Khalil Computer Science Department AUC

  2. ASP.NET Content Introduction .NET Overview .NET Framework ASP (Active Server Pages) .NET Setup JScript .NET A Simple ASP.NET Example Web Forms Session Tracking Cookies Session Tracking with HttpSessionState ASP.NET and XML Reading and Writing Text Files Connecting to a Database in ASP.NET Code-Behind Approach ASP.NET Web Services Web Resources

  3. Objectives • In this topic, you will learn: • To program ASP.NET pages using JScript .NET. • To understand how ASP.NET pages work. • To understand the differences between client-side scripting and server-side scripting. • To create Web services. • To use and manipulate XML files with ASP.NET. • To understand Web forms and code-behind files. • To be able to use session tracking in an ASP.NET application. • To use ActiveX Data Objects .NET (ADO.NET) to access a database.

  4. Session Tracking • Personalization • Protection of privacy • Cookies • .NET’s HttpSessionState object • Use of input form elements of type “hidden” • URL rewriting

  5. Personalization & Privacy • Personalization makes it possible for e-business to communicate effectively with their customers and also improve user’s ability to locate desired products and services. • e-business began to establish mechanisms by which they could personalize user’s browsing experience, tailoring content to individual users while enabling them to bypass irrelevant information. • Business achieve this level of service by tracking each customer’s movement through the Internet and combining the collected data with information provided by the customer, including billing information, personal preferences, interests and hobbies. • A trade-off exists, however, between personalized e-business service and protection of Privacy. • Whereas some consumers embrace the idea of tailored content, others fear that the tracking technologies will have adverse consequences on their lives.

  6. Techniques of Session Tracking • To provide personalized service to consumers, e-business must be able to recognize clients when they request information from a site. The request/response model on which the Web operates is facilitated by HTTP. Unfortunately, HTTP is a stateless protocol – it does not support persistent connections that would enable Web servers to maintain state information regarding clients. • To help the server to distinguish among clients, a unique session ID is assigned to each client on the Internet. • The tracking of individual clients , known as session tracking, can be achieved in a number of ways: • Cookies, • .NET HttpSessionState, • Use of input form elements of type “hidden” • URL rewriting

  7. Cookies • A popular way to customize interactions with Web pages is via cookies. • A cookie is a text file stored by a Web site on an individual’s computer that allows the site to track the individual’s actions. • The user’s computer receives a cookie the first time the user visits the Web site; this cookie is then reactivated each time the user revisits the site. • The collected information is intended to be anonymous record containing data that are used to personalize the user’s future visits to the site. • In addition to identifying users, cookies can also indicate their shopping preferences. When a Web Form receives a request from a client, the Web Form could examine the cookie(s) it sent to the client during previous communications, identify the client’s preferences and immediately display products that are of interest to the client.

  8. Cookies • Every HTTP-based interaction between a client and a server includes a header containing information either about the request (when the communication is from the client to the server) or about the response (when the communication is from the server to the client). • When a WebForm receives a request, the header includes information such as the request type (e.g., GET) and any cookies that have been sent previously from the server to be stored on the client machine. When the server formulates its response, the header information includes any cookies the server wants to store on the client computer and other information, such as the MIME type of the response. • If the programmer of a cookie does not set an expiration date, the Web browser maintains the cookie for the duration of the browsing session. Otherwise, the Web browser maintains the cookie until the expiration date occurs. • When the browser requests a resource from a Web server, cookies previously sent to the client by that Web server are returned to the Web server as part of the request formulated by the browser. • Cookies are deleted when they expire. The expiration date of a cookie can be set in the cookie’s Expires property.

  9. HttpCookie Properties

  10. cookie.aspx(1 of 2)

  11. cookie.aspx(2 of 2)

  12. Session Tracking with HttpSessionState

  13. Session Tracking with HttpSessionState • The next Web application demonstrates session tracking using the Class Library’s HttpSessionState. • Every Web Form includes an HttpSessionState object, which is accessible through property Session of class Page. • The example contains two pages. • In the first page, users select their favorite programming language from a group of radio buttons, then submit the HTML form to the Web server for processing. • Property Session: to manipulate the page HttpSessionState object. When the Web page is requested, an HttpSessionState object is created and assigned to the Page’sSession property. We often refer to property Session as the Session object. • The Web page then allows the user to select another programming language or to view the second page in the application, which lists recommended books pertaining to the programming language that the user selected previously. • When the user clicks the hyperlink, the HttpSessionState previously stored on the client is read and used to form the list of book recommendations.

  14. Session Tracking with HttpSessionState • Line 2 of the first page imports the namespace System. Classes of related components provided by the .NET Framework are organized and grouped into namespaces. • An Import statement makes .NET classes readily available by allowing programmer to use a class name without specifying the namespace in front of it. To import namespaces in an .aspx file, we use the @Import directive with an attribute Namespace to specify the namespace to import. • The .aspx file contains four radio buttons (lines 95-100) with values C#, C++, C, and Python. The user selects a programming language by clicking one of the radio buttons. • The page also contains a Submit button, which, when clicked, calls submitButton_Click event handler (lines 62-79) which stores the user selections in the Session object. • Like a cookie, an HttpSessionState object stores name-value pairs. In session terminology, these are called sessionitems, and they are placed in HttpSessionState object by calling method Add. Line 77 calls Add to place the language and its coressponding recommended book’s ISBN number into the HttpSessionState object.

  15. Session Tracking with HttpSessionState • One of the primary benefits of using HttpSessionState objects (rather than cookies) is that HttpSessionState objects can store objects of any type (not just strings) as attribute values. • After values are added to the HttpSessionState object, the application handles the postback (lines 28-57) in method Page_Load. Here we retrieve information about the current client\s session from the Session object’s properties and display this information in the Web page. • Property SessionID (line 53) contains the session’suniqueID. The first time a client connects to the Web server, a unique session ID is created for that client. When the client makes additional requests, the client’s session ID is compared with the session IDs in the Web server memory. • Property Timeout (line 56) specifies the maximum amount of time that an HttpSessionState object can be inactive before it is discarded.

  16. Session Tracking with HttpSessionState • When the postback occurs, certain components are revealed (i.e., their Visible property is set to true), whereas others become hidden (i.e., their Visible property is set to false). • The welcome message is displayed (line 32), and two hyperlinks located toward the button of the page become visible (line 33-34). One link requests the current page (lines 119-122), and one requests recommendations.aspx (lines 125-129). • Note that clicking the first hyperlink (i.e., the one that requests the current page) does not cause a postback to occur. The file optionsPage.aspx is specified in the NavigateUrl property of the hyperlink. When the hyperlink is clicked, this page is requested as a completely new request to allow the user to select a new programming language. • After the postback, the form labels and elements are hidden (lines 37-39).

  17. Session Tracking with HttpSessionState • Line 17 defines books as a Hashtable, which is a data structure that stores key-value pairs. • A hash table is like an associative array in which keys are mapped to array positions that store values. The program uses the key to store and retrieve the associated value in the hash table. In this example, the keys are strings that contain the programming language names, and the values are strings that contain the ISBN numbers for the recommended books. • Class Hashtable provides method Add, which takes as arguments a key and a value. The value for a specific hash table entry can be obtained by indexing the hash table with the value’s key. For instance: books[ language]; in line 72 returns the value from the key-value pair in which language is a key in the books hash table. • Lines 23-26 add four programming languages and their respective ISBNs to the Hashtable.

  18. Session Tracking with HttpSessionState • Clicking the Submit button causes a postback to occur. As a result, the condition in the if statement (line 28) of the Page_load event handler evaluates to true, and lines 32-56 execute. • Line 42 determines whether the user selected a language. If so, that language is displayed in welcomeLabel (line 49). • Additionally, clicking Submit causes submitButton_Click (lines 62-79) to be called. If the user selects a language, event handler submitButton_Click adds a key-value pair to the Session object specifying the language chosen and the ISBN number for a book on that language. • Next, a postback occurs. • Each time the user clicks Submit, submitButton_Click adds a new language-ISBN pair to the HttpSessionState object. • The user is then provided two links. The user can choose another language (i.e., return to the first page with the radio buttons) or link to recommendationsPage.aspx, which displays a list of book recommendations on the basis of the user’s language selections.

  19. Session Tracking with HttpSessionState • The file recommendationsPage.aspx contains a label (lines 44-47) and a list box (lines 49-51). • The label displays the text Recommendations if the user has selected one or more languages; otherwise it displays NORecommendations. • The ListBox Web control displays the recommendations created by the ASP.NET page’s script. • Evend handler OnInit (lines 15-38) retrieves the session information. OnInit is an event handler that is called when a page is initialized. If a user has never selected a languages during any visit to this site, the Session object’s Count property will be zero. This property indicates the number of session items contained in a Session object. If the Session object is null (i.e., no language has ever been selected), then we display the text NoRecommendations.

  20. Session Tracking with HttpSessionState • If the user has chosen a language, the for statement (lines 22-31) iterates through the Session object. The value in a key-value pair is retrieved from the Session object by indexing the Session object with the key name, using the same process by which we retrieved a value from our hash table. • We access the Keys property of class HttpSessionState (line 25), which returns an object that contains all the keys in the session. This line indexes our object (as if it were an array) using the current for loop’s control variable to retrieve the current key. • Lines 29-30 add to ListBox a string formed by combining the KeyName’s value, the string “HowtoProgram. ISBN#: “ and the value from the Session object for which KeyName is the key.

  21. optionsPage.aspx(1 of 6)

  22. optionsPage.aspx(2 of 6)

  23. optionsPage.aspx(3 of 6)

  24. optionsPage.aspx(4 of 6)

  25. optionsPage.aspx(5 of 6)

  26. optionsPage.aspx(6 of 6)

  27. recommendationsPage.aspx(1 of 3)

  28. recommendationsPage.aspx(2 of 3)

  29. recommendationsPage.aspx(3 of 3)

More Related