Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET.

Slides:



Advertisements
Similar presentations
Introduction to Web Application
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 7 Constructors and Other Tools. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 7-2 Learning Objectives Constructors Definitions.
Chapter 6 Structures and Classes. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-2 Learning Objectives Structures Structure types Structures.
Chapter 4 Parameters and Overloading. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 4-2 Learning Objectives Parameters Call-by-value Call-by-reference.
Chapter 1 C++ Basics. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 1-2 Learning Objectives Introduction to C++ Origins, Object-Oriented.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4 The Basics of Javascript Programming the World Wide Web Fourth.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 15 Introduction to Rails.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Introduction to Ruby.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 10 Servlets and Java Server Pages.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 9 Using Perl for CGI Programming.
Copyright © 2003 Pearson Education, Inc. Slide 1.
© 2010 Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of Chapter 11: Structure and Union Types Problem Solving & Program Design.
11 Copyright © 2005, Oracle. All rights reserved. Using Arrays and Collections.
JavaScript I. JavaScript is an object oriented programming language used to add interactivity to web pages. Different from Java, even though bears some.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
1 What is JavaScript? JavaScript was designed to add interactivity to HTML pages JavaScript is a scripting language A scripting language is a lightweight.
Introduction to .NET Framework
Microsoft.NET Object Oriented Software Engineering Based on a presentation by Murat Can Ganiz.
Tahir Nawaz Introduction to.NET Framework. .NET – What Is It? Software platform Language neutral In other words:.NET is not a language (Runtime and a.
Overview of programming in C C is a fast, efficient, flexible programming language Paradigm: C is procedural (like Fortran, Pascal), not object oriented.
Copyright © 2003 by Prentice Hall Computers: Tools for an Information Age Chapter 15 Programming and Languages: Telling the Computer What to Do.
Chapter 9 Interactive Multimedia Authoring with Flash Introduction to Programming 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Introduction to Programming G51PRG University of Nottingham Revision 1
ICE1341 Programming Languages Spring 2005 Lecture #16 Lecture #16 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
The Web Warrior Guide to Web Design Technologies
C#: Data Types Based on slides by Joe Hummel. 2 UCN Technology: Computer Science Content: “.NET is designed around the CTS, or Common Type System.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
Copyright © 2012 Pearson Education, Inc. Chapter 1: Introduction to Computers and Programming.
History  We first begin with Java which was released in 1995 by Sun Microsystems  Initially Java was 100% interpreted at runtime and was very slow 
COS 381 Day 23. © 2006 Pearson Addison-Wesley. All rights reserved Agenda Capstone progress report due Today Capstone projects are DUE May 10 at.
About the Presentations The presentations cover the objectives found in the opening of each chapter. All chapter objectives are listed in the beginning.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
Platforms and tools for Web Services and Mobile Applications Introduction to C# Bent Thomsen Aalborg University 3rd and 4th of June 2004.
Differences between C# and C++ Dr. Catherine Stringfellow Dr. Stewart Carpenter.
C# Tutorial From C++ to C#. Some useful links Msdn C# us/library/kx37x362.aspxhttp://msdn.microsoft.com/en- us/library/kx37x362.aspx.
OOP Languages: Java vs C++
Chapter 13 © 2005 by Addison Wesley Longman, Inc Overview of the.NET Framework - A component is an encapsulation of software that can stand by.
1 Chapter One A First Program Using C#. 2 Objectives Learn about programming tasks Learn object-oriented programming concepts Learn about the C# programming.
A First Program Using C#
Introduction to Java Appendix A. Appendix A: Introduction to Java2 Chapter Objectives To understand the essentials of object-oriented programming in Java.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Chapter 1: Introduction to Computers and Programming.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 1: Introduction to Computers and Programming.
Computing with C# and the.NET Framework Chapter 1 An Introduction to Computing with C# ©2003, 2011 Art Gittleman.
C#C# Classes & Methods CS3260 Dennis A. Fairclough Version 1.1 Classes & Methods CS3260 Dennis A. Fairclough Version 1.1.
.NET Framework Danish Sami UG Lead.NetFoundry
JavaScript, Fourth Edition
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
Introduction to Java University of Sunderland CSE301 Harry R. Erwin, PhD.
C#C# Introduction CS3260 Dennis A. Fairclough Version 1.0 Introduction CS3260 Dennis A. Fairclough Version 1.0.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Introduction to Java COM379 (Part-Time) University of Sunderland Harry R Erwin, PhD.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
Object Oriented Software Development 4. C# data types, objects and references.
Rich Internet Applications 2. Core JavaScript. The importance of JavaScript Many choices open to the developer for server-side Can choose server technology.
INTRODUCTION CHAPTER #1 Visual Basic.NET. VB.Net General features It is an object oriented language  In the past VB had objects but focus was not placed.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
Chapter 1: Preliminaries Lecture # 2. Chapter 1: Preliminaries Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation.
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Basic Introduction to C#
C# and the .NET Framework
Microsoft .NET 3. Language Innovations Pan Wuming 2017.
CS360 Windows Programming
Introduction to ASP.NET
C Language B. DHIVYA 17PCA140 II MCA.
Presentation transcript:

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 12 Introduction to ASP.NET

12-2 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.1 Overview of the.NET Framework.NET is a collection of technologies Run time environment Library Programming languages

12-3 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.1 Background A component is a piece of software that can used by other components A component has an interface that specifies how it can be used without necessarily exposing the implementation Microsofts component system was named COM.NET is a framework for developing and deploying software Software consists of components These components can reside on multiple systems These components can be programmed in different languages

12-4 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.1.NET Languages.NET initially included five languages Visual Basic.NET Managed C++.NET JScript.NET (similar to JavaScript) J#.NET (Similar to Java) C#.NET (A new language in the C/C++/Java family) Other languages have been added Including COBOL, Eiffel, Fortran, Perl Python

12-5 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.1 The Common Language Runtime CLR Services for processing and executing.NET software no matter what language Garbage collection Type checking Debugging Exception handling Compilers translate a.NET language in Intermediate Language (IL) The runtime system compiles IL on the fly to native machine code and executes that code The IL is not interpreted directly

12-6 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.1 The Common Language Infrastructure Two components Common Type System (CTS) Common Language Specification (CLS)

12-7 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.1 CTS CTS defines types supported by.NET languages Each type has a specified representation Integer types, for example, include Int32, 32-bit, signed integers.NET languages map their types into the CTS types C# type in maps to Int32 Two categories of CTS types Value types Reference types (an address of a memory location)

12-8 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.1 CLS Defines characteristics that languages must have to properly interoperate with other languages in the.NET framework Include requirements and restrictions No operator overloading No pointers Identifiers not case sensitive The C# language violates the listed restrictions The Framework Class Library (FCL) is a collections of classes providing resources for software

12-9 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.1 Introduction to C# C# has many similarities with Java

12-10 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.2 Origins of C# Designed as part of.NET Object-oriented Single inheritance, interfaces, garbage collection, no global variables or methods Pointers, operator overloading, preprocessor Properties Delegates Indexes, attributes, events

12-11 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.1 Primitive Types and Expressions Unsigned integer types byte, ushort, uint, ulong Signed integer types sbyte, short, int, long Floating point types float, double bool decimal char

12-12 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.2 Data Structures Array, ArrayList, Queue, Stack defined by the.NET FCL Array is a class, but syntax is like C/C++/Java int[] a = new int[100] Length property gives number of elements in the array Enumeration type Value type Finite set of values defined by the programmer Type safe

12-13 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.2 Control Statements The standard control statements of C/C++/Java are in C# as well foreach is added to step through a collection foreach (type identifier in collection) … The switch statement is almost the same except the syntax requires either a break or a goto at the end of each case

12-14 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.2 Classes, Methods, Structures C# has no methods or variables outside of classes Syntax of class definitions, variable declarations and function definitions similar to Java Parameters may be passed in any of three modes Pass by value (in) Pass by reference (in-out) Pass by result (out) A method may take a single formal parameter that is an array notated by the keyword params. This allows the method to be called with a variable number of parameters of the type of the elements of the array Overriding methods requires Marking the overridden method with virtual Marking the overriding method with override

12-15 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.2 Structs A C# struct is a lightweight class No inheritance Can have constructors Struct type objects are value types rather than reference types C# primitive types are implemented as structs

12-16 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.2 Properties A property of a class acts as if it were an instance variable However, assignment to the property actually invokes a set method associated with the property Access to the property invokes a get method Either method may be omitted If the set method is omitted, assignments to the property are not allowed Methods may perform whatever checks or calculations are needed

12-17 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.2 Delegates A delegate is a pointer to a method Methods may be subscribed to a delegate A delegate declaration specifies the protocol, or the signature, of methods that may be subscribed to the delegate Methods subscribed to the delegate may be called through the delegate

12-18 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.2 Program Structure The FCL is divided into numerous namespaces The most important namespace is System Input and output String manipulation Event handling Threading Collections System.Console is used for input and output to the console ReadLine WriteLine The using statement allows reference to members of a namespace without qualifying the references The main method of a program is Main Does not require parameters May return int or void

12-19 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.2 File Storage for Programs Multiple classes can be defined in a single source file Each class may have a Main method In that case, running a program must specify which Main is to start Source file names do not have to match the class name Visual Studio is the usual vehicle for developing.NET programs Programs can, however, be developed with any text editor The stackClass.cs file defines two classes

12-20 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.3 Introduction to ASP.NET

12-21 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.3 Basics of ASP.NET Active Server Pages Building dynamic web documents The predecessor, ASP, embedded interpreted scripting languages in XHTML This approach has performance problems It is difficult to divide up the development to different skill sets ASP.NET is similar The embedded languages allowed are the.NET languages All code is compiled ASP.NET documents extend the System.Web.UI.Page class Request and Response objects HTMLControls, WebControls IsPostBack

12-22 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.3 Page Members The Write method in Response sends output to the response document IsPostBack tells whether the current process is the original request for the page or a subsequent request with information from the initial page

12-23 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.3 Code-Behind Code for ASP may be moved to a code-behind class The ASP document itself will extend the code-behind class rather than System.Web.UI.Page

12-24 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.3 ASP.NET Documents Documents can include XHTML Directives Render Blocks Programming code in script elements Cannot define subprograms Program code in script elements Declare variables, define methods Server side comments

12-25 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.3 Directives Directive names begin Directives appear in but usually is attached to the <% is required Language attribute required: specifies.NET language used for program code

12-26 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.3 Output to XHTML Document Use Response.Write method Takes a string parameter Include markup since the target is an XHTML document The string.Format method can be used to format output

12-27 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.3 Example The ex1.aspx example creates an array of random numbers and displays them in the response page A on object of class Random is created to generate numbers Method Next generates the next number None or one or two parameters Two parameter form used, result is in range n…m-1 A script element in the header declares three variables and defines a method The render block in the body references these definitions and creates the dynamic part of the response Static XHTML is sent as part of the response unchanged

12-28 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.3 Code-Behind Files The example ex2 has two files, ex2.aspx and ex2.aspx.cs This partitions the declaration code into a C# file directive includes two new attributes Inherits: value is the class name in the code-behind file Src: value is the name of the code-behind file The Src attribute can be omitted if a compiled version of the file is available in a bin subdirectory

12-29 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 ASP.NET Controls XHTML elements associated with program code The code is executed on the server Two categories HTML controls Web controls

12-30 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 HTML Controls HTML controls are based on elements of XHTML pages The appearance and functionality of these elements can be changed as the server executes Executable code can be associated with the controls Certain controls can raise events ServerClick : control was clicked ServerChange : control content was changed HTML elements become HTML controls if They are on the list associated with controls (Table 12.2) The runat attribute has the value server

12-31 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 Controls and Code Note the runat attribute in the following XHTML: There is a corresponding instance variable in the code generated from this protected HtmlInputText address; There is no action attribute in the form: the ASP document defines the actions that result from submitting the form

12-32 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 Control Objects Controls are represented as objects in the code generated from ASP Control classes inherit from HtmlControl deriving properties and methods Attributes property provides tag attributes as name/value pairs The Href property is defined by the HtmlAnchor class An XHTML tag can be designated as an HTML control by simply adding the runat attribute with the value server

12-33 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 Life Cycle of an ASP.NET Document An ASP.NET document can describe both a form and the response Two kinds of requests to a ASP.NET document Initial request Request with form filled in, called a postback The IsPostBack property is true if the request is a postback request In a postback, the Value propety of a control provides the data entered into the corresponding widget The state of a document is stored in the response after the initial service A hidden control named ViewState contains a reference to a StateBag object The StateBag object stores data about the state of the document This requires extra information to be exchanged between browser and server

12-34 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10.4 Life Cycle A request is received A document object is created and initialized ViewState is initialized The document is sent The client sends a request back A document object is created and initialized with form data, including ViewState Form data is used to update the state of the document object ViewState is updated The program can directly set name/value pairs in ViewState before this point A response is returned

12-35 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 10.4 Postback Sources Clicking a submit button will cause a postback If the AutoPostBack property is set to true, a postback will occur when a change is made to a control

12-36 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 Page Level Events Two levels of events raised during processing Control events ( ServerClick, Server Change ) Page-level events Init: after document class instantiated Load: after state set from form data PreRender: before instance is executed Unload: before instance is discarded Implementing page-level event handling Controlled by AutoEventWireup, default true Which means use predefined method name by default Implement predefined method names ( Page_unload, Page_load, …) Override the virtual handlers

12-37 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 Control Events Two ways to register event handlers Assign method names to attributes OnServerClick and/or OnServerChange Use delegates Using attributes, the methods have predetermined signatures Using delegates Event handler written with proper signature New instance of delegate type created using the event handler Delegate subscribed to the event property of a control This is often done in the Page_Init handler so it is done one time as the page class is instantiated

12-38 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 Web Controls Web controls are based on the controls from Visual Basic Namespace System.Web.UI.WebControls Web controls do not match up directly with HTML form widgets An example of including a control in a page

12-39 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 Some Web Controls Panel organizes other controls AdRotator produces different content on different requests ListControl has four subclasses DropDownList ListBox CheckBoxList RadioButtonList

12-40 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 Creating Control Elements in Code A control can be created with <asp:button…. in the document The same control can be created by instantiating the Button class and assigning values to properties of the object.Text for the button label.id for the id attribute.OnClick for the handler.runat to specify this as a web control A asp:placeholder tag can be used to define a place for controls defined in code Those controls are added to the place holder

12-41 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 Response Output for Controls Response.Write does not place text properly when intermixed with controls Using a label is an alternative in order to place text This can be filled in later in the code <% string msg = string.Format( "The result is {0} ", result); output.Text = msg; %>

12-42 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 Example The ex4.aspx example crates a number of controls and creates a response File ex4.aspx.cs is the code-behind file

12-43 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.4 Validation Controls Validation controls validate data entered from other controls Server side validation is an important component of security These controls are placed immediately after the control whose input is being validated Four common validation controls RequiredFieldValidator CompareValidator RangeValidator RegularExpresionValidator Example ex5.aspsx illustrates three of these One field is required One field (a phone number) must match a regular expression pattern One field must be in a specific range of values

12-44 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.5 Web Services A collection of one or more related methods that can be called by remote systems.NET provides support for constructing and advertising web services

12-45 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.5 Constructing Web Services A document with extension.asmx is created This may simply have a WebService directive spcifying a codebehind file The web service is implemented as a class that extends System.Web.Services.WebService The web service should be place in a developer defined namespace in order to avoid conflicts Some methods of the class will be tagged with [WebMethod] to indicate that they are available as part of the web service Once a service is published, aspects of it can be viewed using Internet Explorer

12-46 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 12.5 Advertising Web Services Two approaches to making a web service known to potential clients A web services discovery document A web services directory written with UDDI