Visual Basic.NET Class September 30, 2003

Download Report

Transcript Visual Basic.NET Class September 30, 2003

.NET Fundamentals
ASP.NET - Web Forms
December 8, 2003
Agenda – December 8, 2003
Homework Questions
Quick Note on Namespaces
ASP.NET Web Applications
Simple WebTime Class Exercise
Web Form GuestBook Class Exercise
Homework Assignment for December 11, 2000
Topics
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
•
Quick Note on Namespaces
ASP.NET Technology
ASP.NET Web Applications in Visual Studio
Visual Studio .NET Software Requirements
Internet Information Services (IIS)
Web-Based Application Development
ASP.NET Web Applications in Visual Studio
Things To Look For When Creating a .NET Web Form Project
WebTime Class Exercise
Web Forms Events
Web Controls
Web Form Database Applications
GuestBook Database Class Exercise
Local versus Web Project
Deploying Web Applications
Tic Tac Toe Homework Problem
Namespaces
•
•
•
•
•
•
Arbitrary grouping
Can be more than one file
Contains at least one class file
Identified with a using statement in C#
Simplifies class declarations
Much freedom in naming (see below)
using System.Microsoft.Chips.GuestBookDB;
ASP.NET Technology
ASP.NET Technology
• Web Forms pages are built with ASP.NET technology.
• ASP.NET is a unified Web platform that provides all the
services necessary for you to build enterprise-class
applications.
• ASP.NET, in turn, is built on the .NET Framework, so the
entire framework is available to any ASP.NET application.
• Your applications can be authored in any language
compatible with the common language runtime, including
Microsoft Visual Basic, Visual C#, and JScript .NET.
ASP.NET Page Framework and the
Web Forms Page
• The ASP.NET page framework is a programming
framework that runs on a Web server to dynamically
produce and manage Web Forms pages.
• In Visual Studio, Web Forms provides a forms designer,
editor, controls, and debugging, which together allow
you to rapidly build server-based, programmable user
interfaces for browsers and Web client devices.
• Web Forms pages run on any browser or client device.
However, you can design your Web Forms page to target
a specific browser, such as Microsoft® Internet Explorer
5, and take advantage of the features of a specific
browser or client device.
ASP.NET Page Framework and the
Web Forms Page
• The ASP.NET page framework creates an abstraction of
the traditional client-server Web interaction so that you
can program your application using traditional methods
and tools that support rapid application development
(RAD) and object-oriented programming (OOP).
• Within Web Forms pages you can work with HTML
elements using properties, methods, and events.
• The ASP.NET page framework removes the
implementation details of the separation of client and
server inherent in Web-based applications by presenting
a unified model for responding to client events in code
that runs at the server.
ASP.NET Page Framework and the
Web Forms Page
• The framework also automatically maintains the state of
a page and the controls on that page during the page
processing life cycle.
• The ASP.NET page framework and Web Forms pages
also support server controls that encapsulate common UI
functionality in easy-to-use, reusable controls.
• ASP.NET supports mobile controls for Web-enabled
devices such as cellular phones, handheld computers,
and personal digital assistants (PDAs).
XML Web Services
• ASP.NET supports XML Web services.
• An XML Web service is a component containing business
functionality exposed through Internet protocols.
• An XML Web service enables applications to exchange
information between Web-based applications using
standards like HTTP and XML messaging to move data
across firewalls.
• XML Web services are not tied to a particular component
technology or object-calling convention. As a result,
programs written in any language, using any component
model, and running on any operating system can access
XML Web services.
State Management Facilities
• ASP.NET provides intrinsic state management
functionality that allows you to save and manage
application-specific, session-specific, and
developer-defined information. This information
can be independent of any controls on the page. It
can be shared between pages, such as customer
information or the contents of a shopping cart.
• ASP.NET offers distributed state facilities. You
can create multiple instances of the same
application on one computer or on several
computers.
Application Events
• ASP.NET allows you to include applicationlevel event-handling code in the optional
global.asax file. You can use application
events to manage application-wide
information and perform orderly application
startup and cleanup tasks.
Compilation
• All ASP.NET code, including server scripts, is
compiled, which allows for strong typing,
performance optimizations, and early binding,
among other benefits. Once the code has been
compiled, the runtime further compiles ASP.NET
to native code, providing improved performance.
Note: Web Forms pages, XML Web services, and
components are compiled into dynamic-link
library (.dll) files. Once compiled, these files do
not need to be registered on the Web server on
which they run.
Configuration
• ASP.NET configuration settings are stored in
XML-based files. Since these XML files are
ASCII text files, you can read and modify them,
so it is simple to make configuration changes to
your Web applications. Each of your
applications can have its own configuration file
and you can extend the configuration scheme to
suit your requirements.
Deployment
• Visual Studio provides a simplified
mechanism for deploying applications to
local or remote servers. You can install files
by simply copying them to your specified
application directories, or by using a more
specialized and configurable deployment
project.
Security
• When an ASP.NET application runs, it executes in
the context of a special local user on the Web
server, called ASPNET, with limited permissions.
This enhances the security of your Web
application code by restricting its access to
Windows resources and processes.
• ASP.NET also provides various authorization and
authentication schemes for Web applications. You
can easily remove, add to, or replace these
schemes depending upon the needs of your
application.
ASP.NET Web Applications in Visual
Studio
ASP.NET Web Applications in Visual
Studio
• Visual Studio .NET allows you to create
applications that leverage the power of the World
Wide Web.
• This includes everything from:
– a traditional Web site that serves HTML pages,
– to fully featured business applications that run on an
intranet or the Internet,
– to sophisticated business-to-business applications
providing Web-based components that can exchange
data using XML.
Visual Studio ASP.NET Web
Applications
• ASP.NET is part of the .NET Framework, so that it
provides access to all of the framework features.
• Create ASP.NET Web applications using any .NET
programming language (Visual Basic, C#,
Managed Extensions for C++, and many others)
and .NET debugging facilities.
• Access data using ADO.NET.
• Access operating system services using .NET
Framework classes, and so on.
Visual Studio ASP.NET Web
Applications
• ASP.NET Web applications run on a Web server
configured with Microsoft Internet Information
Services (IIS). However, you do not need to work
directly with IIS. You can program IIS facilities
using ASP.NET classes, and Visual Studio handles
file management tasks such as creating IIS
applications when needed and providing ways for
you to deploy your Web applications to IIS.
Where Does Visual Studio Fit In?
• As with any .NET application, if you have the .NET
Framework, you can create ASP.NET applications using
text editors, a command-line compiler, and other simple
tools. You can copy your files manually to IIS to deploy
the application.
• Alternatively, you can use Visual Studio. When you use
Visual Studio to create Web applications, you are creating
essentially the same application that you could create by
hand. That is, Visual Studio does not create a different
kind of Web application; the end result is still an ASP.NET
Web application.
Where Does Visual Studio Fit In?
• The advantage of using Visual Studio is that it provides
tools that make application development much faster,
easier, and more reliable. These tools include:
 Visual designers for Web pages with drag-and-drop
controls and code (HTML) views with syntax checking.
 Code-aware editors that include statement completion,
syntax checking, and other IntelliSense features.
 Integrated compilation and debugging.
 Project management facilities for creating and managing
application files, including deployment to local or remote
servers.
Visual Studio .NET Software
Requirements
Visual Studio .NET Software
Requirements
• Certain project types and features in Visual
Studio require that specific software
components, which may be listed as optional
in setup, must be installed before you can use
that feature or project. Some of these
components must be installed on the
development computer, while others can be
installed on a remote computer.
Identifying Software Requirement
Issues
The following table lists the components that must be
installed on the various operating systems to perform
the tasks specified. These components are not
installed with Visual Studio .NET Prerequisites.
Visual Studio .NET Software
Requirements
Do you want to
Windows 2000
Develop ASP Web Internet
applications and
Information
XML Web services Services (IIS)
Windows XP
Windows Server
2003 or later
Internet
Information
Services (IIS)
Compile code
Message Queuing Message Queuing
related to Microsoft Services
Services
Windows Message
Queuing (MSMQ)
Windows Server
2003 or later
Windows NT41
Windows 98
Windows Me
Windows XP Home
Not supported
Not supported
Visual Studio .NET Software
Requirements
Do you want to
Windows 2000
Windows XP
Windows Server
2003 or later
Windows Server
2003 or later
Windows NT41
Windows 98
Windows Me
Windows XP
Home
Debug code on
remote computers
Visual Studio
Remote Debugger
Visual Studio
Remote Debugger
Visual Studio
Remote Debugger
Use source code
control to version
stored procedures
Visual Studio 6.0
Stored Procedure
Versioning
Visual SourceSafe
Microsoft SQL
Server
Visual Studio 6.0
Stored Procedure
Versioning
Visual SourceSafe
Microsoft SQL
Server
Not supported
Internet Information Services (IIS)
Internet Information Services (IIS)
• Internet Information Services (IIS) 6.0 is a powerful Web
server, available in all versions of Microsoft Windows
Server 2003, which provides a highly reliable,
manageable, scalable, and secure Web application
infrastructure.
• IIS 6.0 enables organizations of all sizes to quickly and
easily deploy Web sites and provides a high-performance
platform for applications built using Microsoft ASP.NET
and the Microsoft .NET Framework.
Web-Based Application Development
Web-Based Application Development
• Creates Web content for Web browser clients
– HyperText Markup Language (HTML)
– Client-side scripting
– Images and binary data
• Web Forms (Web Form pages)
– File extension .aspx
– aspx (Web Form files) contain written code, event
handlers, utility methods and other supporting code
– .aspx.cs – C#.NET code behind page
Simple HTTP Transaction
• HyperText Transfer Protocol (HTTP)
– Defines methods and headers which allows clients and
servers exchange information in uniform way
• Uniform Resource Locator (URL)
– IP address indicating the location of a resource
– All HTML documents have a corresponding URL
• Domain Name Server (DNS)
– A computer that maintains a database of hostnames and
their corresponding IP addresses
System Architecture
• Multi-tier Applications
– Web-based applications (n-tier applications)
• Tiers are logical groupings of functionality
• Application Client or User Tier (top level tier)
– User Browser Interface to the Application
• Information Tier (data tier or bottom tier)
– Maintains data pertaining to the applications
– Usually stores data in a relational database management
systems (RDBMS)
• Middle Tier
– Acts as an intermediary between data in the information
tier and the application's clients
Creating and Running a Simple
Web-Form Example
• Visual Component
– Clickable buttons and other GUI components
which users interact
• Nonvisual Component
– Hidden inputs that store any data that document
author specifies such as e-mail address
.NET Frameworks Requires
• Internet Information Services (IIS)
• FrontPage Server Extensions (FPSE)
• See Visual Studio .NET Software Requirements
help text
• Not available on:
–
–
–
–
Windows NT4
Windows 98
Windows Me
Windows XP Home
Things To Look For When Creating a
.NET Web Form Project
Things To Look For When Creating a
.NET Web Form Project
• The project location is split:
– Project Files  C:\inetput\wwwroot\ project name
– Solution Files  C:\Documents and Settings\...\My
Documents\Visual Studio Projects\ project name
• Browser Reference:
– http://localhost/ project name
– C:\inetpub\wwwroot\ project name
Project Files
• Many Project Files and File Types located in:
C:\Inetpub\wwwroot\Week7\WebTime\…
–
–
–
–
–
Global.asax, Global.asax.cs, Global.asax.resx
Web.Config
WebForm1.aspx
 Windows Form Equivalent
WebForm1.aspx.cs  Code Behind File
WebForm1.aspx.resx
or for Visual Basic.NET
– WebTime.aspx, WebTime.aspx.vb,
WebForm1.aspx.resx
Web Forms Pages and Projects in
Visual Studio
• To work with a Web Forms page in Visual Studio, you use the
Web Forms Designer.
• The designer includes a WYSIWYG view, called Design
view, for laying out the elements of the page.
• Alternatively, you can switch the designer to HTML view,
which gives you direct access to the ASP.NET syntax of the
elements on the page.
• Finally, the designer includes a code editor with Intellisense
that you can use to create the page initialization and event
handler code for your page.
Project Files Created With Web Forms
• When you create a Web project, Visual Studio
constructs a Web application directory structure on
the target Web server, and a project structure on your
local computer.
Note You need to have appropriate privileges on the
Web server computer to create and manage Web
application files. The correct way to get those
privileges is to be a member of the VS Developers
group that is automatically created on the Web server.
Project Files Created With Web Forms
File Created
Description
WebForm1.aspx and
WebForm1.aspx.vb files
(or WebForm1.aspx.cs
for Visual C# Web
applications)
These two files make up a single Web Forms page. The .aspx
file contains the visual elements of the Web Forms page, for
example HTML elements and Web Forms controls. The
WebForm1.aspx.vb class file is a hidden, dependent file of
WebForm1.aspx. It contains the code-behind class for the Web
Forms page, containing event-handler code and the like.
AssemblyInfo.vb (for
Visual Basic projects) or
AssemblyInfo.cs (for C#
projects)
A project information file (AssemblyInfo.vb or AssemblyInfo.cs
file) that contains metadata about the assemblies in a project,
such as name, version, and culture information. For details on
data contained in the assembly file, see Assembly Manifest.
Web.config
An XML-based file that contains configuration data on each
unique URL resource used in the project. For more information
see Application Configuration Files.
Project Files Created With Web Forms
File Created
Description
Global.asax and
Global.vb or Global.cs
files
An optional file for handling application-level events. This file
resides in the root directory of an ASP.NET application. The
Global.vb class file is a hidden, dependent file of Global.asax. It
contains the code for handling application events such as the
Application_OnError event. At run time, this file is parsed and
compiled. For more information on Global.asax file usage, see
Global.asax File.
Styles.css
A cascading style sheet file to be used within the project. For
more information see Introduction to Cascading Style Sheets.
Solution Files
• Found in a Separate Directory
C:\Documents and Settings\...\My Documents\Visual Studio Projects\
project name
• Visual Studio.Solution (.sln file type)
WebTime.sln
• Solution User Options (.suo file type)
WebTime.suo
Solution Files (.sln and .suo)
Visual Studio .NET uses two file types (.sln and .suo) to store settings specific to
solutions. These files, known collectively as solution files, provide Solution Explorer
with the information it needs to display a graphical interface for managing your files.
They allow you to concentrate on your projects and final goals rather than on the
environment itself each time you return to your development tasks.
Extension
.sln
.suo
Name
Description
Organizes projects, project items and
Visual Studio.Solution solution items into the solution by providing
the environment with references to their
locations on disk.
Solution User Options
Records all of the options that you might
associate with your solution so that each
time you open it, it includes customizations
that you have made.
WebTime Class Exercise
WebTime Class Exercise
• Create a new ASP.NET Web Application (using C#)
• Add a label for the Title “WebTime”
• Add a label for the time (i.e. DateTime.Now.ToString)
• Set the font size on the above to XX-Large
• Add a Button to refresh the time
• and a little code…
private void Button1_Click(object sender, System.EventArgs e)
{
Label2.Text = DateTime.Now.ToString();
}
Web Form Events
Web Form Events
• Event – an object that encapsulates the idea
that something has happened.
• Events are generated or raised (i.e. when a
button is clicked)
• Event Handler – method that responds to the
event. Event handles are written is C# in the
code-behind page.
• Event handlers are delegates
Event Handles
•
•
Return a void
Two parameters:
1. Object raising the event
2. Event arguments – object of type EventArgs
that can expose properties specific to that
event type.
Types of Events
• Local events – mouseover, button clicks, text
changes
• Server handled events, requiring a "round trip".
– Postback events cause the form to be posted back to the
server immediately, including type events, such as
Button Click event.
– Non-postback events are cached by the control until the
next postback event occurs. You can override this nonpostback behavior by setting a controls AutoPostBack
property to true.
Events and Delegates
• An event is a message sent by an object to signal
the occurrence of an action.
• The action could be caused by user interaction,
such as a mouse click, or it could be triggered by
some other program logic.
• The object that raises (triggers) the event is called
the event sender.
• The object that captures the event and responds to
it is called the event receiver.
Events and Delegates
• In event communication, the event sender class
does not know which object or method will receive
(handle) the events it raises.
• What is needed is an intermediary (or pointer-like
mechanism) between the source and the receiver.
• The .NET Framework defines a special type
(Delegate) that provides the functionality of a
function pointer.
Events and Delegates
• A delegate is a class that can hold a reference to a
method.
• Unlike other classes, a delegate class has a
signature, and it can hold references only to
methods that match its signature.
• A delegate is thus equivalent to a type-safe
function pointer or a callback.
• While delegates have other uses, the discussion
here focuses on the event handling functionality of
delegates. The following example shows an event
delegate declaration.
Event Delegate Declaration.
• button1_Click - the delegate for the button event.
• EventArgs - the class that holds event data for the alarm
event. Actually System.EventArgs.
private delegate void button1_Click(object sender, EventArgs e)
• The syntax is similar to that of a method declaration; however, the
delegate keyword informs the compiler that button1_Click is a
delegate type.
• By convention, event delegates in the .NET Framework have two
parameters, the source that raised the event and the data for the
event.
Delegate
• Note A delegate declaration is sufficient to define a
delegate class.
• The declaration supplies the signature of the delegate, and
the common language runtime provides the
implementation.
• An instance of the delegate can bind to any method that
matches its signature.
Web Controls
Web vs Windows Controls
Windows Controls
Label
Web Controls
Label
TextBox
Button
RadioButton
TextBox
Button
RadioButton
GroupBox
ComboBox
DataGrid
GroupBox
DropDownList
DataGrid
Web Controls
AdRotator Control
• Randomly selects an image to display and
then generates a hyperlink to the Web page
associated with that image
20.5 AdRotator
Web Form Database Applications
Database Insert Command
Map a list of values to a list of the fields to be
updated.
INSERT INTO TableName(list of columns)
VALUES(corresponding list of data values)
Database Insert Command
INSERT INTO Messages(EmailAddress,
FirstName, LastName, Message,
MessageDate) VALUES
(‘[email protected]’,‘John’,‘Smith’,‘Hi to
me…’, ‘10/21/2003 12:34:16 PM’);
Database Insert Command
INSERT INTO Messages(EmailAddress, FirstName,
LastName, Message, MessageDate) " & _
"VALUES ('" & _
TextBox3.Text & "', '" & TextBox1.Text & "', '"
&_
TextBox2.Text & "', '" & TextBox4.Text & "', '"
& mTime.Now.Today & " " &
mTime.Now.ToLongTimeString & "')"
Differences from Windows Forms
•
Populating a Data Grid
–
Windows Form
dg.SetDataBinding(ds, "Books");
dg.Show();
–
Web Form
dg.DataSource = objDS;
dg.DataBind();
Debugging Web Form Applications
•
•
•
•
No MessageBox for error messages…
Use Response.Write to display messages
Use a label or TextBox to display
messages
Enable Tracing
GuestBook Database Class Exercise
Tracing
ASP.NET Tracing
• ASP.NET introduces new functionality that allows you to
view diagnostic information about a single request for an
ASP.NET page simply by enabling it for your page or
application.
• Tracing allows you to write debug statements directly in
your code without having to remove them from your
application when it is deployed to production servers. You
can write variables or structures in a page, assert whether
a condition is met, or simply trace through the execution
path of your page or application.
ASP.NET Tracing
• In order for these messages and other tracing information
to be gathered and displayed, you must enable tracing for
the page or application. When you enable tracing, two
things occur:
– ASP.NET appends a series of diagnostic information
tables immediately following the page's output. The
information is also sent to a trace viewer application (if
you have enabled tracing for the application).
– ASP.NET displays your custom diagnostic messages in
the Trace Information table of the appended
performance data.
ASP.NET Tracing
• Diagnostic information and tracing messages that you
specify are appended to the output of the page that is sent
to the requesting browser. Optionally, you can view this
information from a separate trace viewer (Trace.axd) that
displays trace information for every page in a given
application. This information can help you to clarify
errors or undesired results as ASP.NET processes a page
request.
• Trace statements are processed and displayed only when
tracing is enabled. You can control whether tracing is
displayed to a page, to the trace viewer, or both.
Tracing
Tracing
Local versus Web Project
Local versus Web Project
• Factors to consider when deciding between a local
or Web project model.
– Installation strategy
– Impact of workload
Installation strategy
• Quick-deploying application on a server with
minimal client configuration, the Web project
model is best. You can install and run this project
on the server without installing support files on
each client, and make upgrades only on the server
• Projects that have little server involvement or for
stand-alone applications, a local project model is
the better choice. In this scenario, upgrades can
be completed on each individual client computer.
Impact of workload
• Web projects offer a server-based strategy
where the majority of operations are conducted on
the server. The client computer uses a Web
browser to send information to and display
information from the server-side application.
• If server resources are limited, then use a local
project model where the work is done on the client
computer without consuming precious server time.
The following table gives a side-by-side comparison
of Web projects and local projects in both the design
and working environments:
Consideration
Web project
Local project
File storage during
development
Files are stored at a specified
URL on a computer with
Internet Information Services
(IIS) installed. The project also
keeps a temporary cache of
files on client computers for
working offline.
Project files are stored on
the local computer.
Build model when
developing
Application code is compiled
as a DLL and run on the server.
Results are seen on the client
computer.
Code is compiled and run
on the client computer.
The project is deployed to a
Web server.
Project files are installed
on the client computer
with required support
files.
Deployment
method
Consideration
Web project
Local project
Workload
Client computers run the application
remotely with a Web browser. Processing
is done primarily on the server.
The application is run
locally on the client
computer.
Upgrading
Application changes are made at the
server level without installation of
support files on client computers.
The application upgrades
are installed on client
computers.
Moving project
files
The Copy Project command is used to
move files to the specified URL.
Project files are
physically copied from
one location to another.
Deploying Web Applications
Deploying Web Applications
• Microsoft® ASP.NET applications can take advantage of many
different technologies, such as private and shared assemblies,
interoperation with COM and COM+, event logs, message
queues, and so on.
• An ASP.NET application is defined as all the Web pages (.aspx
and HTML), handlers, modules, executable code, and other (such
as images and configuration) that can be invoked from a virtual
directory and its subdirectories on a Web server.
• An ASP.NET application includes the project DLL (if the codebehind features of .aspx are used) and typically other assemblies
that are used to provide functionality for the application. These
assemblies are located in the bin folder underneath the virtual
directory of the application.
Deploying ASP.NET Application
• The Microsoft® .NET Framework makes deployment of
Web applications much easier than before.
• Configuration settings can be stored in a configuration file
(Web.config), which is a text file based on Extensible
Markup Language (XML)—these settings can be modified
after the Web application is deployed without requiring the
Web solution to be recompiled.
Deploying ASP.NET Application
• ASP.NET allows changes to be made to assemblies
without requiring that the Web server be stopped and
restarted.
• When a newer version of a Web.config, .aspx, .asmx, or
other ASP.NET file is copied to an existing Web
application, ASP.NET detects that the file has been
updated. It then loads a new version of the Web
application to handle all new requests, while allowing the
original instance of the Web application to finish
responding to any current requests. After all requests are
satisfied for the original application, it is automatically
removed. This feature eliminates downtime while updating
Web applications.
Deploying ASP.NET Application
• There are a number of different ways for deploying all of
these elements from your development or test environment
to the production Web server(s).
• For simple Web applications, it is often appropriate to
copy the files to the target computer, using XCOPY,
Microsoft Windows® Explorer, FTP, or the Microsoft
Visual Studio® .NET development system Copy Project
command on the Project menu.
• For more complex solutions, such as those that include
shared assemblies or those that rely on specific Microsoft
Internet Information Services (IIS) settings to be in place,
using Windows Installer technology might be a better
choice.
Tic Tac Toe
Homework
Assignment