Chapter 1. Getting Familiar with the Development Environment

In this chapter, you are introduced to the Visual Studio .NET integrated development environment. You start by creating a simple Web Form Page. Creating this page will introduce you to the process of creating new solutions and projects with Visual Studio .NET.

Next, you are given an overview of the main Visual Studio .NET windows. You learn how to work with the Solution Explorer window, the Properties window, the Server Explorer window, and the Toolbox.

The bulk of this chapter is devoted to a discussion of the two Visual Studio .NET interfaces that you’ll use most often when building Web pages—the Designer and the Code and Text Editor. You’ll learn how to take advantage of all the tools and features included with the Code and Text Editor to build Web pages.

In this chapter, you will learn

  • How to create new ASP.NET Web Application projects with Visual Studio .NET

  • How to use the Visual Studio .NET Designer

  • How to use the Visual Studio .NET Code and Text Editor

Creating a Simple Web Forms Page

The best way to get familiar with the Visual Studio .NET environment is to build a simple Web Form Page. In this section, we’ll create a page that dynamically displays the current date and time.

Whenever you start building an application with Visual Studio .NET, you should begin by creating a new project. Visual Studio .NET supports a number of different project types. When building Web Form Pages, you need to create an ASP.NET Web Application project. To do so, perform the following steps:

  1. If you haven’t already launched Visual Studio .NET, open it now by pointing to the Start button on the Microsoft Windows taskbar, pointing to Programs, and then clicking the Microsoft Visual Studio .NET icon.

  2. After Visual Studio .NET opens, select New Project from the File menu. (Alternatively, you can click the New Project button from the Start Page.)

  3. In the New Project dialog box, select either C# or VB.NET as the language for the project by selecting the Visual C# Projects folder or the Visual Basic Projects folder in the Project Types pane.

  4. Select ASP.NET Web Application in the Templates pane.

  5. Enter the path http://localhost/myWebApp in the Location text box.

  6. Click OK.

After you complete these steps, your hard drive will make some noises as the necessary files for your project are created. When all the files are created, a new Web Form Page named WebForm1.aspx will appear.

Immediately after you create a new ASP.NET Web Application project, you are in Design View. While in Design View, you can visually design a Web Form Page by dragging and dropping elements onto the page from the Toolbox.

We’ll add a Web Form Label to the page that we’ll use to display the current date and time. To do so, perform the following steps:

  1. Open the Toolbox by selecting Toolbox from the View menu.

  2. In the Toolbox, click the tab labeled Web Forms.

  3. From under the Web Forms tab, drag a Label onto the Designer surface (the main window) with your mouse.

Next, we need to add code to our Web Form Page that displays the current date and time in the Label. To add code, we need to switch to the Code Editor by doing the following:

Procedure 1.1. C# Steps

  1. Double-click the Designer surface (the main window) to switch to the Code Editor. (Alternatively, you can select Code from the View menu.)

  2. Add the following code for the Page_Load() method:

    private void Page_Load(object sender, System.EventArgs e)
    {
      Label1.Text = DateTime.Now.ToString();
    }
    

Procedure 1.2. VB.NET Steps

  1. Double-click the Designer surface (the main window) to switch to the Code Editor. (Alternatively, you can select Code from the View menu.)

  2. Add the following code for the Page_Load() subroutine:

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles
    VB.NET Steps MyBase.Load
      Label1.Text = DateTime.Now
    End Sub
    

Finally, we can compile the project and open the Web Form Page in a browser by performing the following steps:

  1. Switch back to the Design View of the Web Form Page by selecting the Designer from the View menu.

  2. Enter the keyboard combination Ctrl+F8 to build and browse the page.

When the page opens in the browser integrated into Visual Studio .NET, you should see the page shown in Figure 1.1.

A simple Web Form Page.

Figure 1.1. A simple Web Form Page.

We moved pretty quickly through each of these steps. In the following sections of this chapter, you’ll learn more about working with projects, the Visual Studio .NET windows, the Designer, and the Code Editor.

Working with Solutions and Projects

When creating our simple Web Form Page in the previous section, the first thing we did was create a new project. A project contains all the files for a particular application.

You can create several different types of projects with Visual Studio .NET. For example

  • ASP.NET Web Application Project—Contains files for creating an ASP.NET Web site

  • Windows Application Project—Contains files for creating a desktop Windows application

  • ASP.NET Web Services Project—Contains files for creating an XML Web service

  • Web Control Library Project—Contains files for creating custom ASP.NET Web Forms controls

  • Class Library Project—Contains files for creating classes that can be shared across multiple projects

For the most part, we’ll be sticking to creating ASP.NET Web Applications in the first part of this book. We’ll need to create other types of projects when building such things as components and custom controls.

Creating Projects

When you create a new ASP.NET Web Application, a new Web is created on your Web server. For example, when we created the myWebApp project in the first section of this chapter, a new Web was created in the wwwroot directory of the local machine.

You can develop a Web application on either your local machine or on a remote Web server. If you want to create a new project on a remote Web server, you’ll need to provide the address of the remote Web server in the New Project dialog box when creating the project. So, instead of entering http://localhost/myWebApp, you would enter something like http://somesite.com/myWebApp.

When you create a new project, files for the project are automatically created in two places. One set of files is created on the Web server, and one set of files is created on the local machine.

First, a new folder containing the project files is created on your Web server (even when your Web server is located on the same machine as Visual Studio .NET). For example, if you enter http://localhost/myWebApp for the location of your project, a new folder named myWebApp is created in the root directory of your Web server (for example, c:inetpubwwwrootmyWebApp).

Second, a local copy of all the files in your project are kept by Visual Studio .NET in a special folder named VSWebCache. You can find the VSWebCache folder under the Documents and Settings folder (for example, c:Documents and Settingsuser loginVSWebCachemachine name).

Visual Studio .NET automatically creates two copies of a project so that you can efficiently work with project files even when you are working with a remote Web server. Normally, Visual Studio .NET takes care of all of the work of synchronizing the two sets of project files for you (but see the “Working with Projects Offline” section later in this chapter).

When a new project is created, two files are created on your Web server that contain information about your project. When creating a C# project, two files with the extensions .csproj and .csproj.webinfo are created. When you create a VB.NET project, two files with the extensions .vbproj and .vbproj.webinfo are created.

The .csproj or .vbproj file contains a list of all the items in your project along with some configuration information. The .csproj.webinfo or .vbproj.webinfo files contain the path to the .csproj or .vbproj file on the Web server.

If you want to open a project that you have previously created, you can open either the .csproj or .vbproj files. If the project is located on the local machine, you can open the project by opening the File menu, pointing to Open, and selecting Project. If the project is located on a remote server, you can select Project from Web on the Open submenu. However, you won’t usually want to open a project directly. Typically, you will want to open a Solution that contains your project (see the next section, “Creating Solutions”).

Creating Solutions

When you create a new project, a Solution is automatically created that contains the project. For example, when we created the myWebApp project in the first section of this chapter, a new Solution with the same name (myWebApp) was automatically created.

A Solution is a container for projects. It is not uncommon to need to work with multiple projects at the same time. For example, you might need to develop a custom Web Forms control while developing an ASP.NET Web Application. In that case, you would want to add both projects to the same Solution.

Information about a Solution is stored in two files with the extensions .sln and .suo. These files are not stored within a project. By default, they are stored on your local computer in the My Documents, Visual Studio Projects directory.

Tip

You can change the default location where your Solutions are stored by selecting Options from the Tools menu and clicking the Environment, Projects, or Solutions folder.

You can open a Solution that you have previously created by selecting Open Solution from the File menu. To open a Solution, browse to a .sln file and open it.

Tip

When you create a new project, a Solution with the same name is automatically created. If you want to control the name of a Solution, you need to create a Blank Solution and then add projects to it. To create a Blank Solution, open the File menu, point to New, and select Blank Solution.

Accessing Project Files

Visual Studio .NET supports two methods of accessing and modifying files located on a Web server. You can access project files by using FrontPage Server extensions, or you can access files directly through a file share.

The advantage of accessing project files through a file share is that this method is typically faster than using FrontPage extensions. However, to use file share access, your Web server must be located on the same local network as the computer running Visual Studio .NET. In other words, the file share method won’t work when working with a Web server located at a remote ISP.

There are two ways of configuring project access. First, you can specify a default access method for all new projects by performing the following steps (see Figure 1.2):

Selecting a project access method.

Figure 1.2. Selecting a project access method.

  1. Select Options from the Tools menu.

  2. Select the folder labeled Projects.

  3. Select Web Settings.

  4. Choose either the File share or FrontPage Extensions option button.

You can also change the project access method for an existing project by performing the following steps:

  1. Right-click the project in the Solution Explorer window and select Properties.

  2. Select the folder labeled Common Properties.

  3. Display the Web Settings page.

  4. Choose either File Share or FrontPage from the Web Access Mode drop-down list.

  5. If you select File Share, you’ll need to supply the path to the Web server directory in the Path text box.

  6. Select Unload Project from the Project menu.

  7. Select Reload Project from the Project menu.

If you want to use FrontPage Server Extensions to access files on a Web server, the Web server must be configured with FrontPage Server Extensions. Perform the following steps to launch the Server Extensions Configuration Wizard for Internet Information Server 5.0 on Windows 2000:

  1. On the Web server on which you want to create your project, launch Internet Services Manager by clicking the Start button on the Microsoft Windows taskbar, pointing to Settings, Control Panel, Administrative Tools, and clicking Internet Services Manager.

  2. In the tree menu, navigate to the node representing your default Web site.

  3. From the Action menu, point to All Tasks, and select Configure Server Extensions. (If this option is unavailable, but the option Check Server Extensions is available, the FrontPage Server Extensions are already installed.)

After you complete the Server Extensions Configuration Wizard, you can create new projects on the Web server by using the FrontPage Extensions access method with Visual Studio .NET.

Note

To use FrontPage Extensions, you must have authentication enabled for your Web server. You can enable Windows authentication by opening the property sheet for your Web site in the Internet Services Manager, clicking the Directory Security tab, clicking the Edit button, and selecting the check box labeled Integrated Windows authentication.

Upgrading Existing Applications to Visual Studio .NET

Most likely, you have existing Web applications that you have developed with development environments other than Visual Studio .NET. For example, before upgrading to Visual Studio .NET, you might have used Microsoft’s Visual InterDev or FrontPage, Macromedia’s Dreamweaver or HomeSite, or even Notepad.

How do you take an existing Web application and use Visual Studio .NET to edit it? You have two choices—migrate or upgrade.

The first choice is to migrate an existing Web application to a new Visual Studio .NET application. In this case, you create a new Visual Studio .NET application on your Web server and then you copy existing files to the new application. To do this, perform the following steps:

  1. Create a new ASP.NET Web Application project by opening the File menu, pointing to New, and selecting Project. In the New Project dialog box, select Visual C# Projects (or Visual Basic Projects) in the Project Types pane, and then select the ASP.NET Web Application template. Create a new path for your project on the Web server by entering the path in the Path text box (for example, http://localhost/newApp). Click OK.

  2. Right-click the name of your new project in the Solution Explorer window, point to Add, and select Add Existing Item.

  3. Navigate to the folder containing your old Web application.

  4. Select All Files (*.*) from the Files of Type drop-down list. Selecting this option will display all files, including HTML files.

  5. Select the files that you want to migrate. You can select multiple files at a time by holding down the Shift or Ctrl key when selecting files.

  6. Click Open.

  7. The old Web application files should now appear within the Solution Explorer window.

The advantage of using this first method of migrating an old application to a new Visual Studio .NET application is that you automatically get all the supporting ASP.NET Web application files. When you create a new project, all the configuration files are created for you.

The disadvantage of this first method is that it requires you to create a new location for your Web application. You must create a new folder on your Web server and migrate all of the old application files to it.

Instead of creating a new folder on your Web server, you can create a new project in an existing folder. To do this, perform the following steps:

  1. Create a new project by opening the File menu, pointing to New, and selecting Project. In the New Project dialog box, select either C# or Visual Basic Projects in the Project Types pane and then select the New Project in Existing Folder template.

  2. Enter the name myApp for the project and click OK. Next, enter the path to your existing application in the Folder location text box (for example, http://localhost/oldApp). Click OK.

  3. Right-click the name of your new project in the Solution Explorer window, point to Add, and select Add Existing Item.

  4. Navigate to the folder containing your old Web application.

  5. Select All Files (*.*) from the Files of Type drop-down list. Selecting this option will display all files, including HTML files.

  6. Select the files that you want to migrate. You can select multiple files at a time by holding down the Shift or Ctrl key when selecting files.

  7. Click Open.

  8. The old Web application files should now appear within the Solution Explorer window.

Note

The Standard Edition of Visual Studio .NET does not support the New Project in Existing Folder project template.

The disadvantage of using the New Project in Existing Folder template to upgrade an application is that it does not automatically create the supporting ASP.NET Web application configuration files for you. For example, if you need to use the Web.Config file, you need to manually add the file yourself.

Note

What about Visual InterDev Design Time Controls (DTCs)? These are no longer supported in Visual Studio .NET. You’ll need to reproduce their functionality by using either Web server controls or Web User Controls.

Working with Projects Offline

Imagine that you are called to a meeting in Paris to demonstrate your newest Web application. You book a flight on the Concorde and get ready to go. Unfortunately, you remember at the last minute that your application has one little bug. What do you do?

Visual Studio .NET supports building Web applications offline. You can create a local copy of the files from a Web application on your local machine, modify the files, and then resynchronize the files with the Web server when you go online again. Consequently, you can leap on the Concorde with your laptop, make the modifications in the air, and update the Web server when you land in Paris.

To switch a project to offline mode, open the Project menu, point to Web Project, and select Work Offline (see Figure 1.3). When you do this, all the files from the Web server are copied to your computer. By default, the files are added to the VSWebCache folder on your computer.

Selecting offline mode.

Figure 1.3. Selecting offline mode.

While working offline, you can continue to open the files in your application in a Web browser. You’ll notice that the files are no longer served from your Web server. They are opened from an offline folder. To use offline mode, you need to have Internet Information Services running on your local computer.

After you have connected to the network, you can go back online by opening the Project menu, pointing to Web Project, and unchecking Work Offline. Your files should be automatically resynchronized with the files on the remote Web server. When you are back online, you can also manually resynchronize files by opening the Project menu, pointing to Web Project, and selecting Synchronize All Folders.

Overview of Visual Studio .NET Windows

In this section, you’ll learn about the major user interface elements of the Visual Studio .NET environment. You’ll receive a quick tour of the windows and objects that appear when you are working on a Web application project. For example, you’ll learn how to create new files, use the Toolbox, and explore resources available on your network.

There are four main windows that you’ll use when building projects in Visual Studio .NET (see Figure 1.4). By default, these windows are displayed when you first launch Visual Studio .NET:

Visual Studio .NET windows.

Figure 1.4. Visual Studio .NET windows.

  • Solution Explorer—Displays all the projects and files contained within the current solution

  • Properties Window—Displays a list of properties for the currently selected object

  • Toolbox—Displays a list of items that you can add to a page

  • Server Explorer—Displays server resources available on your local network, such as databases, event logs, and performance counters

You can open any of these windows by selecting the menu options located in the View menu. You can also open a particular window by clicking the corresponding icon for the window on the Standard Toolbar (located at the top-right of the screen).

Tip

You can reset Visual Studio .NET to its default window layout by selecting Options from the Tools menu and displaying the General page in the Environment folder. Click the button labeled Reset Window Layout.

Using Auto Hide

Before discussing the contents of each window, you should know about a new feature of Visual Studio .NET called auto hide. When auto hide is enabled for a window, the window is automatically minimized when you are not working with it.

Windows that support auto hide have a pushpin icon on their title menu. When the pushpin is oriented horizontally, auto hide is enabled. When the pushpin is oriented vertically, auto hide is disabled. To enable or disable auto hide for a window, simply click the pushpin.

Tip

You can enable auto hide for every window by selecting Auto Hide All from the Window menu. This is useful for instantly freeing up screen real estate.

When auto hide is enabled, the window is automatically hidden on the edge of the development environment when the mouse is not hovering over it. A tab represents the minimized window. To expand a hidden window, you can hover your mouse over the tab (see Figure 1.5).

Using auto hide.

Figure 1.5. Using auto hide.

Using the Start Page

When you first launch Visual Studio .NET, you are presented with the Start Page (see Figure 1.6). The Start Page contains several sections that contain such information as the latest Visual Studio .NET headlines from Microsoft and links to various online resources. To get the most from the Start Page, you should open the page while connected to the Internet.

The Visual Studio .NET Start Page.

Figure 1.6. The Visual Studio .NET Start Page.

Tip

You can open the Start Page at any time by selecting Show Start Page from the Help menu.

You have a couple of options for customizing the Start Page. For example, if you don’t want the Start Page to appear when you open Visual Studio .NET, you can have Visual Studio .NET automatically display the last opened solution at startup, display a dialog box for opening an existing project or creating a new project, or display an empty environment.

To modify the startup behavior of Visual Studio .NET, do the following:

  1. Select Options from the Tools menu.

  2. Select the Environment, General folder.

  3. Select a startup option from the drop-down list labeled “At startup:”

You can also set a custom startup page. For example, you can have Visual Studio .NET display the home page for the www.Superexpert.com Web site whenever it first opens By doing the following:

  1. Select Options from Tools menu.

  2. Select the Environment folder.

  3. Display the Web Browser page.

  4. Next to Home Page, uncheck the Use Default check box.

  5. Enter http://www.Superexpert.com in the Home page text box.

  6. Click OK.

When you next open Visual Studio .NET, the home page of the Superexpert Web site will be displayed by default.

Using the Solution Explorer Window

The Solution Explorer is the main window for manipulating the files and folders in your application. By default, this window is located at the top-right of Visual Studio .NET.

If the Solution Explorer window is not open, you can open it by choosing Solution Explorer from the View menu. You can also open this window by clicking the Solution Explorer icon on the Toolbar (see Figure 1.7).

Displaying the Solution Explorer window.

Figure 1.7. Displaying the Solution Explorer window.

Adding Files and Folders to a Project

You can add files and folders to a project by right-clicking an existing project or folder in the Solution Explorer window and selecting a type of item to add. For example, to add a new folder to your Web application, you would right-click an existing project and choose Add, New Folder.

Tip

You can alternatively add new items to a project by selecting a new item to add from the Project menu.

You can also use Solution Explorer to add already existing items to a project. For example, if you want to add a cool GIF image that you created with Photoshop to your application, you would right-click the folder to which you want to add the image file and select Add, Add Existing Item. Next, you would browse to your image on your local hard drive, select it, and click OK to copy the image to the Web application.

An alternative method of adding existing items to Solution Explorer is to simply drag and drop the item from your desktop into the Solution Explorer window. If you prefer to not drag and drop the item, you can also right-click the item on your desktop, click Copy, right-click a folder in Solution Explorer, and click Paste.

Copying and Moving Items in a Project

Suppose that you need to move a file in your application from one folder to another. There are two ways that you can move items with Solution Explorer. First, you can simply drag and drop the item from one folder to another. To do this, just click the item, keep your mouse button pressed, and move your mouse until the mouse cursor hovers over the new folder. When you release the button, the item is copied.

Alternatively, you can right-click an item, choose Cut, right-click the new folder, and choose Paste. If you want to create a new copy of an item, simply choose Copy instead of Cut when selecting the item.

Displaying All Items in a Solution

By default, all the files and folders contained in a Solution are not displayed in the Solution Explorer window. To display all the items in Solution Explorer, choose Project, Show All Files.

When you show the hidden files, several new files and folders should appear in Solution Explorer. For example, both the Bin and Images directories appear. Also, the code-behind files for any Web Form Pages are explicitly displayed (see Figure 1.8).

Displaying hidden files and folders.

Figure 1.8. Displaying hidden files and folders.

You can also display and hide files by clicking the Show All Files icon on the Solution Explorer toolbar.

Copying a Project

You do not usually develop a Web application on a production Web server (unless your budget is extremely tight). Typically, you develop a Web application on a development server and then, when all the bugs are eliminated, you copy the finished application to the production server.

After you have completed a solution with Visual Studio .NET, you can copy the solution from your development server to your production server by using the Copy Project dialog box. You can open this dialog box by selecting Project, Copy Project.

You can also open the Copy Project dialog box by clicking the Copy Project icon located on the Solution Explorer toolbar.

Before you can copy a project, you need to supply a path to the destination server. You can supply any URL in the Destination Project Folder text box. If you are copying the project to a file share, you can enter the path to a file share.

The Copy Project dialog box enables you to select the particular files that you want to copy to the production server (see Figure 1.9). Normally, you do not want to copy the source code or project files to your production server. You can use the Copy Project dialog box to copy only those files that are needed for your Web application to execute.

Copying a project to a new Web server.

Figure 1.9. Copying a project to a new Web server.

Using the Toolbox

The Toolbox contains a set of commonly used controls, tags, and components that you can add to your applications. By default, the Toolbox is located on the left side of the Visual Studio .NET environment below the Server Explorer window.

If the Toolbox isn’t visible, you can display it by selecting Toolbox from the View menu. Alternatively, you can open the Toolbox by clicking the Toolbox icon on the Toolbar (see Figure 1.10).

Opening the Toolbox.

Figure 1.10. Opening the Toolbox.

The items in the Toolbox are organized under different tabs. The Toolbox displays different tabs depending on the type of application you are building. For an ASP.NET Web Application, the Toolbar contains the following tabs:

  • Data—Contains classes related to data access. These Toolbar items are discussed in the second part of this book, “Working with Database Data.“

  • Web Forms—Contains the standard Web Form controls. These controls are discussed in Chapter 3, “Creating Basic Web Form Pages,” and Chapter 4, “Validating Web Form Pages.”

  • Components—Contains components for performing advanced tasks, such as working with event logs and performance counters. These components are not discussed in this book.

  • HTML—Contains items that represent the most common HTML tags. These items are discussed in the next chapter, “Using the Visual Studio .NET Designer.”

  • Clipboard Ring—Contains the last 12 items added to the system clipboard ring using the Copy or Cut command. For example, when you copy code from the Code Editor, it is automatically added to this tab.

  • General—Contains any custom items or controls you want to add to your project.

Controlling How Toolbox Items Are Displayed

By default, the items under each tab in the Toolbox are displayed in a list. If you have a lot of items to display beneath a particular tab, you might want to disable list view (see Figure 1.11). To disable list view, right-click the Toolbox and uncheck List View.

List view disabled.

Figure 1.11. List view disabled.

You can also arrange the order in which the items in the Toolbar are displayed. If you simply want to display the items in alphabetical order, right-click the Toolbar and select Sort Items Alphabetically.

If you want more fine-grained control over the order of the items in a Toolbar, you can arrange each item individually. Right-click a particular item and select either Move Up or Move Down. You can also drag and drop an item from one position on the Toolbar to another.

Adding New Items to the Toolbox

You can add a variety of different items to the Toolbox including new Web Server controls, components, and even HTML content and code snippets.

For example, imagine that you have just downloaded a new Web server control that you want to use in your applications. You can add this control to the Toolbar by right-clicking the Toolbar and selecting Customize Toolbox (alternatively, you can select Customize Toolbox from the Tools menu).

When you select Customize Toolbox, the dialog box in Figure 1.12 appears. This dialog box contains two tabs—a tab for adding COM components and a tab for adding .NET Framework components. Choose the .NET Framework Components tab and click the Browse button to browse to the Web server control on your computer.

Customizing the Toolbox.

Figure 1.12. Customizing the Toolbox.

After you click Open, the control should appear in the list of .NET Framework components. Select the check box next to the component and click OK, the new control should now appear in the Toolbox, just like all the standard controls.

Adding New Tabs to the Toolbox

You can extend the Toolbox with your own custom tabs. For example, you might want to add a new tab named My Favorite Controls that contains only third-party Web server controls. To add a new tab, right-click the Toolbar and select Add Tab. Next, enter a name for the new tab.

You can remove a tab that you have added by right-clicking the tab in the Toolbar and selecting Delete Tab. You can also right-click the tab and select Move Up or Move Down to modify the order of the tabs in the Toolbar.

Using the Server Explorer

You can use the Server Explorer window to browse the servers and databases available on your network. By default, the Server Explorer window is located at the top left of the Visual Studio .NET environment (see Figure 1.13).

The Server Explorer window.

Figure 1.13. The Server Explorer window.

If the Server Explorer window is not visible, you can open it by selecting Server Explorer from the View menu. You can also display the Server Explorer window by clicking the Server Explorer icon on the Toolbar.

You can use Server Explorer to view information about all the servers located on your local network. For example, you can use Server Explorer to view server event logs, performance counters, message queues, and database servers.

Note

The Standard Edition of Visual Studio .NET does not support browsing server information. The Servers node does not appear in the Server Explorer window.

To add a new server to the Server Explorer window, perform the following steps:

  1. Right-click Servers in the Server Explorer window and select Add Server.

  2. Enter the name or IP address of the server to which you want to connect (sadly, you cannot enter a domain name for a server on the Internet).

  3. Click OK.

After you complete these steps, a new server should appear under the Servers node in the Server Explorer window.

Using the Properties Window

You use the Properties window to set property values for the controls and components that you add to a page. When you first create an ASP.NET Web Application project, the Properties window displays all the properties for the WebForm1.aspx page.

You can use the drop-down list that appears at the top of the Properties window to select a particular object. For example, if you add a label to a page, you can use the drop-down list to select the Label and modify its properties.

Notice that there are several buttons that appear at the top of the Properties window. You can use the Alphabetic button to sort the properties that appear in the Properties window alphabetically. To sort the properties by category, click the Categorized button.

Using the Visual Studio .NET Designer

When you create a new page in Visual Studio .NET, the page opens in the Designer interface. You use the Designer when building the visual portion of your Web pages. The Designer supports two views—Design View and HTML View.

While in Design View, you can build a Web page by dragging and dropping elements from the Toolbox. For example, when building the simple Web Form Page at the beginning of this chapter, we added a Label to the page in Design View by dragging and dropping the Label from the Toolbox.

If you prefer to work directly with HTML tags, you can switch to HTML View. While working in HTML View, you can type HTML and ASP.NET tags directly into the HTML editor.

There are a number of ways to switch between the Design View and HTML View of a page:

  • Click the Design View or HTML View tab that appears at the bottom left corner of the Designer interface.

  • Press Ctrl+PgDn to switch back and forth between Design View and HTML View.

  • Right-click the window and select View HTML Source or View Design.

  • Select Design from the View menu, or select HTML Source from the View menu.

Tip

By default, when you open a new page, it appears in Design View. If you want new pages to automatically open in HTML View, select Options from the Tools menu. Navigate to the HTML Designer folder and display the General page. Finally, select HTML View for the page types that you want to open in HTML View.

We’ll discuss how to build pages using both of the views available in the Designer in the following sections.

Using the Visual Studio .NET Code and Text Editor

You’ll spend more than 50 percent of your time while developing applications with Visual Studio .NET working with the Code and Text Editor. You’ll use the Code Editor to enter all of your C# or Visual Basic .NET code for your ASP.NET application. Because this is such an important tool, it’s worth examining how to use it in detail.

To discuss the features of this editor, we’ll need to open a code file in Visual Studio .NET. Perform the following steps:

  1. If you don’t already have an ASP.NET Web Application project open in Visual Studio .NET, create one. (For instructions on creating new projects, see the “Creating a Simple Web Form Page” section in the first part of this chapter.)

  2. Select Add Web Form from the Project menu. Enter the name TestCode.aspx and click Open.

  3. Switch to the Code and Text Editor by double-clicking the Designer surface.

After you complete these steps, the Code Editor should appear (see Figure 1.14). Don’t worry about the actual code that appears in the Code Editor; we’ll discuss the contents of the Code Editor later in this book in Chapter 3.

The Visual Studio .NET Code and Text Editor.

Figure 1.14. The Visual Studio .NET Code and Text Editor.

Taking Advantage of IntelliSense

When you use the Code and Text editor, you can take advantage of IntelliSense. IntelliSense encompasses a number of features that make it easier to enter code while you type (see Figure 1.15).

Using IntelliSense.

Figure 1.15. Using IntelliSense.

Some of the important features of IntelliSense include the following:

  • Automatic Statement Completion—If you type the first part of a statement, the rest of the statement is completed for you.

  • View Class Members—If you type the name of a class or the name of an instance of a class, followed by a period, all the methods and properties of the class are automatically listed in a drop-down list.

  • View Parameter Information—If you type the name of a method or function, all of the possible parameters to the method or function are listed in a drop-down list.

  • View Variable Declarations—If you hover your mouse over any variable, the declaration for the variable appears in a ToolTip.

  • Automatic Brace Matching—If you type an opening brace such as a (, {, or [, the matching ending brace is displayed.

Note

You can disable IntelliSense statement completion by selecting Options from the Tools menu. Navigate to the Text Editor folder, and then the All Languages folder, and display the General page. In the Statement Completion section, uncheck the features that you don’t want.

To practice using IntelliSense, perform the following steps:

  1. In the Code Editor, find the comment Put User Code to Initialize the Page Here.

  2. Enter a new line below the comment and enter the text Response followed by a single period. A list of all the methods and properties of the Response object should appear (see Figure 1.15).

  3. Select the Write method from the drop-down list. You can select the Write method either by typing W, or you can use the up and down arrow keys.

  4. If you press the Tab key, the text Response.Write is added to the Code Editor. Press the Tab key.

  5. Next, enter an opening parenthesis ( and a list of possible parameters should appear. You can use the up and down arrows to view all the possible parameters for the Write method.

You can use the following keyboard shortcuts with IntelliSense:

  • Alt+Right Arrow—Invokes automatic statement completion after you type the first few letters of the statement

  • Ctrl+J—Invokes the List Members feature listing all the methods and properties of a class

  • Ctrl+Shift+Space—Invokes the Parameter Info features listing all the parameters for a method or function

  • Ctrl+K+I—Invokes the Quick Info feature displaying the complete declaration for any variable

By taking advantage of IntelliSense, you can write code more efficiently. One of the major reasons that people use Visual Studio .NET is to take advantage of IntelliSense!

Navigating Code

The longer your code gets, the more difficult it is to find a particular section of code. Visual Studio .NET supplies a number of tools for quickly navigating to a particular section in the Code Editor.

Using the Navigation Bar

The Navigation Bar appears at the top of the Code Editor. It contains two drop-down lists that enable you to quickly navigate to a previously declared method in your code.

When working with a C# project, the top-left drop-down list contains a list of types in the current page. The top-right drop-down list contains a list of members. For example, you can quickly navigate to a method by selecting a method from the Members drop-down list.

In a VB.NET project, the top-left drop-down list is labeled Class Name and the top-right drop-down list is labeled Method Name. You can navigate to an existing function or subroutine or add a new one by selecting the appropriate method name from the top-right drop-down list.

Moving to a Line Number

If you receive an error when executing an application and you know the line number of the error, you will need to quickly get to the right line of code in the Code Editor. To do this, you can use the Edit, Go To menu option. A dialog box appears that enables you to enter a line number.

Moving to a Definition

You can right-click a variable, method, function, or subroutine and navigate directly to its declaration. To do this, right-click the item in the Code Editor and select Go to Definition.

Searching for Text

Visual Studio .NET supports both basic and advanced text searching features. You can use the Find command to simply perform a search for text in the current document. The Find command also supports several advanced features, such as the ability to search through multiple documents, support for regular expression matches, and support for incremental searches.

To perform a basic search, simply enter your search text in the Find text box on the toolbar. Press Enter and your document will be searched.

To perform more advanced searches, open the Edit menu, point to Find and Replace, and select Find (see Figure 1.16). To find text in external files, open the Edit menu, point to Find and Replace, and select Find in Files (you can also click the Find in Files icon on the toolbar).

Opening the Find dialog box.

Figure 1.16. Opening the Find dialog box.

Finally, you can perform incremental searches. In an incremental search, text is matched as you type it. To perform an incremental search, open the Edit menu, point to Advanced, and choose Incremental Search. After you select this option, the cursor icon appears as a set of binoculars. If you enter text, the text is matched in the editor code document as you type.

Using Bookmarks

You can bookmark any location in a code document and return to it later. To add a bookmark, click the bookmark icon on the toolbar (it looks like a blue flag). After you add a bookmark, a light blue box appears next to the code line (see Figure 1.17).

Adding a bookmark.

Figure 1.17. Adding a bookmark.

To return to a bookmark, you can use one of two additional bookmark icons on the toolbar. The Move the Caret to the Next Unnamed Bookmark icon navigates to the next bookmark in the document. The Move the Caret to the Previous Unnamed Bookmark icon navigates to the previous bookmark in the document.

Outlining Code

As you type methods, functions, and subroutines in the Code Editor, you might notice that plus and minus icons automatically appear on the left margin of the Code Editor. You can use these icons to collapse and expand particular areas of your document.

You can use this code outlining feature for any section of code. For example, you might write a long section of code that does some complicated text parsing. You might find it convenient to hide this section of code while you work on other sections of code in the document.

Warning

Hiding sections of code works better with VB.NET than C#. Before you can hide a region of code with C#, you need to select Edit, Outlining, Stop Outlining. Be warned that selecting this option will disable all the automatic outlining.

To hide a section of code, perform the following steps:

  1. Select the section of code you want to hide with the mouse.

  2. Open the Edit menu, point to Outlining, and select Hide Selection.

After you hide a section of code, you can make it reappear by clicking the plus icon next to it.

Customizing the Code Editor

Visual Studio .NET provides you with several options for customizing the Code Editor. For example, if you happen to want to use a yellow script font on a blue background, you can modify the Code Editor to match your preferences.

In the following sections, you’ll learn how to control how multiple Code editor windows appear, modify the Code Editor font, display line numbers, and display the Code Editor in full-screen mode.

Selecting Tab or Multiple Document Interface Mode

By default, when you open a new document to edit with the Code Editor, a new tab appears at the top of the Code Editor. You can switch between open windows by clicking the tabs. The default view mode for Visual Studio .NET is called Tabbed Documents mode.

If you prefer, you can use Multiple Document Interface (MDI) mode instead. When MDI mode is enabled, you can tile multiple windows on the screen at the same time (see Figure 1.18).

Viewing the Code Editor in MDI mode.

Figure 1.18. Viewing the Code Editor in MDI mode.

To switch from Tabbed Documents mode to MDI mode, perform the following steps:

  1. Select Options from the Tools menu.

  2. Navigate to the Environment folder and display the General page.

  3. Under Settings, check the MDI Environment check box.

This change does not take effect until you shut down and restart Visual Studio .NET.

Modifying the Code Editor Font

You can use any font, foreground color, and background color with the Code Editor that you want. For example, Figure 1.19 shows the Code Editor with a blue background and a 24pt Script font.

Customizing the Code Editor font.

Figure 1.19. Customizing the Code Editor font.

To change the font and background color of the Code Editor window, perform the following steps:

  1. Select Options from the Tools menu.

  2. Navigate to the Environment folder.

  3. Display the Fonts and Colors page.

  4. Choose Text Editor in the Show Settings For drop-down list.

  5. Pick any font and color options you want.

If you want to return these options to their default values, click the Use Defaults button.

Tip

In general, it’s a good idea to stick with monospaced fonts, such as Courier New, with the text editor. Because the characters in a monospaced font have a fixed width, code written with a monospaced font is easier to read. Monospaced fonts appear in bold in the font list.

Displaying Line Numbers

You can display line numbers in the left margin of the Code Editor (see Figure 1.20). To enable line numbering, perform the following steps:

Displaying line numbers.

Figure 1.20. Displaying line numbers.

  1. Select Options from the Tools menu.

  2. Navigate to the Text Editor folder.

  3. Navigate to the All Languages folder.

  4. Display the General page.

  5. Check the Line Numbers check box.

After you complete these steps, line numbers will appear in the Code Editor window.

Note

Enabling line numbers does not cause line numbers to be printed when you print the code in the code window. There is a separate check box on the Page Setup dialog box (select Page Setup from the File menu) that enables or disables line numbers when printing.

Displaying the Code Editor in Full-Screen Mode

If you need to settle down and write a long page of code, it’s nice to be able to switch to full screen mode. In full-screen mode, all the windows and toolbars are hidden except for the Code Editor window (see Figure 1.21).

Using full-screen mode.

Figure 1.21. Using full-screen mode.

To enable full-screen mode, press the key combination Alt+Shift+Enter. You can return to normal mode either by pressing Alt+Shift+Enter again or by clicking the full-screen icon in the full-screen window.

Using the Task List Window

In this section, you’ll learn how to take advantage of the Task List window (see Figure 1.22). The Task List window can be used with the Code Editor to track several categories of tasks. For example, you can use the Task List to track errors in your code, track changes that still need to be made to your code, and to create shortcuts to lines of code.

The Task List window.

Figure 1.22. The Task List window.

There are several ways you can open the Task List window. If you want to open the Task List window and view every task entry, you can open the View menu, point to Show Tasks, and select All. You can also view particular categories of tasks under the View, Show Tasks menu:

  • Comment—These entries are automatically added to the Task List when you use particular keywords in the comments in your code.

  • Build Errors—These entries are automatically added to the Task List when your code contains errors.

  • User—These entries are explicitly added by the user to the Task List.

  • Shortcut—These entries are explicitly added by the user to the Task List as shortcuts to sections of code.

In the following sections, you’ll learn how to work with each category of tasks.

Displaying Tasks for the Current File

By default, the Task List window displays tasks associated with all the files in a project. For example, if you have multiple Web Form Pages in a project, the tasks associated with all the pages appear in the Task List window. However, you might want to focus on only those tasks associated with the file currently opened in the Code Editor.

To display only those tasks associated with the current file displayed in the Code Editor, open the View menu, point to Show Tasks, and select Current File.

Adding Code Comments to the Task Lists

You can automatically add entries to the Task List by using particular keywords when adding comments to your code. For example, suppose that you add the following comment to your code:

C#

// TODO: Must add a subroutine here

VB.NET

' TODO: Must add a subroutine here

The special keyword TODO causes a new entry to automatically be added to the Task List. Notice that the TODO keyword appears in the context of a comment.

When you click the TODO entry in the Task List window, you are brought the correct line in the Code Editor window. To display all comment tasks, select View, Show Tasks, Comment.

You can add your own keywords that will automatically trigger the addition of tasks to the Task List. For example, you might want to add a keyword SLOPPY that indicates a sloppily written section of code.

To add a custom keyword, perform the following steps:

  1. Select Options from the Tools menu.

  2. Navigate to the Environment folder.

  3. Display the Task List page.

  4. Enter a name for the new task in the Name text box (for example, SLOPPY). The name is not case sensitive.

  5. Enter a priority for the task.

  6. Click Add.

  7. Click OK.

After completing these steps, the comment will automatically appear in the Task List whenever you use the keyword SLOPPY.

Tracking Code Errors

When there are errors in your code, the errors are automatically added as tasks to the Task List window. Some errors are added to the Task List before the code is compiled. These errors are added to the Task List as you type. Other errors are added after the code is compiled.

For example, if you start using a variable named strMessage in your code and you neglect to declare it, an entry is automatically added to the Task List warning you of this fact.

You can view all the errors in the Task List by opening the View menu, pointing to Show Tasks, and selecting Build Errors, or by opening the View menu, pointing to Show Tasks, and selecting All. From the Task List window, you can click a particular error entry and navigate directly to the appropriate section of code.

Adding User Comments

You might want to add custom tasks to the Task List. For example, if you have a list of future changes that you plan to make to an application, you can add these proposed changes to the Task List.

To add a new user entry to the Task List, open the View menu, point to Show Tasks, and select User, or open the View menu, point to Show Tasks, and select All.

To add a new task, click the section of the Task List window that reads Click Here to Add a New Task. When you add a new task, you can specify the task priority and enter a description of the task.

After you complete the task, you can check the check box next to the task. Checking the check box next to a user task causes the task description to appear with a line through it (it strikes out the task).

Adding Shortcuts to the Task List

You can use the Task List window to maintain a list of shortcuts to sections of code. This works somewhat like adding a bookmark to your code. However, items added to the Task List are more permanent and they can have descriptions.

To open the Shortcut Task List window, open the View menu, point to Show Tasks, and select Shortcut, or open the View menu, point to Show Tasks, and select All.

To add a new shortcut to the Shortcut Task List window, right-click a line of code in the Code Editor and select the Add Task List Shortcut option. Alternatively, you can open the Edit menu, point to Bookmarks, and select Add Task List Shortcut.

After you add a shortcut to the Task List menu, you can navigate to the corresponding section of code in the Code Editor simply by double-clicking the shortcut.

Automatically Documenting Your Code

You can automatically generate documentation for a project or solution by selecting the Build Comment Web Pages from the Tools menu. Selecting this option generates a set of HTML pages that contain links to the pages in a project. Each page displays a list of the methods declared in the page (see Figure 1.23).

Building Comment Web Pages.

Figure 1.23. Building Comment Web Pages.

You can build Comment Web Pages when working with either a VB.NET or C# project. However, the C# language provides you with some additional options for commenting your code. When working with a C# project, you can add XML comments to your code by using the triple forward slash comment (///).

Consider the following code snippet:

public int AddNumbers( int val1, int val2 )
{
  return val1 + val2;
}

If you add an XML comment (a triple forward slash) before the code snippet in the Visual Studio .NET Code Editor, the following XML comment tags are automatically added.

/// <summary>
///
/// </summary>
/// <param name="val1"></param>
/// <param name="val2"></param>
/// <returns></returns>
public int AddNumbers( int val1, int val2 )
{
  return val1 + val2;
}

If you enter values for the <summary>, <param>, and <returns> tags, these values will appear when the Comment Web Pages are generated.

Summary

In this chapter, you were introduced to the major user interface features of the Visual Studio .NET development environment. In the first section of this chapter, you learned how to create a simple Web Form Page and add new solutions and projects to Visual Studio .NET.

Next, you received a quick tour of the windows that appear while working on an ASP.NET Web application. You learned the function of the Start Page, the Solution Explorer, the Toolbox, and the Server Explorer windows.

Finally, you learned how to customize the code and text editor to match your preferences. You discovered how you can modify the font and colors of the Code and Text editor. You also learned how to take advantage of the Task List window while working with the Code and Text editor.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset