Introducing Web Applications

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.


IIS comes installed in some operating systems (like Windows 2000 Server), and it's on the CDs for some other systems (like Windows 2000 Professional). If you're going to use IIS locally, it should be installed before Visual Studio.

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).


The Web forms you create don't have to run in Microsoft's Internet Explorer. But if they don't, a number of features will be disabled.

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:

  • AdRotator Displays ad banners.

  • Button Displays a button.

  • Calendar Displays a calendar for choosing dates.

  • CheckBox Displays a check box.

  • CheckBoxList Displays a group of check boxes.

  • DataGrid Displays data in a table of columns .

  • DataList Displays data with more formatting options than a repeater control.

  • DropDownList Allows users to select items from a list or enter text directly.

  • HyperLink Displays a hyperlink.

  • Image Displays an image.

  • ImageButton Displays a clickable image.

  • Label Displays non-editable text.

  • LinkButton Displays a button that looks like a hyperlink.

  • ListBox Displays a list box.

  • Panel Lets you group other controls.

  • RadioButton Displays a radio button.

  • RadioButtonList Supports a group of radio buttons .

  • Repeater Displays information from a dataset using HTML elements.

  • Table Displays an HTML table.

  • TableCell Displays a cell in an HTML table.

  • TableRow Displays a row in an HTML table.

  • TextBox Displays a text box.

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:

  • HtmlAnchor Creates a hyperlink <a> element for navigation.

  • HtmlButton Creates an HTML button using the <button> element.

  • HtmlForm Creates an HTML form.

  • HtmlGenericControl Creates a basic control for an HTML element.

  • HtmlImage Creates an HTML <img> element.

  • HtmlInputButton Creates an HTML button using the <input> element.

  • HtmlInputCheckbox Creates an HTML check box.

  • HtmlInputFile Creates an HTML file upload control.

  • HtmlInputHidden Creates an HTML hidden control.

  • HtmlInputImage Creates an HTML button that displays images.

  • HtmlInputRadioButton Creates an HTML radio button.

  • HtmlInputText Creates an HTML text field. (You can also use this control to create password fields.)

  • HtmlSelect Creates an HTML select control.

  • HtmlTable Creates an HTML table.

  • HtmlTableCell Creates an HTML cell in a table.

  • HtmlTableRow Creates an HTML row in a table.

  • HtmlTextArea Creates an HTML text area (a two-dimensional text field).

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.

Because these controls run in the browser, you have to program them with a language the browser understands, such as JavaScript. We'll get to an example in a few pages.

There's another type of control available as wellthe validation control .

Validation Controls

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.

Microsoft Visual C#. NET 2003 Kick Start
Microsoft Visual C#.NET 2003 Kick Start
ISBN: 0672325470
EAN: 2147483647
Year: 2002
Pages: 181

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: