Title: ASP'NET
1ASP.NET
- Web Applications and Web Services
2Web Applications
- Application resides on a web server
- Accessed on client machines via web browsers
- Multiple users from multiple client machines
from anywhere in world - for a single app - Pages from a web site may be
- Static every client sees same page no user
interaction - Dynamic either the page varies over time or it
permits user interaction (click button, enter
data, select choice, etc.) or both. May be
customized to individual clients.
3Desktop vs. Web Programming
TextBox - mobile
TextBox - Web
TextBox - Windows
4Desktop vs. Web Programming
Notice the much shorter list of supported events
for the web version of the TextBox. Where events
are similar they have the same name and are
processed much the same way.
5Desktop vs. Web Programming
- .NET designers intended for web programming to be
as similar to Windows programming as possible. - They both use the same names (such as TextBox)
for similar concepts. - In general, many features in Windows Forms cannot
be duplicated in Web Forms so Windows Forms can
be much richer in features than Web Forms. - On the other hand, some things such as validation
of user input is somewhat easier in Web Forms.
6Windows Programming
App does something and presents Win Form for User
7Windows Programming
- The program and its operating system reside on
the same computer. - The user is interacting with same computer.
- Program is loaded, started, and continues to run
until it ends. - Events are detected by the OS which sends
messages to the program which reacts to the
events to perform the actions of the program. - All of this is on the same computer.
8Web Programming Model
Oracle on Sun Server
Apache on Linux Server
9Web Programming
- Typically, multiple computers involved.
- At least one server at least one client.
- May be using disparate hardware, operating
systems, data types, languages, browsers, etc. - Standards allow these to work together
- HTTP, HTTPS
- TCP/IP
- HTML
- XML
10Web Programming
Client asks the server to do something such as
Send me page from this URL.
Client
Server
Client posts request to Server
Server Responds with HTML
Server retrieves and/or creates the requested
HTML and sends it to the client whose browser can
render (display) it.
11Web Programming
- If all web pages were static (unchanging), there
would be no need for ASP.NET. - The pages could be developed in standard HTML
using tools such as Dreamweaver or FrontPage (or
its replacement Expression Web). - Every time a page is retrieved, it would look
exactly like it did the previous time. - No user interaction with the page.
- No customization per user
12Dynamic Web Pages
- Dynamic web pages may change over time and users
may interact with them. - Add/replace/change/delete data in database.
- Accept input typed by user.
- Authenticate/authorize user to take certain
actions. - Allow user to click a button or select from a
list. - Display different ads, graphics, news item,
weather report, or other item(s) depending on
time/user identity/user location, etc.
13Web Programming
- Much of the work of building and working with
dynamic web pages is done on the server
(server-side or runatserver). - Easier because you only have to worry about one
architecture, one OS, one web server, one
programming language, etc. whereas the clients
can be of many disparate varieties with different
operating systems, architectures and on different
networks. - Harder because the server may have MANY clients
in different stages of interaction with the
dynamic pages and there must be a way to manage
those clients and keep in sync with each of them.
- Can be resource intensive on the server.
14Web Programming - Interaction without a Program
- When the server receives a request from the
client, it may load and execute an ASP.NET Web
Forms program (for example). - The ASP.NET programs output is HTML (that is, it
creates a web page on the fly). - Server sends the HTML to the client to render in
a web browser. - By the time the client receives the HTML and
renders it, the ASP.NET program has ended, and
the server has unloaded it and moved on to its
next task. There is NO program running behind
the web form as it is being viewed by the user.
15Web Programming - Interaction without a Program
- The major architectural difference between
Windows Forms and Web Forms - Windows Forms have a program running behind them
on the same machine at the same time - Web Forms have only a browser that renders their
HTML and there is no other program running behind
them as (while) the user interacts with them. - The user of an ASP.NET application is actually
interacting with the browser which acts as an
intermediary with the server machine, and the
user is not interacting with a running ASP.NET
application in the usual sense.
16Web Programming - Interaction without a Program
- When the user does something that requires work
at the server (e.g., presses a Submit button),
the browser sends a request to the server to
handle the event. This (and whatever data may
accompany the request) is called a post back. - The server reloads the ASP.NET program (or
possibly another one) which - Receives the post back data
- Handles the event
- Generates new HTML
- Responds to the client (sends the HTML back to
the client browser to render). - The ASP.NET program ends and is unloaded before
the client renders the HTML it received. - The cycle continues and may involve multiple
pages or may even transfer (link) to a web site
hosted on a different server.
17Notes about the Web Model
- Programs produce one page at a time and then end.
- Server wants programs to do their work and get
out of the way for the next task or next user. - No variables (program state) hang around from
one event to the next as in Windows Forms
applications. - HTTP is essentially a stateless protocol
- Managing the state is an issue that must be
addressed. - ASP.NET provides multiple ways to manage state.
- Nothing really happens on the server until a post
back occurs. For example, users can enter data
into a form, but it is not processed (or even
noticed) by the server until a post back occurs.
18More Notes About The Web Model
- Client-side execution of some code is possible,
but it requires JavaScript (or VBScript) code to
be embedded in the HTML and requires the client
browser to be able (and permitted) to execute the
script. Script is interpreted rather than
compiled. - Client-side execution is typically limited to
things like validating input before it is posted
back to the server. - ASP.NET is compiled takes longer to load on the
server the first time but then is much faster
than interpreted code.
19ASP.NET
- ASP.NET 2.0 is a set of technologies and classes
not a programming language. - ASP.NET 2.0 applications can be written in any
.NET language, but most are written in VB.NET or
C. - ASP.NET applications can be browser-independent
- .NET libraries include a large collection of
classes that support web programming including a
number of familiar server-side controls such as
TextBoxes, Labels, ListBoxes, and so forth.
20ASP.NET
- ASP.NET applications may include both static HTML
and executable code separated into separate
files (in somewhat the same way that C
separates declarations in header files from
executable code in .cpp files. The code is
compiled into a .dll assembly that is executed
by the ASP.NET worker process running on the web
server. - The static HTML may define the fixed parts of a
Web Form (as in the design view of a Windows
Form), and the code-behind provides additional
functionality to the Web Form much as it does in
the code view for a Windows Form.
21ASP.NET
- Supports data-binding for many of its server-side
controls. - Provides publish deployment of web applications
from development machines to web servers. - Web Server machines must be running a current
version of the .NET framework, but clients do
not.
22ASP.NET and IIS
- ASP.NET is not part of IIS (Internet Information
Server inetinfo.exe), but runs in association
with it. - IIS is the enterprise level Web Server
- XP Pro and Vista have versions that support a
small number of connections. - It is not installed in the default XP Pro
installation, but can be added as an option. - It must be added before installing VS.NET 2005
for ASP.NET to be properly configured to work
with it. - Full version runs under Windows Server 2003 and
supports many more connections depending on the
system and its configuration
23IIS
- Under Windows XP Professional and Vista, IIS
supports web sites at http//localhost/... - IIS web files are typically found in a folder
(usually on C named C\Inetpub\wwwroot. This is
what localhost refers to in the URL above. - Individual ASP.NET apps may be in individual
folders under that parent folder - Under VS2005, web applications may directly from
an arbitrary hard disk folder using a build and
test web server that is part of VS2005.
24New in VS 2005
- VS 2005 comes with a built-in Web Server (named
Cassini) that can be used by developers to test
their web sites as they build them. - Limited to use by the developer that is, VS
2005 must be running to use it, and no outside
user may browse to it. - Any browser registered on the developers machine
may view the pages served by Cassini.
25ASP.NET Application Execution on IIS
- In association with IIS, there is a single worker
process (ASPNET_WP.exe) running that hosts all
ASP.NET applications - Worker process runs under the user ID of aspnet
not the user ID of the web client or the
developer - The aspnet user has a relatively low level of
privilege by default. - Helps keep a web surfer from being able to access
things on the server that he/she should not be
able to access. - Limits legitimate application activities such as
database access, and this may need to be
addressed within the database server, for
example, or in the application configuration file.
26ASP.NET Application Execution
27Web Application Execution
28Base Class
- Windows Forms ultimately all derive from
System.Windows.Forms.Form - Similarly, Web Forms derive from
System.Web.UI.Page. - Files derived from this have an .aspx extension.
- There are two parts to a .aspx file
- An .aspx.resx resource file representing the
graphical view of the Web Form - An .aspx.vb (or .aspx.cs) code-behind file
- A total of 3 files in the project folder
represent this page. They have the extensions
noted.
29The .aspx File(s)
30The .aspx Design View
- Design view has a standard view and an HTML view,
either of which can be edited as needed.
31The HTML View
- HTML pages may include one or more high level
directives - _at_Page is the most important of the high level
directives - Others are in the following table
32The HTML View
33_at_Page Directive Attributes
34More _at_Page Attributes
35Example of _at_Page Directive
- Example
- lt_at_ Page Language"vb" AutoEventWireup"false"
Codebehind"default.aspx.vb" Inherits"DonBailes
._default" gt - Specifies that code-behind is in Visual Basic in
the file default.aspx.vb and that events are not
automatically wired up to handlers with specific
names. Wire up the handlers using the Handles
clause in VB or the in C with flexibility in
naming the handler methods. - Private Sub Page_Load (ByVal sender As
System.Object, ByVal e _ - As System.EventArgs) Handles MyBase.Load
In VB - this.txtName.TextChanged new
- System.EventHandler (this.txtName_TextChanged)
//In C
36Windows Forms vs. Web Forms
- A Windows Forms program renders the form on the
screen by drawing pixels - A Web Forms application sends HTML from the
server to a browser on the client machine and the
browser renders the form - Server dynamically creates some or all the HTML
sent to the browser - .NET Web controls and many HTML controls have the
runatserver attribute which means that their
events are handled on the server side rather than
on the client side (browser). - ltaspimagebutton id"pbtnCSAB" runat"server
- ToolTip"CSAB ImageAlign"Right"
- ImageUrlfile///C\Inetpub\wwwroot\pics\CSABlogo.
gif - BorderStyle"Outset" Width"40px"
Height"35px"gt - lt/aspimagebuttongt
37View State
- The term View State refers to the state of all of
the elements (controls) of a Web Form. - It also refers to a hidden field on the form that
is maintained by .NET. - The ViewState field contains the state
information (such as TextBox contents or ListBox
values) for the server side Web controls. - It is base64-encoded.
- Thus, the server is able restore the controls
values from the ViewState when it sends the
updated HTML back to the browser on the client
side after handling a post back request.
(Remember that the application ends when the HTML
is sent to the browser so there is no inherent
internal maintenance of the value of an
applications variables on the server.)
38Post Requests
- Some controls (such as buttons) always cause post
requests to be sent from the client to the server
when a specific event (such as a button click)
occurs with that control. - Many other controls do not cause a post-back.
- Some have properties that may be set to determine
if a post-back occurs. A TextBox, for example,
has the following property
39Post-Backs and Server Events
- It is important to note that most events that
happen on the client-side in an ASP.NET
application are handled on the server. We will
discuss client-side code execution later under
the topic AJAX. - For example, if a user clicks a button shown in a
page rendered by a browser running on a client
machine, the client machine does not handle that
event. - The response to the event is handled on the
server. - This requires a round trip from the client to the
server and back to the client before the user
gets the response expected from the event - The Post request from the client to the browser
initiates the round trip.
40Post-Backs and Server Events
- When an event on the client generates a Post
request, two additional hidden fields are
populated with information that - Identifies the control causing the event (e.g.,
which Button was clicked) - Gives any other information (arguments) needed to
handle the event (such as which item in a ListBox
was selected) - These hidden fields are named _EVENTTARGET and
_EVENTARGUMENT
41The Life of an ASPX Page
- Events are always handled in a very specific
order for Web Forms (unlike Windows Forms)
because the program ASP.NET application ends when
it sends the HTML to the client whereas a Windows
Form app may handle many events that occur in a
non-predetermined order as dictated by the
users interaction with the form. - The order is shown on the next slide.
- The event sequence shown occurs during a
Post-back sequence. When an initial GET request
is made to a page, the CreateChildControls event
occurs after the Load event of the Page class
fires.
42The Order of Actions for a Page