Characteristics of Server-Based Web Programming


I hope that you now have a reasonably clear picture of how server-based Web programming works. This model of Web programming has some interesting features that you need to be aware of because they affect the way you will do your own programming. These characteristics aren t unique to ASP.NET; they re inherent in server-based Web programming in general. In fact, ASP.NET includes a variety of features to make some of the following characteristics invisible to you in most cases.

Postbacks and Round Trips

If you followed the explanations discussed previously, you can see that ASP.NET programs your pages run in response to a user request. When a page runs, it generates HTML to send to the browser, which is what the user sees. To run another page, or the same page again, users have to send another request to the server.

This process sounds a little abstract, but you ve seen it in action frequently. Go to http://www.google.com/ and type in the word Microsoft. You ll get thousands of hits, but you see only 10 at a time. At the bottom of the page is a link that says Next. When you click the link, you are running the Google search again. Google reruns the search, but this time it plucks out the hits 11 through 20. Click Next again and Google runs the Microsoft search yet again, and this time shows hits 21 through 30.

Clicking the Next link posts the page and its current contents back to the Web server. That is, the Next link performs a postback. When Google gets the postback, it can read the search term ( Microsoft ) out of the page, along with other useful information, such as which hits it showed you last time, before running the query again.

This back-and-forth between browser and Web server is referred to as a round trip. In server-based Web programming, all interactions between users and server-based programs require a round trip because it s the postback from user to server that causes the server program to run. In ASP.NET, the buttons that you put on a page automatically cause a postback. As it turns out, this feature makes sense because users are accustomed to clicking a button in order to get something to happen.

The Disconnected Web

The browser sends requests to the Web server, and the Web server sends pages down to the browser. Except for the brief time in which information is being exchanged, the browser and Web server are not in contact. The official terminology is to say that the browser and Web server are disconnected.

In more human terms, imagine again that hypothetical fast-food restaurant. You enter and stand in line. When it s your turn, the clerk receives your request, obtains your meal, and hands it to you. That s it; the interaction was only long enough to perform your transaction. But now you re done with the clerk and the clerk is done with you and already on to the next customer.

The Web was designed to be disconnected for a good reason. Web servers have finite resources, but can potentially be serving pages to thousands of different individuals. It s impractical for the Web server to maintain an open channel to every user who has requested a page; the server would quickly run out of connections. As you know, you don t have to explicitly tell the Web server OK, I m done looking at pages on your site now. Instead, the Web server assumes it s done with you the moment it has sent the page you requested.

Each Page Is New

An interesting implication to the disconnected nature of the Web is that when the Web server gets a request, it always treats it like a first-time request. For example, go back to the example of searching with Google. You search for Microsoft, and Google sends you back a list of pages. When you click Next, Google doesn t know that it s you and that you re continuing a search made earlier. Google forgot all about you the second it sent you that first page. (Google probably fielded several thousand requests while you were looking through that first page.) So when you send it that Next request, Google starts from scratch with your search.

Remember that fast-food restaurant? If you want another order of fries with your meal, you stand in line and start the ordering process all over. Same thing.

In ASP.NET terms, this means that your pages run from scratch with each round trip. ASP.NET gets your request, runs the page, and sends you the HTML. ASP.NET then throws the page away, in keeping with the general disconnected model of the Web. If you click a button on the page to do a postback, ASP.NET starts all over with your page, even if it s only been a second or two since it ran the page the last time.

The fact that pages are recreated from scratch with each round trip is an important point that often trips up even experienced programmers. I ll remind you about this point a number of times as you program because you ll have to master some techniques that will initially seem odd to accommodate the new-page-every-time model.

Web Amnesia: Stateless Pages

As mentioned, the Web is disconnected and each postback results in pages running all over again. If pages run from scratch with each round trip, they can t remember anything about the last time they ran. The formal term here is that pages are stateless that is, they don t maintain state.

I ll use that search example one more time. When you get the first 10 hits and click Next, the search runs all over again. How does the page remember that you ve already seen the first 10 hits?

In a narrow sense, the page itself doesn t remember what hits you ve already seen at all. Instead, other mechanisms are available for storing this information that is, for maintaining state. In Google searches, for example, the search term is embedded into the URL so that when you click Next, Google can remember your search term by reading the URL. (You might want to try a search on Google to see all the gobbledygook that Google stores in the URL to maintain state.) When the page runs again, it can read the postback values and the URL and reconstruct where it was the last time.

ASP.NET provides a number of mechanisms for maintaining state. For example, ASP.NET does all the work required under the covers to maintain the values (state) of controls on the page. We ll also discuss other ways in which ASP.NET maintains state throughout this book.




Microsoft ASP. NET Web Matrix Starter Kit
Microsoft ASP.NET Web Matrix Starter Kit (Bpg-Other)
ISBN: 0735618569
EAN: 2147483647
Year: 2003
Pages: 169
Authors: Mike Pope
BUY ON AMAZON

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net