220 likes | 373 Views
13.1 Overview of the .NET Framework - A component is an encapsulation of software that can stand by itself and be used by other components - .NET is based in in part its predecessor, COM - .NET Framework is a collection of technologies
E N D
13.1 Overview of the .NET Framework - A component is an encapsulation of software that can stand by itself and be used by other components - .NET is based in in part its predecessor, COM - .NET Framework is a collection of technologies for the development and deployment of .NET software systems - The .NET Common Language Runtime (CLR) - Assembly integrity checking – using a hash code - JIT compilation - Garbage collection – can be forced - Exception handling - .NET languages from Microsoft: VB .NET Managed C++ .NET JScript .NET J# .NET C# + many more from other sources
13.1 Overview of the .NET Framework (continued) - .NET languages from Microsoft: VB .NET Managed C++ .NET JScript .NET J# .NET C# - There are now >20 .NET languages, including COBOL, Fortran, Perl, and Python - Every .NET language has a compiler that produces IL, which is JIT compiled to machine code - There is no IL interpreter - Advantage of multi-language systems: - Can use old components - Easy transition to .NET - Disadvantage of multi-language systems: - Maintenance is difficult
13.1 Overview of the .NET Framework (continued) - Common Language Infrastructure (CLI) - Common Type System (CTS) - Minimum type set for .NET - All .NET languages must support them - e.g., Int32, which corresponds to int in C# - All CTS types are derived from System.object - Two categories of data types: value and reference - Common Language System (CLS) - Minimum language constructs and rules - e.g., no operator overloading, no pointers, identifiers are not case sensitive, etc. - Framework Class Libraries (FCL) - > 4000 classes
13.1 Overview of the .NET Framework (continued) - Aim of CLI and CLR: interoperability - A component in any .NET language can: - Inherit from any other .NET language class - Call the methods of any other .NET language class - Subclass any class from any .NET language
13.2 Introduction to C# • - C# heritage: • - From Java: • - Single inheritance • - Interfaces • - Garbage collection • - No global types or variables • - Level of coercion • - From C++: • - Pointers • - Operator overloading • - Preprocessor • - structs, enums, … • - From Delphi and VB: • - Properties • - From J# (actually, J++): • - Delegates
13.2 Introduction to C# (continued) - C# heritage (continued): - New Features: - Indexes - Attributes - Events - A safer switch statement - A new kind of struct - Primitive Types and Expressions - Similar to Java, except C# has unsigned integers, a 16-byte decimal type, and pointers - Data Structures - Similar to Java and C++: class library support for Array, ArrayList, Queue, and Stack - Array class has many methods and a Length property - An enumeration type, similar to that of C++, except no coercions to or from other types
13.2 Introduction to C# (continued) - Control Statements - Like Java, except: 1. There is a goto, 2. There is a foreach statement foreach (int myInt in myIntArray) { … } 3. The switch has a static semantics rule that requires each selectable segment to end in an unconditional transfer (either break or goto) case 0: Zeros++; goto case 1; case 1: … - Classes, Methods, and Structures - Like Java, except: 1. Parameters can be passed by value (default), passed by reference, or passed by result - Pass by reference - ref - Pass by result - out
13.2 Introduction to C# (continued) 2. A method that can be overriden must be marked virtual A method that overrides must be marked override A method that has the same protocol as an inherited method but is NOT to override it is marked new 3. A method can take a variable number of parameters, if they are the same type void SumInts(params int[] intValues) { … } int [] myIntArray = new int[6] {2, 4, 6, 8, 10, 12}; ... sum1 = SumInts(myArray); sum2 = SumInts(10, i, 17, k); 4. A C# struct is a lightweight class - Supports constructors and can implement interfaces - Does not support inheritance or subclasses - Is allocated from the stack
13.2 Introduction to C# (continued) - Properties A property is a special data field of a class that can provide implicitly called get and set accessors public class Weather { public int DegreeDays { get { return degreeDays; } set { degreeDays = value; } private int degreeDays; } //** end of property DegreeDays ... } //** end of class Weather ... Weather w = new Weather(); .. w.degreeDays += degreeDaysToday; - Delegates – object-oriented method pointers public delegate void AHandler ( object o, System.EventArgs e); AHandler can reference any method with this protocol
13.2 Introduction to C# (continued) - Program Structure - System is a namespace of FCL that provides input & output, string manipulation, threading, and collections - Abbreviations of elements of System are premitted by: using System; Complete program example: using System; public class myTester { public static void Main() { Console.WriteLine(″Howdy!″); } } - File Storage for Programs - A file can store any number of public classes and/or interfaces - Every class can define a Main method, but if there are more than one in a file, you must tell the system where to begin
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)
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 - 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
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 - <input type = ″submit″ …/> HtmlInputRadioButton - <input type = ″radio″ - Difference between XHTML elements and their corresponding HTML controls: - Server-side code can interact with HTML controls - 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
13.4 ASP.NET Controls (continued) <form runat = ″server″> <input type = ″text″ id = ″address″ runat = ″server″ /> … </form> - 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)
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 SHOW ex3.aspx
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
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 event handler 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); }
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.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” /> -
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 <asp:placeholder id = ″buttonPlace″ runat = ″server″ /> 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
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 <asp:label id = ″output″ runat = ″server″ /> <% string msg = string.Format{ ″The result is {0} <br />″, result); output.Text = msg; %> SHOW ex4.aspx and ex4.aspx.cs
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