Building the User Interface with Web Forms
While most traditional applications used the Windows Forms model, creating Web or browser applications continues to become more and more important. Today, we will look at how we can create browser-based user interfaces. The tools within Visual Basic.NET assist the developer in easily creating web pages that provide fairly rich user interfaces across any browser type. In particular, today we will focus on:
How the web programming model differs from the traditional Windows based model
Using standard Web Forms controls
Using advanced Web Forms controls
Using the Validator controls
The Web Programming Model
Some days I think that everyone in the world is on the Internet, especially when my access speed is in the painfully slow mode as everyone is browsing, chatting and e-mailing (but not doing business). (As a tip, I somehow find that the best time to be online is during Star Trek – probably just a coincidence). Obviously, one of the most important, or at least popular, tools aspects of the Internet is the World Wide Web (WWW). However, there has generally been a shortage of really good programming tools for creating WWW 'programs', rather than just simple WWW pages. This is at least partly because creating applications for the Web is different from creating applications for a desktop computer, where you have more control. In addition, web applications must deal with the network more frequently.
So, what is the "Web Programming model"? It is just a term used to describe how you can design, or architect, a program that uses web pages in a browser as a way to allow the user to enter information. These web pages are designed using HTML (HyperText Markup Language – we won't be covering HTML in this book, but there are plenty on the market). The browser is an application that knows how to read HTML and display it on the screen. Usually (but not always), most of the work of the application is done on the web server. The Web Server is another program running on a computer that knows how to return HTML on demand. In Windows NT and 2000, this program is called Internet Information Server (IIS). The information is carried between the browser and the server using a protocol, or language, called HTTP (HyperText Transfer Protocol).
The actual name of IIS has changed with the different versions. In Windows NT 4.0 Server, it is called Internet Information Server. In Windows 2000, it is called Internet Information Services, while in Windows NT 4.0 Professional it is Personal Web Server.
In the 'beginning' of the World Wide Web, web pages were static. That is, they never really changed. Things got more interesting when people started to create dynamic, or changing, web pages. This was the dawn of the 'Web Program'. With 'Web programs', rather than simply returning the same HTML every time, the web server may perform some task and return HTML appropriate to the result. For example, the user might request the sales information for a particular date range. This information is passed to the server. The server in turn might look that information up in a database, and then write the HTML to display it to the user. The whole process looks similar to Figure 10.1.
Figure 10.1 Web programming model.
Figure 10.2 Dynamic HTML programming model.
There are a variety of techniques that can be used to create a web program. Some of the most common techniques used in the past have been ASP (Active Server Pages), Perl (another programming language) or JSP (Java Server Pages). The technique used by Web Forms is an improvement of ASP, ASP.NET.
ASP.NET is Microsoft's name for its improved version of ASP. While ASP was a very easy method for building dynamic web pages, it had some problems that ASP.NET is designed to solve:
ASP often required too much code to get something done. ASP.NET requires less code (often much less code) for common programming tasks.
ASP still suffered from the limited number of controls that HTML suffers from. ASP.NET adds the idea of "server-side controls" that can generate HTML appropriate to the browser requesting it. While these controls are just HTML in the browser, they can represent a great deal of HTML and code, saving you from _having to write it.
ASP only allows you to program in a language such as VBScript. VBScript is interpreted at run time, not compiled like real Visual Basic. ASP.NET allows you to write their web pages in real, fully compiled Visual Basic.NET.
ASP.NET is also designed to solve other problems with ASP that aren't appropriate to our discussion of building user interfaces, such as improvements to scalability and memory use. There should be a few good books available specifically on ASP.NET by the time you read this.
How Creating Web-based Programs is different from Creating Windows-based Programs
When you are designing a Web-based program using WebForms, there are a number of differences you must keep in mind. Some of the differences include:
Web-based applications tend to have more code on the server, rather than the client. That is, the 'look and feel' of your program is in the browser, but the smarts are on the server.
Web-based applications are dependant on the capabilities of the browser used to view them. Sadly, each browser has different capabilities, and many web developers have had to struggle with these differences as they designed their programs.
Once you receive the web page, it tends to be static. While there are ways you can update the page without returning to the server (that is, to make it dynamic), these make creating the page more complex. Therefore, animated forms (or any kind of responding to the user) are more difficult with web-based applications.
Many operations of a web-based application require a "network round-trip". This is due to the separation of code and design. In order to have a button or other control do something, it is often necessary to send information to the server. The server then responds as appropriate. You should keep this in mind when you are creating web applications. This back and forth communication might take a while, so you should only do it as necessary.
Web-based applications are limited, both by the limitations on the browser itself, and on the number of browsers in useavailable on the market. Browsers are limited in the type of controls that can be used, as well as by their limited drawing capabilities. In addition, if the user has installed an older version of a browser, or has disabled certain features, the web page may react in different ways. This is one of the main reasons that web-based applications tend to place most of the code on the server. In addition, this has meant that traditionally web-based applications have required a lot of code to be added to change the appearance of the web page based on the browser viewing it. Fortunately, the WebForm controls hide most of those details from you. They are written to produce dynamic output (that is, the page can change without needing to send information back to the server) if they detect the browser is capable of using it. If they do not recognize the browser being used, or if the browser would not support dynamic updating, only plain HTML is returned to the client browser. This ensures the developer that the client browser will receive the web page designed, within the limitations of the browser.
In addition to the limits caused by the browser, web-based applications also require the developer consider the fact that the client and server are separated, possibly by great_ distances by a network. This means that operations that might only take a few seconds_ if the client and server are close (or even the same machine) could take a long time. _So, operations like animations might be jerky, or not display at all until the download is completed. Also, the speed of the connection comes into play. If you are accessing the web page using a slower modem this difference is made even more significant.
With all these issues to keep in mind, you might ask 'Why bother creating web applications'? While there is a downside to creating web applications, there are many benefits:
Installation in order to make your application available, all you need to do is point someone at the URL. The application is immediately usable by the client. This saves you from having to visit each of those client machines, or from having your users all install the application.
New Versions and/or bug fixes when you want to upgrade to a newer version of part of your application, you only need to install the upgrades at the server, not at every client.
Performance the answers to how to improve performance of web applications are much easier than those for regular applications. You can improve performance by adding more servers, and distributing the requests across all the servers.
Knowledge if you already know some HTML, Web Applications can be much easier than Windows Applications. Alternately, they can be easier to learn if you know neither.
So, when designing an application, should you create a Windows-based or Web-based program? The easy (but unsatisfying) answer is, "It depends". Many applications fit either type, but more and more often, people are starting to create more Web-based applications. The ability to easily make upgrades and fixes available is compelling, so you might want to at least consider creating your programs as Web applications first.
Some applications are not candidates for Web applications, however. Any programs that require a continuous link between client and server are not appropriate, nor applications that require a lot of graphics (like games). Finally, if you only have a single computer involved (that is, not a client-server application like a database program), or if it is only for yourself, it may make sense to create a Windows-based application.