Intro to ASP.NET (VBL).pdf

(326 KB) Pobierz
Introduction to ASP.NET
Introduction to ASP.NET
Introduction to
ASP.NET
Objectives
Review the features and shortcomings of classic Active Server Pages.
Understand the advantages of an ASP.NET application.
Learn about server controls and events in ASP.NET.
Create a simple Web Service in ASP.NET.
Web Pages and Web Services Professional Skills Development
1-1
Copyright © by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
39687568.008.png 39687568.009.png
Introduction to ASP.NET
A Review of Classic ASP
You may be an experienced developer of Web applications that use a previous
version of Microsoft Active Server Pages, or “classic” ASP. Or, you may be
just getting started as a Web developer. In either case, it is useful to review the
Active Server Page technology that preceded ASP.NET.
Dynamically Creating Web Pages
ASP is a technology that Microsoft created to ease the development of
interactive Web applications. When the Internet began, it was used to transmit
static HTML pages in response to HTTP requests from a browser. These Web
pages included hyperlinks that allowed users to navigate easily from page to
page, but that was about the extent of their interactivity.
As the Internet evolved, Web publishers developed ever more sophisticated
ways of allowing users to have more control over the pages they received.
HTML forms allowed users to enter information using simple Windows-like
controls such as text boxes, list boxes, and check boxes. The data from these
controls is embedded in the HTTP request that is issued when the user clicks a
Submit button.
Web server software execution technologies like CGI (Common Gateway
Interface) evolved to meet the need for more interactivity by intercepting
certain Web requests and running programs that created and returned custom
Web pages, usually based on data retrieved from a database.
After a couple of false starts (anyone remember ADC/HTX files?), Microsoft
settled on Active Server Pages as their way of supporting dynamic creation of
HTML pages based on user input.
An ASP Example
See Products-
ASP.asp
Fig ure 1 sho ws the results of running a typical ASP application. Microsoft
installs an ISAPI application with IIS that intercepts all requests for pages with
an extension of .asp.
These requests and the pages they point to are then handled within the ASP
run-time environment. This execution environment allows pages to contain
code in special script blocks delimited by < % and % > characters or contained
within < script > elements that include the attribute, runat=server. This script
performs whatever processing tasks are necessary to generate a custom HTML
page in response to the user’s request.
1-2
Web Pages and Web Services Professional Skills Development
Copyright © by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
39687568.010.png 39687568.011.png
A Review of Classic ASP
Figure 1. A typical “classic” Active Server Page.
The ASP page in this example begins with a couple of script-related lines that
identify the language and specify that all variables will be explicitly declared:
<%@ Language=VBScript %>
<%Option Explicit%>
The next lines are typical HTML that creates a title, defines an HTML form,
adds some text to the page, and begins a drop-down list box:
<html>
<head>
<title>Products-ASP</title>
</head>
<body>
<form action="Products-ASP.asp" method="post">
Select a Category:
<select name="Category">
Web Pages and Web Services Professional Skills Development
1-3
Copyright © by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
39687568.001.png 39687568.002.png 39687568.003.png
Introduction to ASP.NET
Embedding Data Access Code in the Page
Now, the page gets more interesting (and more complicated!), using ADO
objects to retrieve a list of product categories from the Northwind database on
a local SQL Server:
<%
Dim cnn, cmd, rst
Set cnn=Server.CreateObject("ADODB.Connection")
' Adjust user name and password, if necessary
cnn.Open "Provider=SQLOLEDB.1;Data Source=(local);" _
& "Initial Catalog=Northwind;User ID=sa;Password=;"
' Open a read-only, forward-only, server-side recordset.
Set cmd=Server.CreateObject("ADODB.Command")
cmd.CommandText= _
"SELECT CategoryID, CategoryName FROM Categories" _
& " ORDER BY CategoryName;"
cmd.ActiveConnection=cnn
Set rst=cmd.Execute
The ASP Object Model
The code above uses the CreateObject method of the ASP Server object to
instantiate the ADO objects used for data access. In addition to providing a
script execution engine, ASP also provides a set of six objects, including
Server, to facilitate the development of Web applications. Here is a brief
summary of these objects:
The Request object is used to read data that was packaged inside the
HTTP request for the page.
The Response object allows you to inject data, including HTML,
cookies, or redirection headers into the response stream that is sent
back to the client’s browser.
A Session object is created when the first request from a particular
client is processed, and it stays in scope until a timeout period expires
following the last request from that user, allowing you to store data
and objects that span multiple requests from one user.
The Application object is similar to the Session object, but its data is
shared across all client requests over the lifetime of the application,
and it also allows you to write code that runs automatically when the
applications starts or ends.
1-4
Web Pages and Web Services Professional Skills Development
Copyright © by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
39687568.004.png 39687568.005.png
A Review of Classic ASP
The ObjectContext object is used to commit or abort transactions
managed by MTS or COM+.
The Server object provides a set of generic utility methods for
creating COM objects, encoding data as HTML or URL strings that
can be embedded within the HTML sent back to a browser, and
finding the actual file locations that correspond to virtual paths.
Getting back to our sample, the next lines of code use the ASP Response
object to inject HTML into the HTTP response being sent back to the browser.
The category ID from each row in the recordset is assigned as the value for
each row of the drop-down list box:
' Use the ADO recordset to populate the dropdown list.
Do Until rst.EOF
Response.Write( _
"<option value=""" & rst("CategoryID") & """")
Handling ASP Postbacks
A postback is what happens when an Active Server Page creates HTML that
allows the user to call the same ASP all over again. In the example, the page
Products-ASP.asp is reloaded every time the user clicks the Show Products
button. This happens because the page’s address appeared in the action
attribute of the opening tag for the HTML form:
<form action="Products-ASP.asp" method="post">
When the user clicks the button a postback occurs, and the drop-down list is
populated all over again. But you want the users to see the category when they
get the new page, which will also now show them the products in that
category. The remainder of this section of code ensures that the category
matching the one in the HTTP request will be selected in the new page, and it
also adds the category name as the text for each row in the drop-down list box:
Web Pages and Web Services Professional Skills Development
1-5
Copyright © by Application Developers Training Company and AppDev Products Company, LLC
All rights reserved. Reproduction is strictly prohibited.
39687568.006.png 39687568.007.png
Zgłoś jeśli naruszono regulamin