Sample Programming
Sample Programming
Sample Programming
murachs
ASP.NET 3.5
web programming with
VB 2008
(Chapter 1)
Thanks for downloading this chapter from Murachs ASP.NET 3.5 Web Programming with VB 2008. We hope it will show you how easy it is to learn from any Murach book, with its paired-pages presentation, its how-to headings, its practical coding examples, and its clear, concise style. To view the full table of contents for this book, you can go to our web site. From there, you can read more about this book, you can find out about any additional downloads that are available, and you can review our other books on .NET development. Thanks for your interest in our books!
Chapter 6
Section 1
Chapter 1
1
An introduction to ASP.NET web programming
This chapter introduces you to the basic concepts of web programming and ASP.NET. Here, youll learn how web applications work and what software you need for developing ASP.NET web applications. Youll also see how the HTML code for a web form is coordinated with the Visual Basic code that makes the web form work the way you want it to. When you finish this chapter, youll have the background you need for developing web applications of your own.
An introduction to web applications .................................... 4
Two pages of a Shopping Cart application ..................................................... 4 The hardware and software components for web applications ...................... 6 How static web pages work ............................................................................ 8 How dynamic web pages work ..................................................................... 10 How state is handled in ASP.NET applications ............................................ 12
Perspective ............................................................................ 28
Section 1
Chapter 1
Figure 1-1
Section 1
Chapter 1
Internet
Web browser
Description
Web applications are a type of client/server application. In that type of application, a user at a client computer accesses an application at a server computer. In a web application, the client and server computers are connected via the Internet or via an intranet (a local area network). In a web application, the user works with a web browser at the client computer. The web browser provides the user interface for the application. The most popular web browser is Microsofts Internet Explorer, but other web browsers like Mozilla Firefox and Opera may also be used. The application runs on the server computer under the control of web server software. For ASP.NET web applications, the server must run Microsofts web server, called Internet Information Services, or IIS. For most web applications, the server computer also runs a database management system, or DBMS, such as Microsofts SQL Server. The DBMS provides access to information stored in a database. To improve performance on larger applications, the DBMS can be run on a separate server computer. The user interface for a web application is implemented as a series of web pages that are displayed in the web browser. Each web page is defined by a web form using HTML, or Hypertext Markup Language, which is a standardized set of markup tags. The web browser and web server exchange information using HTTP, or Hypertext Transfer Protocol.
Figure 1-2
Section 1
Chapter 1
Server
Browser
HTTP response
Web server
HTML file
Description
A static web page is an HTML document that is the same each time its viewed. In other words, a static web page doesnt change in response to user input. Everyone who views a static web page sees exactly the same content. Static web pages are usually simple HTML files that are stored on the web server. When a browser requests a static web page, the web server retrieves the file from disk and sends it back to the browser. Static web pages usually have a file extension of .htm or .html. A web browser requests a page from a web server by sending the server an HTTP message known as an HTTP request. The HTTP request includes, among other things, the name of the HTML file being requested and the Internet addresses of both the browser and the web server. A user working with a browser can initiate an HTTP request in several ways. One way is to type the address of a web page, called a URL, or Uniform Resource Locator, into the browsers address area and then press the Enter key. Another way is to click a link that refers to a web page. A web server replies to an HTTP request by sending a message known as an HTTP response back to the browser. The HTTP response contains the addresses of the browser and the server as well as the HTML document thats being returned.
Figure 1-3
10
Section 1
Chapter 1
11
Server
Browser
HTTP response
Description
A dynamic web page is an HTML document thats generated by a web form. Often, the web page changes according to information thats sent to the web form by the browser. When a web server receives a request for a dynamic web page, it looks up the extension of the requested file in a list of application mappings to find out which application server should process the request. If the file extension is aspx, the request is passed on to ASP.NET for processing. When the application server receives a request, it runs the specified web form. Then, the web form generates an HTML document and returns it to the application server, which passes it back to the web server and from there to the browser. The browser doesnt know or care whether the HTML was retrieved from a static HTML file or generated dynamically by a web form. Either way, the browser simply displays the HTML that was returned as a result of the request. After the page is displayed, the user can interact with it using its controls. Some of those controls let the user post the page back to the server, which is called a postback. Then, the page is processed again using the data the user entered. The process that begins with the user requesting a web page and ends with the server sending a response back to the client is called a round trip. If you omit the file name from the URL when you use your browser to request a page, IIS will look for a file with one of four names by default: Default.htm, Default.asp, index.htm, and iisstart.asp. (If youre using IIS 7.0, it will also look for a file with the name index.html or default.aspx.) If you want another page to be displayed by default, you can add the name of that page to this list. See appendixes A and B for more information.
Figure 1-4 How dynamic web pages work
12
Section 1
Chapter 1
13
Server
Web server
Concepts
State refers to the current status of the properties, variables, and other data maintained by an application for a single user. The application must maintain a separate state for each user currently accessing the application. HTTP is a stateless protocol. That means that it doesnt keep track of state between round trips. Once a browser makes a request and receives a response, the application terminates and its state is lost.
Figure 1-5
14
Section 1
Chapter 1
15
Server
Windows Server 2003 or later Microsoft .NET Framework 3.5 Internet Information Services 6.0 or later Microsoft SQL Server or equivalent database FrontPage Server Extensions (for remote development only)
Description
Supports Windows and Web development using Visual Basic, C#, and C++. Same as Standard Edition with several additional features such as additional deployment options and integration with SQL Server 2005. The top-of-the-line version of Visual Studio, with special features added to support large development teams. Free downloadable edition for web development in Visual Basic, C#, or J#.
Visual Studio 2008 Team System Visual Web Developer 2008 Express Edition
Chapter
9
Description
Lets you define the overall look of a web site with one master page and then define the look of individual portions of the web site with other master pages that refer to the main master page. Lets you display, sort, insert, update, and delete the data in a bound data source. Provides a highly-customizable interface. Adds paging capabilities to the ListView control. The DataPager control is displayed separately from the control its used to page. Provides for using LINQ (Language-Integrated Query) to query and update a variety of data sources. Provides for developing web pages that are more responsive to the user and reduce the load on the web server.
ListView control
16
DataPager control
16
18 25
Figure 1-6
16
Section 1
Chapter 1
17
Description
.NET applications work by using services of the .NET Framework. The .NET Framework, in turn, accesses the operating system and computer hardware. The .NET Framework consists of two main components: the .NET Framework Class Library and the Common Language Runtime. The .NET Framework Class Library provides pre-written code in the form of classes that are available to all of the .NET programming languages. These classes are organized into groups called namespaces. The classes that support ASP.NET web programs are stored in the System.Web namespace. The Common Language Runtime, or CLR, manages the execution of .NET programs by coordinating essential functions such as memory management, code execution, security, and other services. The Common Type System is a component of the CLR that ensures that all .NET applications use the same data types regardless of what programming languages are used to develop the applications. All .NET programs are compiled into Microsoft Intermediate Language (MSIL) or just Intermediate Language (IL), which is stored on disk in an assembly. This assembly is then run by the CLR.
Figure 1-7 The components of the .NET Framework
18
Section 1
Chapter 1
19
Standalone development
Windows XP or later .NET Framework 3.5 Visual Studio 2008 Optional: IIS, SQL Server
Internet development
Windows XP or later .NET Framework 3.5 Visual Studio 2008
Internet connection
Windows Server 2003 or later .NET Framework IIS 6.0 or later FTP server SQL Server Server
Client
Description
When you use standalone development, a single computer serves as both the client and the server. Because Visual Studio comes with a scaled-back web server called the ASP.NET Development Server (or just development server), you dont need to use IIS for testing web applications on your own PC. However, you do need to use IIS to test certain features of web applications. When you use a local area network (LAN), a client computer communicates with a server computer over the LAN. With this setup, two or more programmers at the same site can work on the same application. This setup requires that FrontPage Server Extensions (FPSE) be installed on the server. When you use Internet development, a client computer communicates with a server computer over the Internet. With this setup, programmers at different locations can work on the same application. This setup requires an FTP server on the server. The FTP server uses File Transfer Protocol (FTP) to transfer files between the client computer and the server.
Figure 1-8
20
Section 1
Chapter 1
21
The aspx and Visual Basic files in the Shopping Cart application
Folder
App_Code App_Code (root) (root) (root) (root)
File
CartItem.vb Product.vb Cart.aspx Cart.aspx.vb Order.aspx Order.aspx.vb
Description
A class that represents an item in the shopping cart. A class that represents a product. The aspx file for the Cart page. The code-behind file for the Cart page. The aspx file for the Order page. The code-behind file for the Order page.
Description
For each web form in an application, ASP.NET 3.5 keeps two files. The file with the aspx extension holds the HTML code and the asp tags for the server controls. The file with the aspx.vb extension is the code-behind file that contains the Visual Basic code for the form. If an ASP.NET 3.5 application requires other classes like business classes, they are kept in the App_Code folder.
Figure 1-9
22
Section 1
Chapter 1
23
Figure 1-10
24
Section 1
However, since Page is the default object within a code-behind file, you can omit this reference. Although this is just a quick introduction to a code-behind file, I hope it gives you some insight into the way code-behind files are coded. Keep in mind, though, that all of this code will be explained in detail in the next two chapters. For now, you just need a general idea of how the events are processed, how the IsPostBack property is used, how one web page can switch to another page, and how the session state object is used.
Chapter 1
25
Figure 1-11
26
Section 1
Chapter 1
27
Other classes
Web form
1
ASP.NET runtime
4 2
Compiler Order class (dll)
Order_aspx
Compiler
3
Compiler Order.aspx class (dll) Other classes (dll)
Description
The first four steps of this process are done only the first time the aspx page is requested. After that, the page is processed directly from the compiled assemblies. For the Default page, the name of the code-behind class is _Default.
Figure 1-12 How an ASP.NET 3.5 application is compiled and run
28
Section 1
Perspective
Now that youve read this chapter, you should have a general understanding of how ASP.NET applications work and what software you need for developing these applications. With that as background, youre ready to learn how to develop ASP.NET applications of your own. And thats what youll learn to do in the next two chapters.
Terms
web application web page web form client/server application client server HTTP (Hypertext Transfer Protocol) web browser web server IIS (Internet Information Services) DBMS (database management system) LAN (local area network) intranet static web page HTML document HTML (Hypertext Markup Language) URL (Uniform Resource Locator) HTTP request HTTP response domain name dynamic web page server control application server application mapping round trip postback state stateless protocol view state session state session state object session ID application state application state object profile .NET Framework .NET Framework Class Library class namespace CLR (Common Language Runtime) IL (Intermediate Language) MSIL (Microsoft Intermediate Language) Common Type System standalone environment development server SQL Server Express FPSE (FrontPage Server Extensions) FTP (File Transfer Protocol) FTP server business class aspx code code-behind file page directive attribute render assembly
Chapter 1
29
Exercise 1-1
1.
Then, search for information about Visual Web Developer 2008 Express Edition. This of course is the site that you can use to download Visual Web Developer 2008 Express and SQL Server 2005 Express for free. As you move from page to page, note that some are static html pages and others are dynamic aspx pages. 2. Type this URL into your web browser:
http://www.discountasp.net
Then, click on some of the links on the home page, and note which pages are static and which are dynamic.
30
Section 1
Exercise 1-2
Use your web browser to run the Shopping Cart application on your PC
In this exercise, youll use your web browser to run the Shopping Cart application thats illustrated in this chapter. This assumes that youve already installed IIS, Visual Studio 2008, and the files for these exercises. When you get this application running, youll know that youve got IIS and the application set up correctly on your PC.
This should start the Shopping Cart application thats shown in figure 1-1. If it doesnt, you need to make sure that IIS is installed, that the exercise files are installed, and that you did step 1 correctly. 3. Select a product from the drop-down list on the Order page thats displayed. Note that this starts a postback that returns the Order page with the data and image for the product that was selected. Enter a quantity and click the Add to Cart button to display the Cart page. Then, click the Continue Shopping button to return to the Order page. Continue to experiment with this application until you understand how it works. Then, click the Close button in the upper right corner of the browser window to end the application.
4. 5.