Creating a Web Application

To create Web applications, you need access to an installation of Microsoft's Internet Information Server (IIS) running on a server that must have the .NET Framework installed. Visual Studio will create the files you need and upload them directly to the server when you create the Web application (usually in the IIS Web root directory named wwwroot ).

USING IIS VIRTUAL DIRECTORIES

To avoid cluttering the Web root directory, you can create IIS virtual directories in which to store your Web applications. To support virtual directories, right-click a folder in the Windows Explorer and select Web Sharing.


To create a Web application, make sure IIS is running and, in the IDE, select the File, New, Project menu item to open the New Project dialog box you see in Figure 8.1. This time, select the ASP.NET Web Application icon in the Templates box, as you see in the figure. Enter the URL of your Web server in the Location box or browse to it by clicking the Browse button. If you're developing your Web applications with an installation of IIS on the same machine as Visual Studio, as in this example, the URL will be http://localhost . To create a Web application named ch08_01, we enter this location in the Location box: http://localhost/ch08_01 .

Figure 8.1. Creating a new Web application.

graphics/08fig01.jpg

OPENING AN EXISTING WEB APPLICATION PROJECT

To open a Web application that you've already created, use the File, Open, Project From Web menu item (not the File, Open, Project menu item). The IDE will ask for the URL of the server to use, and then will open the Open Project dialog box.


By default, a new Web form is automatically added to the new Web application, and that Web application opens in the IDE, as you see in Figure 8.2.

Figure 8.2. A Web application under design.

graphics/08fig02.jpg

Working with Web Forms

You can see the new Web form, WebForm1 , in the middle of Figure 8.2. What you're looking at is the visual representation of the Web form's file, WebForm1.aspx. Web forms like WebForm1 are based on the System.Web.UI.Page class , as you can see in the beginning of the actual code for WebForm1.aspx:

 
 namespace ch08_01 {   /// <summary>   /// Summary description for WebForm1.   /// </summary>  public class WebForm1 : System.Web.UI.Page  {     .     .     . 

The text in the Web form you see in Figure 8.2 indicates that the Web form is in grid layout mode , which means you can position controls where you want them in the Web form, just as you can in a Windows form.

You can set the Web form's layout yourself using the pageLayout property. Besides the grid layout mode, the other option is flow layout . Flow layout is the layout for controls that browsers usually use. With flow layout, the controls you add to a Web form "flow," much like the words in a word processor's page, changing position when the page changes size. To place your controls where you want them, use grid layout.

Now that you have the Web form open in the C# IDE, you can customize it much as you'd customize a Windows form. For example, you can set the Web form's background color using the properties window. This time, however, use the bgColor property, which corresponds in Web pages to the BackColor property of Windows forms. At design time, the properties window will display the HTML properties of the Web form you can work with. (In the IDE, HTML properties begin with a lowercase initial letter, such as bgColor and link .) You can set the foreground color (that is, the default color of text) used in Web forms and HTML pages with the text property, mirroring the attribute of the same name in HTML pages.

You can also set the text in the browser's title bar when the Web form is being displayed using the title property. And as in other Web pages, you can set the background image used in Web forms and HTML pages, using the background property. You can set this property to the URL of an image at runtime. Or, at design time, you can browse to an image file to assign to this property.

The Web form itself, WebForm1.aspx, is where the actual HTML that browsers will open is stored. You can see that HTML directly if you click the HTML button at the bottom of the Web form designer ( next to the Design button), as you see in Figure 8.3.

Figure 8.3. Using the HTML view.

graphics/08fig03.jpg

This is the HTML that a Web browser will see, and you can edit this HTML directly, as we'll do later in this chapter. Note the ASP directives in this document, which begin here with <%@ and <asp: . These ASP.NET directives will be executed by IIS, which will create HTML from them, and that's what is sent to the browser.

Let's add some Web server controls to this Web application. Click the Design button in the IDE to get back to the visual representation of the Web form, select the Web Forms tab in the toolbox, and then drag a button and a text box to the Web form. Give the button the caption Click Me using the properties window as you would in Windows applications. This adds a new System.Web.UI.WebControls.Button object and a new System.Web.UI.WebControls.TextBox object to WebForm1.aspx:

 
 namespace ch08_01 {   /// <summary>   /// Summary description for WebForm1.   /// </summary>   public class WebForm1 : System.Web.UI.Page   {  protected System.Web.UI.WebControls.Button Button1;   protected System.Web.UI.WebControls.TextBox TextBox1;  .     .     . 

As you've done in Windows applications, double-click the button to open its Click event handler in the "code-behind" file, WebForm1.aspx.cs, which is where your C# code for the Web application goes:

 
 private void Button1_Click(object sender, System.EventArgs e) { } 

Except for the fact that Button1 is capitalized hereall Web server names begin with an initial capitalthis is exactly what you'd see in a Windows application. Add this code to display a message, "Web Applications!" , in the text box:

 
 private void Button1_Click(object sender, System.EventArgs e) {  TextBox1.Text = "Web Applications!";  } 

Now run this new application just as you would any Windows application, by selecting the Debug, Start menu item. This makes Visual Studio upload your code to the Web server and launches Internet Explorer to display your Web application, as you see in Figure 8.4. When you click the Click Me button, the message "Web Applications!" appears in the text box, as you see in Figure 8.4.

Figure 8.4. Running a new Web application.

graphics/08fig04.jpg

Coding this application was remarkably like coding a Windows application. But there are still significant differences, as we'll see in the next section.



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

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