Web II - 02 - How ASP.NET Works

11,796 views

Published on

Lecture slides for COMP 3512 (Web 2: Server-Side Development). Suitable for Web 2 course in a degree following CIT/CIS/CS ACM model curriculum.

Published in: Technology
1 Comment
2 Likes
Statistics
Notes
No Downloads
Views
Total views
11,796
On SlideShare
0
From Embeds
0
Number of Embeds
393
Actions
Shares
0
Downloads
184
Comments
1
Likes
2
Embeds 0
No embeds

No notes for slide

Web II - 02 - How ASP.NET Works

  1. 1. HOW ASP.NET WORKS 2
  2. 2. Overview 2  ASP.NET Event Model  ASP.NET Code Compilation  The P  Class Page   ASP.NET Application Lifecycle
  3. 3. Event Model 3  One of the key features of ASP.NET is that it uses an event-based programming model.  In the simple Hello World example, we added a small p p , bit of programming to a method named Page_Load.  This method is an event handler.  An event handler is a method that determines what actions are performed when an event occurs, such as when the user clicks a button or selects an item from a list.  When an event is raised, the handler for that specific event is executed.
  4. 4. Event Handlers 4  In the .NET Framework, all event handlers have a specific method signature, that is, a specific return type and parameters. yp p  Event handlers are always void methods.  Event handlers always accept two parameters:  an object parameter  an EventArgs parameter  (or ( a subclass of EventArgs, such as CommandEventArgs or bl f h d ImageClickEventArgs). protected void Page_Load(object sender, EventArgs e) protected void Page Load(object sender, EventArgs e) { … }
  5. 5. ASP.NET Event System y 5  The event system in ASP.NET operates in a different manner than in a Windows application or from the event system in browser-based Javascript. y p  In a Windows application, for instance, events are raised and handled on the same processor.  In contrast, ASP.NET events are raised on the client (the browser) but transmitted to and handled on the server.  Si Since its event h dli requires a round-trip to the server, i handling i d i h ASP.NET offers a smaller set of events in comparison to a totally client-based event system.
  6. 6. ASP.NET Event System y 6
  7. 7. 7 HTTP P Protocol l A Di Digression i
  8. 8. HTTP 8  Communication protocol for the web  Current version HTTP/1.1
  9. 9. HTTP 9  An HTTP session is a sequence of network request- q q response transactions.  An HTTP client initiates a request.  Itestablishes a Transmission Control Protocol (TCP) connection to a particular port on a host (typically port 80)  An HTTP server listening o that po wa s for a client's se ve s e g on a port waits o c e s request message.  Upon receiving the request, the server sends back  a status line, such as "HTTP/1 1 200 OK , and line HTTP/1.1 OK"  a message of its own, the body of which is perhaps the requested resource, an error message, or some other information.
  10. 10. HTTP Requests q 10  An HTML page, once received, typically will invoke subsequent HTTP requests for other resources: <img src="someimage.gif" /> g g g / <link href="somestyles.css" /> <script src="somecode.js" /> <embed src= someflash.swf  /> <embed src="someflash swf" />
  11. 11. HTTP Example 1 E l
  12. 12. 1. User makes request q
  13. 13. 2. Browser sends HTTP request to server Request Line GET /comp1274/randyc/lab10done/enter_country.htm HTTP/1.1 Accept: */* Accept‐Language: en‐us,en‐ca;q=0.5 Accept‐Encoding: gzip, deflate Request Header User Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322;  User‐Agent: Mozilla/4 0 (compatible; MSIE 6 0; Windows NT 5 1; SV1;  NET CLR 1 1 4322;  InfoPath.1; .NET CLR 2.0.50727) Host: csweb2.mtroyal.ca Connection: Keep‐Alive Blank line HTTP Request Browser owse Web Server Se ve
  14. 14. 3. Server receives and processes the HTTP request HTTP Request Browser Web Server Retrieve Requested file
  15. 15. 4. Server sends HTTP response back p Response Line + Status Code HTTP/1.1 200 OK Server: Microsoft‐IIS/5.0 Date: Thu, 30 Mar 2006 19:50:54 GMT Content‐Type: text/html Response Header Accept Ranges: bytes Accept‐Ranges: bytes Last‐Modified: Fri, 24 Mar 2006 17:50:50 GMT Content‐Length: 209 Blank line <html><head><title>Enter A Country</title></head> <body> <form method="post" action="form_filter.asp"> Enter Country Search:  y Server Response Content <input type="text" name="search"><p> <input type="submit"> HTTP </form> Response </body></html> Browser
  16. 16. 5. Browser displays response p y p
  17. 17. 6. User submits data
  18. 18. 7. Browser sends HTTP request to server POST /comp1274/randyc/lab10done/form_filter.asp HTTP/1.1 Accept: */* A t  */* Referer: http://csweb2.mtroyal.ca/comp1274/randyc/lab10done/enter_country.htm Accept‐Language: en‐us,en‐ca;q=0.5 Content‐Type: application/x‐www‐form‐urlencoded C t t T   li ti / f l d d Accept‐Encoding: gzip, deflate User‐Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR  ) 1.1.4322; InfoPath.1; .NET CLR 2.0.50727) Host: csweb2.mtroyal.ca Content‐Length: 13 Connection: Keep‐Alive Cache‐Control: no‐cache Search=Canada User entered form data goes here in the http request header (because form method="post") as a series of name=value pairs (called a querystring)
  19. 19. What if method=get rather than post? <form method="get" action="form_filter.asp"> Then the user entered form data is added to the requested URL GET /comp1274/randyc/lab10done/form_filter.asp?Search=Canada HTTP/1.1 Versus <form method="post" action="form_filter.asp"> Then the user entered form data is added to the end of HTTP request header POST /comp1274/randyc/lab10done/form_filter.asp HTTP/1.1 rest of HTTP request header goes here Search=Canada
  20. 20. 8. Server receives and processes the HTTP request HTTP Request Browser Web Server Server processes the request h
  21. 21. 9. Server script generates response sent back to browser HTTP/1.1 200 OK Server: Microsoft‐IIS/5.0 Date: Thu, 30 Mar 2006 19:51:06 GMT Content‐Length: 164 Content‐Type: text/html Cache‐control: private <html><head><title>reading recordsets</title></head> <body> <h2>countries</h2> <table border=1> <tr><td>2</td><td>canada</td></tr> </table> </body></html> /b d /ht l
  22. 22. 10. Browser displays the response p y p
  23. 23. HTTP Example 2 E l
  24. 24. 1. User makes request q
  25. 25. 2. Browser sends HTTP request to server GET /comp1274/randyc/lab10done/data_browser.asp HTTP/1.1 Accept: */* Accept‐Language: en‐us,en‐ca;q=0.5 Accept‐Encoding: gzip, deflate User‐Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR  1.1.4322; InfoPath.1; .NET CLR 2.0.50727) Host: csweb2.mtroyal.ca Connection: Keep‐Alive
  26. 26. 3. Server receives and processes the HTTP request HTTP Request Browser Web Server Server processes the request h
  27. 27. 4. Server script generates response sent back to browser HTTP/1.1 200 OK Server: Microsoft‐IIS/5.0 S  Mi ft IIS/5 0 Date: Thu, 30 Mar 2006 19:51:06 GMT Content‐Length: 1590 Content Type: text/html Content‐Type: text/html Cache‐control: private <html><head><title>Data Browser</title></head> <body> <h1>SELECT * FROM movies WHERE runtime < 90<h1> <table border=1> <tr> <td><b>Title</b></td> <td><b>Release Date</b></td> <td><b>Run Time</b></td>       </tr> ...
  28. 28. 5. Browser displays the response p y p
  29. 29. 6. User makes request (click on a link) q ( )
  30. 30. 7. Browser sends HTTP request to server GET /comp1274/randyc/lab10done/movie.asp?ID=84 HTTP/1.1 Accept: */* Referer: http://csweb2.mtroyal.ca/comp1274/randyc/lab10done/data_browser.asp Accept‐Language: en‐us,en‐ca;q=0.5 Accept‐Encoding: gzip, deflate User‐Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR  1.1.4322; InfoPath.1; .NET CLR 2.0.50727) Host: csweb2.mtroyal.ca y Connection: Keep‐Alive
  31. 31. 8. Server receives request, processes it, and generates response HTTP/1.1 200 OK Server: Microsoft‐IIS/5.0 Date: Thu, 30 Mar 2006 19:51:06 GMT Content‐Length: 585 Content‐Type: text/html /h l Cache‐control: private <html><head><title>nightmare before christmas, the</title></head> <html><head><title>nightmare before christmas  the</title></head> <body> <h1>Nightmare Before Christmas, The</h1> Directed by <b>Henry Selick</b><br/> Released on  12/9/1994<br/> Movie length is 76 minutes <h2>Summary</h2> ...
  32. 32. 9. Browser displays the response p y p
  33. 33. HTTP Example 3 E l
  34. 34. 1. User makes request q
  35. 35. 2. Browser sends HTTP request to server GET /comp1274/randyc/lab10done/does_not_exist.asp HTTP/1.1 Accept: */* Accept‐Language: en‐us,en‐ca;q=0.5 Accept‐Encoding: gzip, deflate User‐Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR  1.1.4322; InfoPath.1; .NET CLR 2.0.50727) Host: csweb2.mtroyal.ca Connection: Keep‐Alive
  36. 36. 3. Server receives and processes the HTTP request HTTP Request Browser Web Server Server processes the request
  37. 37. 4. Server receives request, processes it, and generates response HTTP/1.1 404 Object Not Found Server: Microsoft‐IIS/5.0 i f / Date: Thu, 30 Mar 2006 19:51:06 GMT Content‐Length: 4040 Content Type: text/html Content‐Type: text/html Cache‐control: private <html> ... <title>The page cannot be found</title> ... <body> <table> ... The page you are looking for might have been removed, had its  The page you are looking for might have been removed  had its  ...
  38. 38. 5. Browser displays the response p y p
  39. 39. Examining HTTP g 39 Google Chrome FireFox Firebug Extension
  40. 40. 40 Back ASPNET B k to ASP.NET Digression over … Di i
  41. 41. ASP.NET Event Handling g 41  How then does ASP.NET communicate the triggering of an event on one machine (the client browser’s) to another machine (the server hosting the page)? ( g p g )  Answer 1:  Through HTTP  Answer 2:  Through querystring parameters sent via HTTP  Answer 3:  Through postback
  42. 42. Postback 42  Postback is the process by which the browser posts information back to itself.  That is, p , posts information back to the server by y requesting the same page.  That is, it is simply a HTTP POST request for the same ASP.NET page
  43. 43. Postback 43 <body> EventTest.aspx (on server) <form id= form1  runat= server > <form id="form1" runat="server"> Please enter your name: <asp:TextBox ID="name" runat="server" /><br /> Choose favorite author: <asp:DropDownList ID="myList" runat="server" > <asp:ListItem>Choose an author</asp:ListItem> <asp:ListItem>Atwood</asp:ListItem> <asp:ListItem>Austin</asp:ListItem> <asp:ListItem>Hawthorne</asp:ListItem> <asp:ListItem>Melville</asp:ListItem> </asp:DropDownList><br /> <asp:Button ID="btnEnter" runat="server" Text=“Enter“ OnClick=“btnEnter_Click" /> <p><asp:Label ID="msg1" runat="server" /></p> <p><asp:Label ID "msg1" runat "server" /></p> </form> </body> Result in browser <body> <form name="form1" method="post" action="EventTest.aspx" id="form1"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"  Q: Which page will be value="/wEPDwUJMzU4OTQyMTQyD2QWAgIDD2Q … requested when the user … <input type="submit" name="btnEnter" value="Submit" id="btnEnter" /> presses the submit button? <p><span id="msg1">In Page_Load<br/></span></p> …
  44. 44. Postback 44  Postback in ASP.NET  only occurs within web forms (i.e., within a form element with runat="server"), and  only server control events cause a post back.  Each cycle in which information is displayed then y p y posted back to the server, and then redisplayed again, is sometimes also called a postback round trip.
  45. 45. Postback 45  In other words, it is postback which makes it possible for browser-based events (e.g., clicking a button, choosing from a list) to be handled on the web g ) server.
  46. 46. Postback 46
  47. 47. Event Types yp 47  Two types  Page events  Always y triggered and always in a certain specific order (see gg y p ( Page Lifecycle).  Triggered and handled on the server.  Thus Th no postback involved tb k i l d  Control events  Associated with particular controls and only triggered in certain circumstances.  Requires postback
  48. 48. Postback and Events 48  How does the server “know” that a request is a know postback request (i.e., a request in which a control event will have to be handled)? )  By the presence of the viewstate variable.
  49. 49. HTTP + Postback 49 First request for page (from URL, or link, bookmark, …) 1 GET http://localhost:53372/Chapter2/EventTest.aspx NOTE: since this is the first request, there are no querystring parameters in the request 2 Page executes on the server. HTTP/1.1 200 OK Server: ASP.NET Development Server/9.0.0.0 Date: Tue, 21 Sep 2010 18:52:36 GMT Content‐Length: 2039 C t t th 2039 Content‐Type: text/html <html xmlns="http://www.w3.org/1999/xhtml"> <head><title>Event Test</title></head> <body> <form name="form1" method="post" action="EventTest.aspx" id="form1"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"  value="/wEPDwUJMzU4 … ... The generated HTTP response (above) for request contains hidden viewstate
  50. 50. HTTP + Postback 50 3 GET http://localhost:53372/Chapter2/EventTest.aspx … Second request for page Name=Randy&myList=Melvillee&__VIEWSTATE=/wEPDwUJMzU4… (from user clicking button) NOTE: since this is the postback request, there are querystring parameters in the request header, one of which is the viewstate (as well as user’s form data) user s 4 HTTP/1.1 200 OK Server: ASP.NET Development Server/9.0.0.0 Date: Tue, 21 Sep 2010 18:52:36 GMT Date: Tue  21 Sep 2010 18:52:36 GMT Content‐Length: 2039 Content‐Type: text/html Server can figure out that this is <html xmlns="http://www.w3.org/1999/xhtml"> a postback request from the <head><title>Event Test</title></head> presence of the viewstate iewstate <body> <form name="form1" method="post" action="EventTest.aspx" id="form1"> querystring parameter <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"  value="/wEPDwUJMzU4 … ... HTTP Response for this postback request still contains hidden, but probably modified, viewstate
  51. 51. View State 51  View state is one of the most important features of ASP.NET.  It is a specially encoded string that is used to retain page and form information between requests and is sent to the browser within a hidden HTML <input> element.  All page elements not posted back via the standard HTTP POST mechanism are stored within this string. <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE"  value= /wEPDwUJODExMDE5NzY5D2QWAgIDD2QWAgIBDw8WAh4EVGV4dAUKMDgvMDE value "/wEPDwUJODExMDE5NzY5D2QWAgIDD2QWAgIBDw8WAh4EVGV4dAUKMDgvMDE vMjAwNmRkZDZPhFHJER4chf3nmlgfL+uq4W58" />
  52. 52. View State 52  View state is a mechanism for preserving display state within web forms.  Recall that HTTP is by nature stateless.  This means that after the server responds to a request, it no longer preserves any data used for that request.  Nonetheless, web applications very frequently need to retain state on a page between requests.
  53. 53. View State 53  View state is generated once all the page code has executed but before the response is rendered.  The value of each web server control on the page is serialized into text as a number of Base64- encoded triplets, one of which contains a name- p value pair.  Serialize = convert programming object into some type of text representation.  This view state string is then output to the browser as a hidden <input> element named "__VIEWSTATE".
  54. 54. View State 54  When the form is posted back, ASP.NET receives the view state (since it was contained in a form element), deserializes this information and restores ), the state of all the controls prior to the post.  Deserialize = convert some type of text representation back into a programming object.  ASP.NET updates the state of the controls based on the data that has just been posted back, and then calls the usual page and control event handlers.
  55. 55. View State 55  Since the details of encoding and decoding values from the view state are handled by the ASP.NET runtime, y can g , you generally ignore the view state y g and simply enjoy its benefits.
  56. 56. Viewstate 56  However, sometimes a developer may wish to turn off the view state for a page.  For instance, if a very large data set is being , y g g displayed, the view state will also be quite large, which may significantly lengthen the time it takes the browser to d download and render the page. l d d d h
  57. 57. Large View State g 57 Viewstate = 277K
  58. 58. Turn Off Viewstate 58  If a page is not going to post back to itself, you can improve page performance by disabling the view state for the page within the Page directive. p g g <%@ Page ... EnableViewState="false" %>  You can also disable viewstate on a control-by- control by control basis. <asp:Label runat="server" ID="Label2" ViewStateMode="Disabled" ... />
  59. 59. Disabling Viewstate g 59 viewstate = 0.05 K
  60. 60. Disabling Viewstate g 60  Do remember that viewstate is required for any type of postback (control-event) processing.  Again, you only want to disable it for pages or controls that don’t need it.
  61. 61. Page Life Cycle g y 61  Page and control events occur in a certain order which we can call the page life cycle.  Five general stages:  Page initialization  Loading  Postback control event handling  Renderingg  Unloading
  62. 62. Complete Page Life Cycle p g y 62
  63. 63. Event Handlers 63  Within each of these stages, the ASP.NET page raises events that you can handle in your code.  For most situations, you only need to worry about the Page_Load event and certain control events.
  64. 64. Event Handlers 64  Because page events always happen, you simply need to p g y pp , y py define a page event handler by using the appropriate naming convention:  Page_XXXX Page XXXX where XXXX is the event name  Control events need to be explicitly wired.  i.e., you must explicitly bind the handler method to the event.  This can be done declaratively in the markup (the usual case), <asp: utto <asp:Button id="btnSubmit" runat="server"  d bt Sub t u at se e OnClick="btnSubmit_Click" />  or programmatically in the code (rare). btnSubmit.Click += new EventHandler( this.btnSubmit_Click );
  65. 65. Adding Event Handlers in VS g 65
  66. 66. Detecting Postback g 66  There are times when you may want your page to behave differently the very first time it is requested.  One typical example is that you want to read and yp p y display values from a database in a list only the first time the page is requested.  In subsequent postbacks, the data is preserved by the view state so there is no need to re-read the database.
  67. 67. Detecting Postback g 67  Your page can test if it is being requested for the first time via the IsPostBack property  Thisp p y property is equal to false if the page is being q p g g requested for the first time. protected void Page_Load(object sender, EventArgs e) { … if (! IsPostBack) { // Do something here for very first request } … }
  68. 68. Postback and Non-Postback Controls 68  Button-type controls with Click events always generate an immediate postback to the server.  But not all control events generate an immediate postback.  In fact, most control events by default do not cause a postback.  Some controls—for i S l f instance, a Label control—never can l cause a postback.  Change events also do not generate a postback, by default. d f lt  An example of a change event is selecting an item from a drop- down list or entering text into a text box.
  69. 69. Change Events g 69  You may be able to enable postback for change- change type events by setting the control’s AutoPostBack p p y property to true.  e.g., you could change the previous example so that the DropDownList control automatically causes a postback.  By doing so, you could eliminate the button completely and instead do your message processing in the event handler for the S l t dI d Ch d event SelectedIndexChanged event.
  70. 70. Without AutoPostBack 70 Notice: label didn’t change didn t Notice: selection event not handled until the button click triggered a postback
  71. 71. With AutoPostBack 71 Notice: label changed because list selection triggered a postback Notice: button click also triggered postback but only button click event handler is executed.
  72. 72. AutoPostBack 72 <fieldset> <legend>Sample Form ‐ Testing Autopostback</legend> <p><label>Address: </label><br />  <asp:TextBox ID="txtAddress" runat="server" /></p> <p><label>Country: </label><br /> <asp:DropDownList ID="drpCountry" runat="server" AutoPostBack="true" onselectedindexchanged="drpCountry_SelectedIndexChanged"> <asp:ListItem>United States</asp:ListItem> <asp:ListItem>Canada</asp:ListItem> </asp:DropDownList></p> <p><label> <asp:Literal ID="litZipOrPostal" runat="server" Text="Zip: " /> </label> <br /> <asp:TextBox ID="txtZipOrPostal" runat="server" /></p> <asp:Button ID "btnSubmit" runat "server" Text "Submit"  ID="btnSubmit" runat="server" Text="Submit"  onclick="btnSubmit_Click" /> </fieldset> protected void Page_Load(object o, EventArgs e) <asp:Label ID="labMessage" runat="server" /> { labMessage.Text = "In page load<br/>"; } protected void drpCountry_SelectedIndexChanged(object o, EventArgs e) t t d  id d C t S l t dI d Ch d( bj t   E tA ) { labMessage.Text += "In country select event handler<br/>"; if (drpCountry.SelectedValue == "Canada") litZipOrPostal.Text = "Postal Code"; else if (drpCountry.SelectedValue == "United States") litZipOrPostal.Text = "Zipcode";   Zipcode ; } protected void btnSubmit_Click(object sender, EventArgs e) { labMessage.Text += "In button event handler<br/>"; }
  73. 73. Visually Detecting Postback y g 73 1 2 Browser will go blank at start of any new HTTP GET or POST request (including a postback request) and/or will be request delay. When testing a web site locally, this can happen 3 Postback request finished rendering in browser. too quickly to notice, but will be noticeable when hosted on a real remote web server (especially a busy one).
  74. 74. Visually Detecting Postback y g 74 Normal postback request always incurs a delay because it is a round trip round-trip: • request (across internet), • execute, (on server), • response (across internet), • render in browser.
  75. 75. Using AJAX g 75  It is possible to asynchronously make a postback request using ASP.NET’s AJAX abilities.  This avoids the blank screen and wait of a full postback p request.  Instead, the browser will make the request “in the background”using asynchronous Javascript (AJAX).
  76. 76. 76 ASP.NET ASPNET AJAX Another Di A h Digression i
  77. 77. ASP.NET AJAX  ASP.NET AJAX adds Asynchronous JavaScript and XML (AJAX) support to ASP.NET.  ASP.NET AJAX was up until the fall of 2006 was known by the code-known of Atlas.
  78. 78. ASP.NET AJAX  AJAX is not a product.  Rather it is the most common name given to a style of web development that makes fuller use of Javascript and the browser Document Object Model (DOM) in the construction of web application user interfaces.
  79. 79. ASP.NET AJAX  AJAX web applications also make use of all the current browsers’ ability to asynchronously request and receive information from the browser independently of the usual page request/page receive/page display cycle.  Thisability to asynchronously communicate with the server makes it possible to create pages that are more responsive and l di i d less disruptive. ti
  80. 80. Normal web application client-server interaction
  81. 81. AJAX web application client-server interaction
  82. 82. ASP.NET AJAX Examples p Here client-side only (no communication with server) Javascript is being used to ih )J i i b i d provide a richer user experience. Here the browser is communicating asynchronously with the server (the loading animation tells the user this) in order to provide a list of relevant search terms. Javascript is being used to make the request and receive the d h data b k in either XML or JSON format. back h f
  83. 83. AJAX  Asynchronous communication is typically achieved in y yp y Javascript using the XMLHttpRequest object (though some sites use hidden IFrame elements instead).  This bj t Thi object was initially offered i I t i iti ll ff d in Internet E l t Explorer 5 as an ActiveX component;  FireFox, Opera, and Safari subsequently have added native support for XMLHttpRequest.  However, since Internet Explorer 5 and 6 XMLHttpRequest support is via an ActiveX component, it is possible for the pp p , p user to disable it via browser settings.  Internet Explorer 7, like the other browsers now supports XMLHttpRequest natively natively.
  84. 84. AJAX  While richer AJAX web applications are a real boon for the user, creating them can be a real nightmare for the developer. g p  Javascript as a language lacks the kind of object- oriented features developers expect.  As well, Javascript can be quite difficult to debug.  As a consequence, there has been a proliferation of frameworks and toolkits to simplify the process of f k d lk lf h f developing AJAX applications.
  85. 85. Why AJAX Applications Are Good y pp  Fewer roundtrips  Less bandwidth  only need to transfer info/markup that needs updating  Less work for the web server  More responsive for the user
  86. 86. Why AJAX Applications Are Bad y pp  Accessibility problems  Often don’t work very well with screen readers.  Can be harder to develop  Can be a nightmare to debug
  87. 87. AJAX at Work 87 Only these areas were “updated” creating the illusion of better 1 responsiveness 2 Notice: no obvious round-trip (though request + execution + response did happen, it just happened p pp , j pp in the background asynchronously).
  88. 88. ASP.NET AJAX Coding g 88 <form id="form1" runat="server"> Ensures appropriate Javascript libraries are sent <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> to browser. <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> Any postback requests within <fieldset> this UpdatePanel will be done <legend>Sample Form ‐ Testing Autopostback</legend> asynchronously. y y <p><label>Address: </label><br />  <asp:TextBox ID="txtAddress" runat="server" /></p> <p><label>Country: </label><br /> <asp:DropDownList ID="drpCountry" runat="server" AutoPostBack="true" onselectedindexchanged="drpCountry_SelectedIndexChanged"> <asp:ListItem>United States</asp:ListItem> p p <asp:ListItem>Canada</asp:ListItem> </asp:DropDownList></p> <p><label><asp:Literal ID="litZipOrPostal" runat="server" Text="Zip: " /></label> <br /> <asp:TextBox ID="txtZipOrPostal" runat="server" /></p> <asp:Button ID="btnSubmit" runat="server" Text="Submit"  onclick="btnSubmit_Click" /> </fieldset> /fi ld t <asp:Label ID="labMessage" runat="server" /> </ContentTemplate> </asp:UpdatePanel> </form>
  89. 89. 89 ASP.NET B hi d h S ASPNET Behind the Scenes Warning: Advanced Stuff Ahead W i Ad d S ff Ah d (you may want to wait until later to cover this)
  90. 90. ASP.NET Behind the Scenes 90  What happens when the browser requests an pp q ASP.NET web page?  Quick Answer  the visual elements of the page are parsed into a class,  this class, along with its code is dynamically compiled (into MSIL) (i t MSIL),  This MSIL is JIT compiled and then executed on the server, ,  Execution produces the HTML and Javascript that is then sent to the browser.
  91. 91. ASP.NET 2.0 Compilation Process p 91
  92. 92. Where is this stuff? 92  The path for the generated class files created from the web forms along with the temporary assemblies is  [.NET System Directory]Temporary ASP.NET Files[virtual  directory][x][y]  where x and y are randomly-generated names.  For instance, the path for the assembly on my development server was C:WINDOWSMicrosoft.NETFrameworkv4.0.30319Temporary ASP.NET Fileschapter27229f9fd8d0746a9.
  93. 93. Parsed Class (created by ASP.NET from web form) 93
  94. 94. Page class g 94  All Web forms ultimately inherit from the Page class, which is defined in the System.Web.UI namespace.
  95. 95. ASP.NET 2.0 Class Inheritance 95
  96. 96. Page class g 96  The Page class inherits from the TemplateControl class, which in turn inherits from the Control class.  As a result, the Page class provides a great deal of functionality exposed as properties and methods that you can make use of in your web forms. y y  Some of these properties are analogous to the intrinsic global objects of ASP classic, such as Request, Response, Session, and Server.
  97. 97. Application Lifecycle pp y 97  The page life cycle is just one of several processing steps which occur as part of the larger ASP.NET life cycle. y
  98. 98. Application Lifecycle pp y 98

×