ASP.NET Lecture 2

7,011 views

Published on

This lecture presents:
User Controls
Validation Controls
State Management

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
7,011
On SlideShare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Standardize repeated content across all pages, reuse header, footer and navigation controls
  • TestControl pageTryUserControl pageDateTimeControl
  • Validation page
  • CounterNoViewState pageCounterViewState page
  • Deserialization happens before Page.LoadCost  enlarged page size, slower transmission times!!
  • CrossPagePosting pageCrossPagePosting2 page
  • Cookie page
  • 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.
  • Global.asax page
  • ASP.NET Lecture 2

    1. 1. ASP.NET 4.0Julie IskanderMSC. Communication and Electronics
    2. 2. Course Outlines User Controls Validation Controls State Management
    3. 3. User Controls“Always DRY”
    4. 4. 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
    5. 5. 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
    6. 6. Step 1
    7. 7. Step 2
    8. 8. Step 3
    9. 9. Convert from an HTML to an aspx page
    10. 10. Validation Controls
    11. 11. Validation Controls Validates using client-side JavaScript first, if valid posts back and validates using server-side code. Must be bound to an input control CauseValidation property of controls Inherits from BaseValidator Class (System.Web.UI.WebControls)
    12. 12. Validation Controls
    13. 13. Convert from an HTML to an aspx page
    14. 14. Regular Expression
    15. 15. Custom Validator
    16. 16. Custom Validator
    17. 17. Validation Groups
    18. 18. Validation Groups
    19. 19. 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
    20. 20. State Management“The art of retaininginformation betweenrequests”
    21. 21. To Windows Form developers, ASP. NET seems like continuous running applications!!!A Clever ILLUSION
    22. 22. 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
    23. 23. 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
    24. 24. ViewState Collection To save value ◦ this.ViewState[“ButtonClicked”]=1; To retrieve value ◦ int count = (int)this.ViewState[“ButtonClicked”];
    25. 25. Convert from an HTML to an aspx page
    26. 26. 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
    27. 27. 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.
    28. 28. Page1 Page2 In an IButtonControl  Access Page1 through ◦ PostBackUrl Property = “url PreviousPage Property of page2” Cross-Page Posting
    29. 29. Convert from an HTML to an aspx page
    30. 30. 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"];
    31. 31. 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.
    32. 32. Cookies Creating cookies and storing information
    33. 33. Cookies Retrieving information from cookies
    34. 34. Convert from an HTML to an aspx page
    35. 35. 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
    36. 36. 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
    37. 37. Convert from an HTML to an aspx page
    38. 38. Session State
    39. 39. Convert from an HTML to an aspx page
    40. 40. SQL Server Data Store forSessionState Slowest SessionState storage Command to create Database Store Configure the web.config to use it
    41. 41. 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.
    42. 42. Convert from an HTML to an aspx page
    43. 43. ASP.NETApplication andGlobal Events
    44. 44. 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
    45. 45. Application Events PerRequest Events ◦ Application_BeginRequest() ◦ Application_EndRequest() PerSession Events ◦ Session_Start() ◦ Session_End() Special Events ◦ Application_Start() ◦ Application_End() ◦ Application_Error()
    46. 46. Convert from an HTML to an aspx page
    47. 47. ASP.NETConfiguration
    48. 48. ASP.NET Configuration XML-based files Machine level ◦ machine.config  [windows]Microsoft.NetFramework[Version]C onfig ◦ Root web.config All applications inherit these configurations
    49. 49. ASP.NET Configuration Per Application ◦ web.config  In root application directory ◦ web.config in subdirectories
    50. 50. ASP.NET Configuration
    51. 51. 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
    52. 52. Report #2 Show a simple example of creating and retrieving cookies in javascript. How can you load a User Control in code?
    53. 53. 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
    54. 54. Lab Hints Add events to dynamic controls Cast the object sender of the event to the wanted control Use Javascript and/or jQuery for the user control Try Trace=true in Page directive, to help in debugging
    55. 55. 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.
    56. 56. REFERENCES [1] Beginning ASP.NET 4 In C# 2010, Matthew Macdonald, Apress [2] Web Application Architecture Principles, Protocols And Practices, Leon Shklar And Richard Rosen, Wiley [3] Professional AS P.NE T 4 In C# And VB, Bill Evjen, Scott Hanselman And Devin Rader, Wiley [4] Pro ASP.NET In C# 2010, Fourth Edition,matthew Macdonald, Adam Freeman, And Mario Szpuszta, Apress

    ×