C# Web applications are based on the ASP.NET protocol (ASP is Active Server Pages, Microsoft's software that runs on the server and lets you create HTML to send back to the browser). You don't have to know ASP in order to create Web applications, and that's the beauty hereC# and the IDE will handle the details for you. You might be creating an application on the Internet, but the development process feels just as though you're writing a Windows application. The IDE even uploads your code to the Web server. However, there are many differences between Windows and Web applications, and you'll get a good feel for those differences in this chapter as well.
In order to develop Web applications, you'll need access to a Web server that runs Microsoft Internet Information Services (IIS) version 5.0 or later. IIS must be running on a Windows machine with the .NET Framework installed so your C# code can run. You can also use IIS locally, on the same machine as Visual Studio.
As with Windows applications, Web applications center on forms, but this time they're not Windows forms, they're Web forms .
Creating Web Forms
Unlike Windows applications, the user interface in Web applications is designed to appear in Web browsers, not in a standard Windows window. In Web applications, you display Web forms, which are based on the System.Web.UI.Page class (unlike Windows forms, which are based on the System.Windows.Forms.Form class), which are specially designed to appear in Web browsers. As far as your C# development goes, however, Web forms look and act very much like Windows forms.
Developing a Web application is much like developing a Windows application. You can still make use of all that the C# IDE offers, such as drag-and-drop development, IntelliSense prompts, what-you-see-is-what-you-get visual interface, project management, and so on. When you create a new Web application, a default Web form is added to the project, just as a Windows form is added to new Windows projects. You can populate that Web form with Web controls, not Windows controls. You can add Web controls to Web forms just as you can with Windows controlsyou just use the toolboxbut Web controls are different from Windows controls. To start, there are different types of Web controlsthose that run on the server, and those that run in the client (the browser).
Web Server Controls
Web server controls are the controls we'll work with for the most part. These controls are designed to appear and act much like Windows controls. They don't run in the browser, but in the server. When an event occurs, the browser has to send the Web page back to the server so the event can be handled by your C# code. The reason things work this way is because Web browsers differ greatly in their capabilities. To give you a more controlledand more powerfulprogramming environment for Web applications, you can run your code back on the server.
On the other hand, sending the Web page back to the server slows things down, so Microsoft has restricted the number of events that are available for Web server controls; some Web server controls can only handle Click events. Nor do Web forms handle mouse events such as MouseMove . There are no Web server scroll controls because the endless round-trips to the server would be too time consuming.
To add Web server controls to a Web form, you select the Web Forms tab in the toolbox. The advantage of Web server controls is that because their code runs on the server, you can use C# code with them. That means the C# programming we've already seen applies here too. To be able to run in Web browsers, Web server controls are actually made up of standard HTML controlsthe same kinds of controls you see in standard HTML pagesbut because C# often demands more functionality from a control than HTML controls can give, Web server controls are sometimes made up of a combination of standard HTML controls. Here are the Web server controls that ship with Visual Studio .NET:
Web server controls give you a lot of power in Web applications, but Microsoft also knows that users might expect to see the kind of standard HTML controls they normally see in Web pages. For that reason, C# also supports the standard HTML controls like HTML text fields (in HTML, text boxes are called text fields) and HTML buttons. When you want to add these controls to a Web form, you use the HTML tab in the toolbox. There are two kinds of HTML controls HTML server controls and HTML client controls .
HTML Server Controls
You can turn standard HTML controls into HTML server controls whose events are handled back at the server. To do that, you right-click a control and select the Run As Server Control item. When you do, you can handle such HTML server controls in C# code in your program by connecting event-handling code to them just as you would in Windows forms. Here are the HTML server controls:
HTML Client Controls
By default, when you add an HTML control to a Web form from the HTML tab in the toolbox, that control is a standard HTML control, an HTML client control in C#. HTML client controls are handled in the browser, out of the reach of C# code. If you handle events in the Web clientthe browserthe page doesn't have to make the round-trip to the server.
There's another type of control available as wellthe validation control .
Before sending user input back to the server, a validation control lets you test that input. For example, you can make sure that the user has entered an email address into the Web page's email text box. You can perform many tests using these controls, such as comparing what's been entered against a pattern of characters or numbers . We'll work with several of the validation controls in this chapter.
That completes the overview you need on the controls you can use in Web pages; it's time to start coding your own Web applications.