Module 1: Working with ASP.NET Contents Overview Introducing ASP.NET Server Controls
1 2 13
Adding ASP.NET Code to a Page
23
Handling Page Events
30
Lab 1: Using ASP.NET to Output Text
35
Review
36
Information in this document is subject to change without notice. The names of companies, products, people, characters, and/or data mentioned herein are fictitious and are in no way intended to represent any real individual, company, product, or event, unless otherwise noted. Complying
with all applicable copyright laws is the responsibility of the user. No part of this document may be reproduced or transmitted in any form or by any means, electronic or mechanical, for any purpose, without the express written permission of Microsoft Corporation. If, however, your only means of access is electronic, permission to print one copy is hereby granted. Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property. 2000 Microsoft Corporation. All rights reserved. Microsoft, BackOffice, MS-DOS, Windows, Windows NT, names or titles. The publications specialist replaces this example list with the list of trademarks provided by the copy editor. Microsoft is listed first, followed by all other Microsoft trademarks in alphabetical order. > are either registered trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries. trademarks, provided by the copy editor> Other product and company names mentioned herein may be the trademarks of their respective owners.
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
Module 1: Working with ASP.NET
iii
Instructor Notes Presentation: 120 Minutes Lab:
30 Minutes
This module provides students with an overview of ASP.NET. Students will learn about the main features of ASP.NET and discover the differences between ASP and ASP.NET. They will also learn about server controls and see how to add server-side script to an ASP.NET page. In the lab, students will add HTML server controls to an ASP.NET page, and then add event procedures for the HTML server controls. Students will also add event procedures to the page. After completing this module, students will be able to: !
Identify the main features of ASP.NET.
!
Identify the differences between ASP and ASP.NET.
!
Describe the working model of ASP.NET.
!
Describe the architecture of server controls.
!
Add an HTML server control to a page.
!
Access the properties and methods of server controls in code.
!
Add event handlers for page events.
!
Use the IsPostback property to handle postback forms.
Materials and Preparation This section provides the materials and preparation tasks that you need to teach this module.
Required Materials To teach this module, you need the following materials: !
Microsoft® PowerPoint® file 2063A_01.ppt
!
Module 1, “ Working with ASP.NET” (2063A_01.doc)
!
Lab, “Using ASP.NET to Output Text” (2063A_L01.doc)
!
Animation: ASP.NET Execution Model (2063A_01A001.swf)
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
iv
Module 1: Working with ASP.NET
Preparation Tasks To prepare for this module, you should: !
Read all of the materials for this module.
!
Complete all the demonstrations.
!
Complete the lab.
!
Go through the animation.
!
Read up material about the .NET framework.
!
Read up material on the various differences between ASP and ASP.NET.
Demonstration This section provides demonstration procedures that will not fit in the margin notes or are not appropriate for the student notes.
Adding Server Controls to an ASP.NET Page ! To run this demonstration 1. View the file //localhost/2063/democode/Mod01/server_controls.aspx in Internet Explorer. This file has a form with an input box, a list box, and a Submit button. When you fill information in the controls and click Submit, the information is lost. 2. Edit the page. Add a runat="server" attribute to the 3 controls on the page. 3. View the page in the browser again. View the source of the page to show the changes that were made. The controls still lose their values. 4. Edi the page and add a runat="server" attribute to the form. 5. View the page in the browser again. View the source of the page to show the changes that were made. The controls now save their values. 6. Edit the page and change the controls to intrinsic web controls: asp:textbox, asp:listbox, asp:button. 7. View the page in the browser and show the source of the page.
Adding Code to Controls ! To run this demonstration The file 2063\DemoCode\Mod01\eventproc.aspx has the completed code
for this demonstration. 1. Open the file //2063/democode/mod01/server_controls.aspx. 2. Change the controls back to HTML server controls and add ID attributes to the text box and the listbox: txtName and lstTitle. 3. Add a span tag to the bottom of the page: id="spnGreeting" runat="server">
4. Add an onServerClick attribute to the button that will call the GreetMe procedure. BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
Module 1: Working with ASP.NET
v
5. Create a SCRIPT section for server-side code. <SCRIPT language="VB" runat="Server">
6. Create the GreetMe sub procedure. Sub GreetMe(s As Object, e As EventArgs) spnGreeting.InnerHTML = "Hello " & txtName.Value & _ ". I see your occupation is " & lstTitle.Value End Sub
7. View the page in Internet Explorer. When you click the button the values of the controls are displayed in the span 8. View the source of the page to show that the server-side code is not included in the page.
Using PostBack Forms ! To run this demonstration The file 2063\DemoCode\Mod01\postback.aspx has the completed code for this demonstration. 1. Open the file //2063/democode/mod01/server_controls.aspx. 2. Add a Page_Load event procedure that initializes the text box. Sub Page_Load(s As Object, e As EventArgs) txtName.Value = "Enter your name" End Sub
3. View the page in Internet Explorer. The text box gets loaded with default text each time. 4. In the Page_Load event procedure, check for IsPostBack and only initialize the text box the first time the page is loaded. If Not Page.IsPostBack Then txtName.Value = "Enter your name" End If
5. View the page again. Now the initial text is only used the first time.
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
vi
Module 1: Working with ASP.NET
Multimedia Presentation This section provides multimedia presentation procedures that do not fit in the
margin notes or are not appropriate for the student notes.
ASP.NET Execution Model ! To present the animation: Action
Say this
Click First Request
This is what happens when an .aspx is requested for the first time. The client sends a request for a page to the ASP.NET server. (The request is in the HTTP-GET form). In this animation, the client is requesting the Hello.aspx page. The server checks to see whether there is an existing output cache for the requested page, or else if the requested page is already compiled. If there is no output cache or compiled code, the source code for the page is interpreted by a parser, and a compiler is invoked. The compiler loads the page and compiles the code into an intermediate language (IL). The compiled IL code is used for returning the requested page to the client.
Click Second Request
When the client requests for the same page the second time, the server again checks for an existing output cache
or the compiled code. If the server finds the compiled code, then notice how the compiler step is omitted and the compiled code is straightaway used for returning the request.
Click Output Cache
This is the other case, when the server finds the requested page in the output cache. In this case notice how all the other steps are bypassed and the requested page is directly returned from the output cache.
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
Module 1: Working with ASP.NET
vii
Module Strategy Use the following strategy to present this module: !
Introducing ASP.NET This section provides students an overview of the .NET framework and how ASP.NET fits in that architecture. The focus of this section is the ASP.NET architecture; therefore, avoid getting into too many details about the .NET architecture. The section also talks about the main features of ASP.NET. Again avoid getting into the details because the following chapters deal with these features in detail. After discussing the main features of ASP, ask
students to compare it with ASP and come up with a comprehensive list of differences between ASP and ASP.NET. ASP
ASP.NET
.asp files
.aspx files
Can only be scripted using VBScript or JScript
Language independent
Difficult to reuse code
Allows separation of code and content
Need to write code for everything
Provides server controls
Does not support Web services
Includes Web services
This section also includes a demonstration on using VS.NET. This demonstration gives them the familiarity with the VS.NET interface. !
Server Controls ASP.NET introduces the concept of server controls that encapsulate common tasks and provide a clean programming model. This section provides students with an overview of server controls and how to use them in applications. This section also discusses Web controls, but do not get into the details of Web controls as these are discussed in detail in Module 2, “Using Web Controls”.
!
Adding ASP.NET Code to a Page ASP.NET introduces a new way of coding that is very similar to coding in event-driven languages. In this section, students will learn how to create a basic ASP.NET page. In addition, this section also includes a topic on the major differences between Visual Basic 6.0 and Visual Basic.NET Version 7.0.
!
Page Event Life Cycle This section provides students with information on the life cycle of an ASP.NET page. It talks about the main events that are involved in a page life cycle. The section also includes information on the use of IsPostBack property for postback forms. In the demonstration, show them what will happen if you do not use the IsPostBack property.
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
Module 1: Working with ASP.NET
1
Overview Topic Objective
To provide an overview of the module topics and objectives.
Lead-in
In this module, you will learn about the main features of ASP.NET, and learn how to add server-side script to an ASP.NET page.
!
Introducing ASP.NET
!
Server Controls
!
Adding ASP.NET Code to a Page
!
Handling Page Events
Microsoft's Active Server Pages (ASP) technology is widely used to create dynamic Web sites and applications. However, ASP has several limitations, such as the need for redundant and long-winded coding to accomplish simple goals. To overcome these limitations of ASP, Microsoft has developed a new technology called ASP.NET, which is a part of the .NET strategy for Web development. ASP.NET is a unified Web development platform that provides the services necessary for developers to build enterprise-class Web applications. In this module you will learn about the main features of ASP.NET and discover the differences between ASP and ASP.NET. You will also learn about server controls and see how to add server-side script to an ASP.NET page. After completing this module, you will be able to: !
Identify the main features of ASP.NET.
!
Identify the differences between ASP and ASP.NET.
!
Describe the working model of ASP.NET.
!
Describe the architecture of server controls.
!
Add an HTML server control to a page.
!
Access the properties and methods of server controls in code.
!
Add event handlers for page events.
!
Use the IsPostback property to handle postback forms.
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
2
Module 1: Working with ASP.NET
# Introducing ASP.NET Topic Objective
To introduce the topics
included in the section.
Lead-in
Before you start using ASP.NET, you should have an overview of ASP.NET and its framework.
!
The .NET Framework
!
ASP.NET Features
!
Animation: The ASP.NET Execution Model
!
Demonstration: Using Visual Studio.NET
In this section, you will read an overview of the .NET Framework and see how ASP.NET fits in. Next, you will learn about the various features of ASP.NET and see a working model. You will also learn about the main differences between ASP and ASP.NET.
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
Module 1: Working with ASP.NET
3
The .NET Framework Topic Objective
To provide an overview of the .NET Framework.
Web Forms Web Services
Lead-in
ASP.NET is integrated into the operating system. This architecture differs remarkably from earlier versions of ASP, which were basically add-ons to the operating system. ASP.NET is a part of the .NET Framework that supports all applications in Windows.
Win Forms
Data
Base Class Library Common Language Runtime
The .NET platform provides all the tools and technologies needed to build distributed Web applications. It exposes a consistent, language-independent programming model across all tiers of an application, while providing seamless interoperability with and easy migration from existing technologies. The .NET platform is composed of several core technologies: !
The .NET Framework
!
The .NET Building Block Services
!
The .NET Enterprise Servers
!
Microsoft Visual Studio.NET
!
Microsoft Windows.NET
The .NET Framework is a set of technologies that is an integral part of the Microsoft .NET platform. It provides the basic building blocks for developing Web applications and services. The .NET Framework includes the following components: !
Common Language Runtime
!
Base class library
!
Data
!
Web forms and Web services
!
Win forms
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
4
Module 1: Working with ASP.NET
Common Language Runtime The Common Language Runtime provides the programming interface between the .NET Framework and the programming languages available for the .NET platform. It simplifies application development, provides a robust and secure execution environment, supports multiple languages, and simplifies application deployment and management. The runtime loads and runs code written in any runtime-aware programming language. Code that targets the runtime is called managed code. Managed code simply means that there is a defined contract of cooperation between natively executing code and the runtime itself. Responsibility for tasks like creating objects, making method calls, and so on is delegated to the runtime, which enables the runtime to provide additional services to the executing code.
Base Classes and Libraries The .NET Framework includes classes that encapsulate data structures, perform I/O, give you access to information about a loaded class, and provide a way to invoke security checks. It also includes classes that encapsulate exceptions, and other helpful functionality such as data access, server side UI projections and rich GUI generation. The .NET Framework provides both abstract base classes and class implementations derived from those base classes. You can use these derived classes "as is" or derive your own classes from them. The .NET Framework classes are named using a dot-syntax naming scheme that connotes a naming hierarchy. This technique is used to logically group related classes together so that they can be searched and referenced more easily. A grouping of classes is called a namespace. For example, your program can use classes in the System.Data.SQL namespace to read data from a SQL Server database. The root namespace for the .NET Framework is the System namespace.
Data ADO.NET is the next generation of ActiveX Data Object (ADO) technology. ADO.NET provides improved support for the disconnected programming model and also provides rich XML support. ADO was created to provide data services to traditional client applications that were tightly coupled to the database; consequently it was not effective for web applications. ADO.NET was created with the characteristics of web applications in mind.
Web Forms and Web Services ASP.NET is a programming framework built on the Common Language Runtime that can be used on a server to build powerful Web applications. ASP.NET Web forms provide an easy and powerful way to build dynamic user interfaces. ASP.NET Web services provide the building blocks for constructing distributed Web-based applications. Web services are based on the industry standard SOAP specification.
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
Module 1: Working with ASP.NET
5
Win Forms For Windows-based applications, the .NET Framework provides the System.WinForms namespace to create the user interface. You can use System.WinForms to do rapid application design (RAD). It provides inheritance in the same client user interface library. You can build components using inheritance and then aggregate them using a form designer such as Microsoft Visual Basic.
WinForms will not be covered as part of this class.
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
6
Module 1: Working with ASP.NET
ASP.NET Features Topic Objective
To describe the main features of ASP.NET.
Lead-in
ASP.NET includes many features that make ASP.NET a preferred tool for Web development.
!
Multiple Language Support
!
Increased Performance
!
Classes and Namespaces
!
Server Controls
!
Web Services
ASP.NET is more than just the next version of ASP. It is a totally rearchitected technology for creating dynamic, Web-based applications. While ASP pages use the .asp extension, ASP.NET pages use the .aspx extension. Note Both ASP and ASP.NET pages can be used in the same Web site. Your existing ASP pages will still work along with your new ASP.NET pages, they don't have to be converted into ASP.NET pages. ASP.NET, with a host of new features, allows developers to write cleaner code that is simple to reuse and share. ASP.NET boosts performance and scalability by offering access to complied languages. Some of the main features of ASP.NET are described below.
Multiple Language Support ASP.NET provides a true language-neutral execution framework for Web applications. You can currently use over twenty languages to build .NET applications. Microsoft has compilers for Visual Basic, C#, C++, and JScript. Third party vendors are writing .NET compilers for Cobol, Pascal, Perl, and Smalltalk, among others.
The labs and the sample code in this course will use Visual Basic.
Increased Performance In ASP.NET, code is compiled. When the page is requested for the first time, the runtime compiles the code and the page itself and keeps a cached copy of the compiled result. The cached copy can be used whenever there is another request for the page. This results in greatly increased performance because after this first request, the code can run from the much faster compiled version, and the content on the page doesn’t have to be parsed again.
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
Module 1: Working with ASP.NET
7
Classes and Namespaces ASP.NET includes a range of useful classes and namespaces. These class libraries can make writing Web applications easier. Some of the classes included with ASP.NET are HtmlAnchor, HtmlControl, and HtmlForm, which are included within the System.Web.UI.HtmlControls namespace. For more information on namespaces, see the online Help for the .NET Framework.
Server Controls ASP.NET provides several server controls that simplify the task of creating pages. These server controls encapsulate common tasks that range from displaying calendars and tables to validating user input. They automatically maintain their selection state, and expose properties, methods, and events for
server-side code, thereby providing a clean programming model. You will learn more about using server controls in Module 2: “Using Web Controls.”
Web Services A Web service is an application delivered as a service that can be integrated with other Web services using Internet standards. ASP.NET allows you to use and create Web services. For example, a company can assemble an online store using the Microsoft Passport service to authenticate users, a third-party personalization service to adapt Web pages to each user's preferences, a credit-card processing service, a sales tax service, package-tracking services from each shipping company, and an in-house catalog service that connects to the company's internal inventory management applications. Web services provide the building blocks for constructing distributed Webbased applications. ASP.NET files have an .aspx extension, while Web services have an .asmx extension. The technologies are similar; however, instead of outputting HTML, a Web service outputs a computer-readable answer to the input it receives. You will learn more about Web services in Module 7: “Using Web Services.”
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
8
Module 1: Working with ASP.NET
ASP.NET Features (Continued)
!
Improved Security
!
Caching
!
Greater Scalability
!
Application State and Session State Management
!
Easy Configuration and Deployment
Improved Security In ASP, the only type of authentication that you can use is the Windows authentication, whereas ASP.NET allows different types of login and user authentication; Windows, Passport, and Cookies. ASP.NET also enables you to get real account impersonation and have the server execute code as if the user were present. You can also programmatically check to see if the user is in a given role and conditionally let him or her perform certain tasks when given permission. In addition, creating forms-based authentication in which you can create your own custom login screen and credential checking is much easier using ASP.NET.
You will learn more about authentication and creating login forms in Module 7: “Creating an ASP.NET Web Application.”
Caching Caching is an extremely important technique for building high-performance, scalable Web server applications. ASP.NET offers two caching mechanisms; output caching and ASP.NET caching that enhance Web application performance. Output caching is a powerful technique that increases server application throughput by caching the content generated from dynamic pages. The output cache is enabled by default and will cache any response that has a valid expiration or validation policy, and public cache visibility.
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
Module 1: Working with ASP.NET
9
ASP.NET provides a full-featured cache engine that can be utilized by pages to store and retrieve arbitrary objects across HTTP requests. The ASP.NET cache is private to each application and stores objects in memory. The lifetime of the cache is equivalent to the lifetime of the application. This means that when the application is restarted, the cache is recreated. You will learn more about caching in Module 7: “Creating an ASP.NET Web Application.”
Greater Scalability Scalability of .NET applications is enhanced because pages are compiled, and
page output can be cached. In addition, using ASP.NET, session state can now be maintained in a separate process on a separate machine, or in a database allowing for cross server sessions. This allows you to add more Web servers as your traffic grows.
Application State and Session State Management In ASP.NET application state and session state management is extended to provide a persistent and more scalable environment for storing values relevant to specific clients and applications. In particular, a user’s session state can now easily be maintained across a Web farm because it no longer depends on client side cookies. You will learn more about maintaining state in Module 7: “Creating an ASP.NET Web Application.”
Easy Configuration and Deployment Configuration and deployment are now easier with the use of human-readable configuration files and DLLs that don't have to be registered. In ASP.NET, all the configuration details for all Web applications are kept in human-readable files named config.web. The standard format for the configuration files is XML, and each application inherits the settings in the default config.web file. With .NET, all files that a Web site needs are located under the site's root folder. DLLs are in the /bin folder, and the config.web file is in the root folder. To deploy a Web site, all you have to do is copy the site’s root folder using file copy commands, the Microsoft FrontPage server extensions, or FTP. You will learn more about configuration options in Module 7: “Creating an ASP.NET Web Application.”
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
10
Module 1: Working with ASP.NET
Animation: The ASP.NET Execution Model Topic Objective
To describe the ASP.NET execution model.
Lead-in
In this animation, you will see how ASP.NET pages are processed on the server.
Delivery Tip
Run the Macromedia Flash animation ASP.NET Execution model. (2063A_01A001.swf)
In this animation, you will she how ASP.NET pages are processed on the server. To view the animation, open the 2063A_01A001.swf file from the Media folder.
For details on how to run
and describe the animation, see the Multimedia Presentation section in the Instructor Notes for this module.
Delivery Tip
After showing the animation, summarize the differences between ASP and ASP.NET.
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
Module 1: Working with ASP.NET
11
Demonstration: Using Visual Studio.NET Topic Objective
To demonstrate how to use Visual Studio.NET as an ASP.NET page editor.
Lead-in
In this demonstration, you
will see how Visual Studio.NET will be used in this course.
In this demonstration, you will see how to use Visual Studio.NET.
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY
12
Module 1: Working with ASP.NET
Delivery Tip
1. Open Visual Studio.NET and Create a new Web Application project. 2. Close all the windows except the Solution Explorer. 3. Point out the files that are added to the project by default. For most of the course we will only be working with ASPX files. 4. An ASPX page is created by default, WebForm1.aspx, and is opened in Design view. Show students how to
switch to HTML view. 5. A code-behind page is created by default, WebForm1.vb, for the ASPX page. Show students how to delete all the attributes of the @Page directive except the Language attribute. Note: Even though we won't be using the code-behind page, it is still there. Show students how to view the code-behind page (by double-clicking on the ASPX page in design view) and how to close it (by clicking the close button). 6. Add an HTML text control and a button Web control to the default form to show the intellisense. id="txtName" runat="server"> name="btnSubmit" text="submit" id="btnSubmit" onclick="btnSubmit_OnClick " runat="server"/>
7. Add a script section to an ASP.NET page, and create a Click event for the button that outputs a value in the text box. 8. Save your work. 9. Either view the page in the browser or click Start.
BETA MATERIALS FOR MICROSOFT CERTIFIED TRAINER PREPARATION PURPOSES ONLY