Presentation is loading. Please wait.

Presentation is loading. Please wait.

COS 381 Day 23. © 2006 Pearson Addison-Wesley. All rights reserved. 13-2 Agenda Capstone progress report Overdue –Only received 1 out of 6 Capstone projects.

Similar presentations


Presentation on theme: "COS 381 Day 23. © 2006 Pearson Addison-Wesley. All rights reserved. 13-2 Agenda Capstone progress report Overdue –Only received 1 out of 6 Capstone projects."— Presentation transcript:

1 COS 381 Day 23

2 © 2006 Pearson Addison-Wesley. All rights reserved. 13-2 Agenda Capstone progress report Overdue –Only received 1 out of 6 Capstone projects are DUE May 10 at 1PM Assignment #6 Corrected –2 C’s, 1 D and 3 F’s –F’s are for incomplete assignment – hard to get any credit of you don’t try –Answer to exercise six –http://perleybrook.umfk.maine.edu/classes/cos381/instructor/cgi/conelec2.htmlhttp://perleybrook.umfk.maine.edu/classes/cos381/instructor/cgi/conelec2.html Next we will be doing Chapter 13 on ASP (with C#) –This text is a bit weak on ASP.Net so I will be adding outside material and handouts to augment the material –3 lectures total There will be one lecture on Database access Quiz 4 on May 4 –ASP.NET and Database Access Assignment 7 (final one) is posted and is Due on May 2 –Create a complex ASP.NET page –http://perleybrook.umfk.maine.edu/samples/assign7/UMFKorder.aspxhttp://perleybrook.umfk.maine.edu/samples/assign7/UMFKorder.aspx

3 © 2006 Pearson Addison-Wesley. All rights reserved. 13-3 13.3 Introduction to ASP.NET - The Basics of ASP.NET - Based on ASP, but revolutionarily different - ASP documents could have embedded scripts in either Jscript or VB – both purely interpreted - Disadvantages: 1. Inefficient 2. Mixing script and markup is confusing 3. Scripting languages are unreliable - ASP.NET differs from JSP in two ways: 1. Any.NET language can be used 2. All ASP.NET code is compiled - Code can be embedded in ASP.NET documents, or can be separate in a code-behind file - Every ASP.NET document is compiled into a class - Base class is System.Web.UI.Page, unless there is a code-behind class (then it is the base)

4 © 2006 Pearson Addison-Wesley. All rights reserved. 13-4 Inheritance Diagrams for ASP.NET documents with and without code-behind files

5 © 2006 Pearson Addison-Wesley. All rights reserved. 13-5 13.3 Introduction to ASP.NET (continued) - ASP.NET documents - Can include: 1. XHTML markup 2. Directives – appear in blocks 3. Render blocks - No method definitions - Put into a function in the document class 4. Declaration blocks - Script elements - method definitions 5. Server-side comments - The only directive covered here is @Page - The only necessary attribute is Language  SHOW ex1.aspx http://perleybrook.umfk.maine.edu/classes/cos381/instructor/asp/ex1.aspx ex1.aspx http://perleybrook.umfk.maine.edu/classes/cos381/instructor/asp/ex1.aspx - Code-behind Files - The @Page directive must specify the code-behind file in a Inherits attribute - If you want the code-behind file implicitly compiled, include a Src attribute  SHOW ex2.aspx and ex2.aspx.cs ex2.aspx ex2.aspx.cs http://perleybrook.umfk.maine.edu/classes/cos381/instructor/asp/ex2.aspx

6 © 2006 Pearson Addison-Wesley. All rights reserved. 13-6 13.4 ASP.NET Controls - Two collections of server controls: HTML controls and Web controls - HTML Controls - One-to-one correspondence with the XHTML elements HtmlInputButton - HtmlInputRadioButton - <input type = ″radio″ - Difference between XHTML elements and their corresponding HTML controls: - Server-side code can interact with HTMLcontrols - Many HTML controls can raise events - ServerClick and ServerChange - Any element that will be used by server-side code must include the runat = ″server″ attribute - A form that has server-side elements must also include the runat = ″server″ attribute - Some HTML controls are not form elements

7 © 2006 Pearson Addison-Wesley. All rights reserved. 13-7 13.4 ASP.NET Controls (continued) <input type = ″text″ id = ″address″ runat = ″server″ /> … - The form has no action attribute - The server-side code (the document class) would have: protected HtmlInputText address; - All HTML controls are converted to objects in the document class - An ASP.NET document with a form has two purposes: 1. Describe the form to be displayed by the browser 2. Process the form when its data is submitted - Each of these has its own kind of request – initial and postback  SHOW ex3.aspx (after the form is filled) ex3.aspx http://perleybrook.umfk.maine.edu/classes/cos381/instructor/asp/ex3.aspx

8 © 2006 Pearson Addison-Wesley. All rights reserved. 13-8 TABLE 13.1 HTML control types, corresponding XHTML elements, and their events

9 © 2006 Pearson Addison-Wesley. All rights reserved. 13-9 13.4 ASP.NET Controls (continued) - Document classes maintain form data state between postbacks - In the ViewState hidden element of the form - The life of an ASP.NET document: 1. The client requests the document 2. A document class is created by compiling the requested document; then its constructor is called 3. The control state of the document is initialized with ViewState 4. Request form data is used to set the control state 5. The current control state is saved in ViewState 6. The instance is executed and the results are returned to the client 7. The class and its instance are deleted on the server 8. The client interacts with the form 9. The client causes a postback 10. A document class is compiled and its constructor is called 11. The control state is initialized from ViewState 12. The control state is set with the form data 13. The current state is saved in ViewState 14. The instance is executed and the results are returned to the client http://perleybrook.umfk.maine.edu/classes/cos381/instructor/asp/ex3.aspx

10 © 2006 Pearson Addison-Wesley. All rights reserved. 13-10 13.4 ASP.NET Controls (continued) - There are two levels of events – control events and page-level events - Four page-level events are implicitly raised during the process of processing a request Load, Unload, PreRender, and Init - There are two ways to write and register handlers for these events 1. Write handlers with preassigned names and a specific protocol – implicitly registered public void Page_Init(System.EventArgs e) { … } - Called auto event wireup 2. Overload virtual methods and manually register them - Control Events - ServerClick and ServerChange - Two ways to write and register handlers

11 © 2006 Pearson Addison-Wesley. All rights reserved. 13-11 13.4 ASP.NET Controls (continued) 1. Write them as functions and register them in the XHTML ( OnServerClick and OnServerChange ) - The protocol for control event handlers is protected void TextBoxHandler ( object src, System.EventArgs e) {... } <input type = ″text″ id = ″Name″ OnServerChange = ″TextBoxHandler″ runat = ″server″ /> 2. The second way to register a control eventhandler is to use the standard CLR approach - Write the handler, as before - Create a handler delegate instance, passing the name of the handler to the constructor - Subscribe the new delegate instance to the control and the event name protected void Page_Init( object src, EventArgs e) { Name.ServerChange += new EventHandler(TextboxHandler); }

12 © 2006 Pearson Addison-Wesley. All rights reserved. 13-12 13.4 ASP.NET Controls (continued) - Revised list of what happens: 1. Client requests the document 2. A document class is compiled and its constructor is called 3. The Page event Init is raised 4. The control state of the instance is initialized with ViewState 5. The form data is used to initialize the control state 6. The Page event Load is raised 7. Server-side control events are raised 8. The Page event PreRender is raised 9. The current control state of the instance is saved in ViewState 10. The instance is executed and the results returned to the client 11. The Page event Unload is raised 12. The class and its instance are deleted on the server - Web Controls - A larger and richer collection than the HTML controls – based on those of VB - A weaker connection to the XHTML elements

13 © 2006 Pearson Addison-Wesley. All rights reserved. 13-13 13.4 ASP.NET Controls (continued) - A more consistent programming interface - Web controls include: - Xml – allows the inclusion of XSL transformations - Panel – allows collections of elements to be handled together (placement, etc.) - AdRotator – Easy way to have different content appear on different requests - Validator controls – later - Controls can be created by either markup or by programming code - For example, <asp.button id = ″helpButton″ Text = ″help″ OnClick = ″OnClickHandler″ runat = “server” /> -

14 © 2006 Pearson Addison-Wesley. All rights reserved. 13-14 13.4 ASP.NET Controls (continued) protected Button helpButton = new Button(); helpButton.Text = ″help″; helpButton.id = ″helpButton″; helpButton.OnClick = ″OnClickHandler″; helpButton.runat = ″server″; - There are two problems with doing it this way: 1. It required more typing 2. Placement of the control in the document is cumbersome - Can use a placeholder buttonPlace.Controls.Add(helpButton); - Although creating elements is easier with markup, modifying them is a good use of code - Example: put the list items in a drop down list with code

15 © 2006 Pearson Addison-Wesley. All rights reserved. 13-15 13.4 ASP.NET Controls (continued) - Response output from controls - Can’t use Response.Write, because the output goes to the beginning of the buffer, rather than close to the controls - Alternative: - Create a label element where you want the output to appear - Set the content of the label by assigning to its Text property - Use string.Format for output with text and values <% string msg = string.Format{ ″The result is {0} ″, result); output.Text = msg; %>  SHOW ex4.aspx and ex4.aspx.cs

16 © 2006 Pearson Addison-Wesley. All rights reserved. 13-16 13.4 ASP.NET Controls (continued) - Validation Controls - There are six; the most commonly used four: - RequiredFieldValidator - CompareValidator - RangeValidator - RegularExpressionValidator - Validation controls are placed just after the controls whose values they are to validate - Use the ControlToValidate attribute to specify the control to be validated - Use the ErrorMessage attribute to specify the error message  SHOW ex5.aspx


Download ppt "COS 381 Day 23. © 2006 Pearson Addison-Wesley. All rights reserved. 13-2 Agenda Capstone progress report Overdue –Only received 1 out of 6 Capstone projects."

Similar presentations


Ads by Google