Pro ASP.NET 2.0 In CSharp 2005 (2005) [eng]
.pdf28 C H A P T E R 2 ■ V I S U A L S T U D I O 2 0 0 5
Figure 2-2. Browsing to a website location
Once you make your selection and click Open, Visual Studio returns you to the Create Web Site dialog box. Click OK, and Visual Studio will create the new web application. A new website starts with exactly one file—a default.aspx start page.
Projectless Development
In many ways, Visual Studio 2005 web applications are more remarkable for what they don’t contain than what they do. Unlike previous versions of Visual Studio, Visual Studio 2005 web applications don’t include extra files, such as .csproj project files and .sln solution files. Instead, every file in your web folder automatically is considered part of the web application. (This model makes sense, because every web page in a virtual directory is independently accessible, whether or not you consider it an official part of your project.)
Clearing out this clutter has several benefits:
•It’s less work to deploy your website, because you don’t need to specifically exclude these files. There’s also less duplication of settings, because most of what Visual Studio needs (such as assembly references) is stored in the web.config configuration file.
•Team collaboration is greatly simplified, because different people can work independently on different pages without needing to lock the project files.
C H A P T E R 2 ■ V I S U A L S T U D I O 2 0 0 5 |
29 |
•It’s easier to author websites with other tools, because no extra project files need to be maintained.
•Files can easily be transferred from one web application to another—all you need to do is copy the file.
Although this simplifies life dramatically, under the radar there are still the last vestiges of Visual Studio’s solution-based system.
When you create a web application, Visual Studio actually creates solution files (.sln and .suo) in a user-specific directory, like c:\Documents and Settings\[UserName]\Visual Studio 2005\ Projects\[ProjectName]. This file provides a few Visual Studio–specific features that aren’t directly related to ASP.NET, such as debugging settings. For example, if you add a breakpoint to the code in a web page (as discussed in the “Visual Studio Debugging” section later in this chapter), Visual Studio stores the breakpoint in the .suo file so it’s still there when you open the project later. Similarly, Visual Studio tracks the currently open files so it can restore your view when you return to the project. This approach to solution management is fragile—obviously, if you move the project from one location to another, you lose all this information. However, because this information isn’t really all that important (think of it as a few project-specific preferences), losing it isn’t a serious problem. The overall benefits of a projectless system are worth the trade-off.
Migrating a Visual Studio .NET Project
If you have an existing web application created with Visual Studio .NET 2002 or 2003, you can open the project or solution file using the File Open Project command. When you do, Visual Studio begins the Conversion Wizard.
The Conversion Wizard is exceedingly simple. It prompts you to choose whether to create a backup and, if so, where it should be placed (see Figure 2-3). If this is your only copy of the application, a backup is a good idea in case some aspects of your application can’t be converted successfully. Otherwise, you can skip this option.
Figure 2-3. Importing a Visual Studio .NET 2003 project
30 C H A P T E R 2 ■ V I S U A L S T U D I O 2 0 0 5
When you click Finish, Visual Studio performs an in-place conversion. The conversion tool is fairly aggressive, and it attempts to convert every web page to use Visual Studio’s new code-behind model. Any errors and warnings are added to a conversion log, which you can display when the conversion is complete. In a typical website, the conversion operation runs without any errors but generates a long list of warnings. These inform you when Visual Studio removes precompiled files, changes pages to use automatic event wire-up, and modifies the accessibility of event handlers (switching them from private to protected). All of these changes are minor modifications designed to apply the new coding model, which is described in the section “The Coding Model” later in this chapter. Figure 2-4 shows a sample log.
Figure 2-4. A conversion log with typical warnings
Visual Studio 2005 doesn’t support adding old web pages to a new web application using the Website Add Existing Item. If you take this step and try to run your web application, you’ll receive an error informing you that the Visual Studio .NET 2003 version of the code-behind model is no longer supported. Instead, Visual Studio will recommend you use the Open Project feature to start the Conversion Wizard.
Designing a Web Page
To start designing a web page, double-click the web page in the Solution Explorer (start with default.aspx if you haven’t added any pages). A blank page will appear in the designer.
To add controls, choose the control type from the Toolbox on the left. (The controls in the Toolbox are grouped in numerous categories based on their functions, but you’ll find basic ingredients in the Standard tab.) Once you’ve added a control, you can resize it and configure its properties
C H A P T E R 2 ■ V I S U A L S T U D I O 2 0 0 5 |
31 |
in the Properties window. Every time you add a web control, Visual Studio automatically adds the corresponding tag to your .aspx web-page file. You can switch your view to look at the tags by clicking the Source button at the bottom of the web designer window. Click Design to revert to the graphical web form designer.
Figure 2-5 shows two views of the same web page that contain a label and a button. One view is in HTML mode, and the other is in design mode.
Figure 2-5. The two modes for editing web pages
32 C H A P T E R 2 ■ V I S U A L S T U D I O 2 0 0 5
Using the HTML view, you can manually add attributes or rearrange controls. In fact, Visual Studio even provides limited IntelliSense features that automatically complete opening tags and alert you if you use an invalid tag. Generally, you won’t need to use the HTML view in Visual Studio. Instead, you can use the design view and configure controls through the Properties window.
■Note Unlike previous versions, Visual Studio 2005 doesn’t tamper with your HTML markup. Instead, it always preserves the indenting you use. You can fine-tune this behavior using the Text Editor HTML group of settings in the Tools Options dialog box. For example, one handy option that isn’t turned on by default is Format HTML on Paste, which indents arbitrary blocks of markup when you paste them into a page.
To configure a control, click once to select it, or choose it by name in the drop-down list at the top of the Properties window. Then, modify the appropriate properties in the window, such as Text, ID, and ForeColor. These settings are automatically translated to the corresponding ASP.NET control tag attributes and define the initial appearance of your control. Visual Studio even provides special “choosers” (technically known as UITypeEditors) that allow you to select extended properties. For example, you can select a color from a drop-down list that shows you the color, and you can configure the font from a standard font selection dialog box.
To position a control on the page, you need to use all the usual tricks of HTML, such as paragraphs, line breaks, and tables. Unlike previous versions, Visual Studio 2005 doesn’t support a grid-layout mode for absolute positioning with CSS (Cascading Style Sheets). Instead, it encourages you to use the more flexible flow-layout mode, where content can grow and shrink dynamically without creating a problem. However, there is a way to get back to the grid-layout behavior. All you need to do is add an inline CSS style for your control that specifies absolute positioning. (This style will already exist in any pages you’ve created with a previous version of Visual Studio .NET in gridlayout mode.) Here’s an example:
<asp:Button id="cmd" style="POSITION: absolute; left: 100px; top: 50px;" runat="server" ... />
Once you’ve made this change, you’re free to drag the button around the window at will. Of course, you shouldn’t go this route just because it seems closer to the Windows GUI (graphical user interface) model. Few great web pages rely on absolute positioning, because it’s just too awkward and inflexible.
Smart Tags
Another timesaving feature that’s new in Visual Studio 2005 is the smart tag; smart tags make it easier to configure complex controls. Smart tags aren’t offered for all controls, but they are used for rich controls such as the GridView, TreeView, and Calendar.
You’ll know a smart tag is available if, when you select a control, you see a small arrow in the top-right corner. If you click this arrow, a window will appear with links that trigger other, higherlevel tasks. For example, Figure 2-6 shows how you can use this technique to access Calendar autoformatting. (Smart tags can include many more features, but the Calendar smart tag provides only a single link.)
C H A P T E R 2 ■ V I S U A L S T U D I O 2 0 0 5 |
33 |
Figure 2-6. A smart tag for the Calendar control
Static HTML Tags
Along with full-fledged web controls, you can also add ordinary HTML tags. You simply drag these from the HTML tab of the Toolbox.
For example, you might want to create a simple <div> tag to group some web controls with a border. Visual Studio provides a valuable style builder for formatting any static HTML element with CSS style properties. To test it, add the Div from the HTML section of the Toolbox, which appears on your page as a panel. Then right-click the panel, and choose Style. The Style Builder dialog box (shown in Figure 2-7) will appear, with options for configuring the colors, font, layout, and border for the element. As you configure these properties, the web page’s HTML will be updated to reflect your settings.
Figure 2-7. Building HTML styles
34 C H A P T E R 2 ■ V I S U A L S T U D I O 2 0 0 5
If you want to configure the HTML element as a server control so that you can handle events and interact with it in code, you need to right-click it in the web page and select Run As Server Control. This adds the required runat="server" attribute to the control tag. Alternatively, you could switch to design view and type this in on your own.
HTML Tables
One convenient way to organize content in a web page is to place it in the different cells of an HTML table using the <table> tag. In previous versions of Visual Studio, the design-time support for this strategy was poor. But in Visual Studio 2005, life gets easier. To try it, drag a table from the HTML tab of the Toolbox. You’ll start with a standard 3 3 table, but you can quickly transform it using editing features that more closely resemble a word processor than a programming tool. Here are some of the tricks you’ll want to use:
•To move from one cell to another in the table, press the Tab key or use the arrow keys. The current cell is highlighted with a blue border. Inside each cell you can type static HTML or drag and drop controls from the Toolbox.
•To add new rows and columns, right-click inside a cell, and choose from one of the many options in the Insert submenu to insert rows, columns, and individual cells.
•To resize a part of the table, just click and drag.
•To format a cell, right-click inside it, and choose Style. This shows the same Style Builder dialog box you saw in Figure 2-7.
•To work with several cells at once, hold down Ctrl while you click each cell. You can then right-click to perform a batch formatting operation.
•To merge cells (in other words, change two cells into one cell that spans two columns), just select the cells, right-click, and choose Merge.
With these conveniences, you might never need to resort to a design tool like Dreamweaver.
The Visual Studio IDE
Now that you’ve created a basic website, it’s a good time to take a tour of the different parts of the Visual Studio interface. Figure 2-8 identifies each part of the Visual Studio window, and Table 2-1 describes each one.
Table 2-1. Visual Studio Windows
Windows |
Description |
Solution Explorer |
Lists the files and subfolders that are in the web application folder. |
Toolbox |
Shows ASP.NET’s built-in server controls and any third-party controls or |
|
custom controls that you build yourself and add to the Toolbox. Controls |
|
can be written in any language and used in any language. |
Server Explorer |
Allows access to databases, system services, message queues, and other |
|
server-side resources. |
Properties |
Allows you to configure the currently selected element, whether it’s a file in |
|
the Solution Explorer or a control on the design surface of a web form. |
C H A P T E R 2 ■ V I S U A L S T U D I O 2 0 0 5 |
35 |
Windows |
|
Description |
||||||||||||||||||||||||||||||||||||||
Error List |
|
Reports on errors that Visual Studio has detected in your code but that you |
||||||||||||||||||||||||||||||||||||||
|
|
|
|
haven’t resolved yet. |
||||||||||||||||||||||||||||||||||||
Task List |
|
Lists comments that start with a predefined moniker so that you can keep |
||||||||||||||||||||||||||||||||||||||
|
|
|
|
track of portions of code that you want to change and also jump to the |
||||||||||||||||||||||||||||||||||||
|
|
|
|
appropriate position quickly. |
||||||||||||||||||||||||||||||||||||
Document |
|
Allows you to design a web page by dragging and dropping and to edit |
||||||||||||||||||||||||||||||||||||||
|
|
|
|
the code files you have within your Solution Explorer. Also supports |
||||||||||||||||||||||||||||||||||||
|
|
|
|
non-ASP.NET file types, such as static HTML and XML files. |
||||||||||||||||||||||||||||||||||||
Macro Explorer |
|
Allows you to see all the macros you’ve created and execute them. Macros |
||||||||||||||||||||||||||||||||||||||
|
|
|
|
are an advanced Visual Studio feature; they allow you to automate time- |
||||||||||||||||||||||||||||||||||||
|
|
|
|
consuming tasks. Visual Studio exposes a rich extensibility model, and |
||||||||||||||||||||||||||||||||||||
|
|
|
|
you can write a macro using pure .NET code. |
||||||||||||||||||||||||||||||||||||
Class View |
|
Shows a different view of your application that is organized to show all the |
||||||||||||||||||||||||||||||||||||||
|
|
|
|
classes you’ve created (and their methods, properties, and events). |
||||||||||||||||||||||||||||||||||||
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Figure 2-8. The Visual Studio interface
36 C H A P T E R 2 ■ V I S U A L S T U D I O 2 0 0 5
■Tip The Visual Studio interface is highly configurable. You can drag the various windows and dock them to the sides of the main Visual Studio window. Also, some windows on the side automatically slide into and out of view as you move your mouse. If you want to freeze these windows in place, just click the thumbtack icon in the top-right corner of the window.
Solution Explorer
The Solution Explorer is, at its most basic, a visual filing system. It allows you to see the files that are in the web application directory.
Table 2-2 lists some of the file types you’re likely to see in an ASP.NET web application.
Table 2-2. ASP.NET File Types
File |
Description |
Ends with .aspx |
These are ASP.NET web pages (the .NET equivalent of the .asp file in an ASP |
|
application). They contain the user interface and, optionally, the underlying |
|
application code. Users request or navigate directly to one of these pages to |
|
start your web application. |
Ends with .ascx |
These are ASP.NET user controls. User controls are similar to web pages, |
|
except that they can’t be accessed directly. Instead, they must be hosted |
|
inside an ASP.NET web page. User controls allow you to develop an important |
|
piece of the user interface and reuse it in as many web forms as you want |
|
without repetitive code. |
Ends with .asmx |
These are ASP.NET web services. Web services work differently than web |
|
pages, but they still share the same application resources, configuration |
|
settings, and memory. |
web.config |
This is the XML-based configuration file for your ASP.NET application. |
|
It includes settings for customizing security, state management, memory |
|
management, and much more. Visual Studio adds a web.config file when |
|
you need it. (For example, it adds a web.config file that supports debugging |
|
if you attempt to run your web application.) When you first create a website, |
|
you won’t have a web.config file. |
global.asax |
This is the global application file. You can use this file to define global |
|
variables and react to global events, such as when a web application first |
|
starts (see Chapter 5 for a detailed discussion). Visual Studio doesn’t create |
|
a global.asax file by default—you need to add it if it’s appropriate. |
Ends with .cs |
These are code-behind files that contain C# code. They allow you to separate |
|
the application from the user interface of a web page. The code-behind |
|
model is introduced in this chapter and used extensively in this book. |
|
|
In addition, your web application can contain other resources that aren’t ASP.NET file types. For example, your virtual directory can hold image files, HTML files, or CSS files. These resources might be used in one of your ASP.NET web pages, or they can be used independently.
Visual Studio distinguishes between different file types. When you right-click a file in the list, a context menu appears with the menu options that apply for that file type. For example, if you rightclick a web page, you’ll have the option of building it and launching it in a browser window.
Using the Solution Explorer, you can rename, rearrange, and add files. All these options are just a right-click away. To delete a file, just select it in the Solution Explorer, and press the Delete key.
You can also add new files by right-clicking the Solution Explorer and selecting Add Add New Item. You can add various different types of files, including web forms, web services, stand-alone
C H A P T E R 2 ■ V I S U A L S T U D I O 2 0 0 5 |
37 |
classes, and so on. You can also copy files that already exist elsewhere on your computer (or an accessible network path) by selecting Add Add Existing Item. Use the Add New Folder to create a new subdirectory inside your web application. You can then drag web pages and other files into or out of this directory.
Visual Studio also checks for project management events such as when another process changes a file in a project you currently have open. When this occurs, Visual Studio will notify you and give you the option to refresh the file.
Document Window
The document window is the portion of Visual Studio that allows you to edit various types of files using different designers. Each file type has a default editor. To learn a file’s default editor, simply right-click that file in the Solution Explorer, and then select Open With from the pop-up menu. The default editor will have the word Default alongside it.
Depending on the applications you’ve installed, you may see additional designers that plug into Visual Studio. For example, if you’ve installed FrontPage 2003, you’ll have the option of editing web pages with a FrontPage designer (which actually opens your web page in a stand-alone FrontPage window).
Toolbox
The Toolbox works in conjunction with the document window. Its primary use is providing the controls that you can drag onto the design surface of a web form. However, it also allows you to store code and HTML snippets.
The content of the Toolbox depends on the current designer you’re using as well as the project type. For example, when designing a web page, you’ll see the set of tabs described in Table 2-3. Each tab contains a group of buttons. You can see only one tab at a time. To view a tab, click the heading, and the buttons will slide into view.
Table 2-3. Toolbox Tabs for an ASP.NET Project
Tab |
Description |
Standard |
This tab includes the rich web server controls that are the heart of ASP.NET’s web |
|
form model. |
Data |
These components allow you to connect to a database. This tab includes |
|
nonvisual data source controls that you can drop onto a form and configure at |
|
design time (without using any code) and data display controls such as grids. |
Validation |
These controls allow you to verify an associated input control against user-defined |
|
rules. For example, you can specify the input can’t be empty, it must be a number, |
|
it must be greater than a certain value, and so on. Chapter 4 has more details. |
Navigation |
These controls are designed to display site maps and allow the user to navigate |
|
from one page to another. You’ll learn about the navigation controls in Chapter 16. |
Login |
These controls provide prebuilt security solutions, such as login boxes and a |
|
wizard for creating users. You’ll learn about the login controls in Chapter 20. |
WebParts |
This set of controls supports web parts, an ASP.NET model for building |
|
componentized, highly configurable web portals. You’ll learn about web parts |
|
in Chapter 29. |
HTML |
This tab allows you to drag and drop static HTML elements. If you want, you can |
|
also use this tab to create server-side HTML controls—just drop a static HTML |
|
element onto a page, right-click it, and choose Run As Server Control. |
General |
Provides a repository for code snippets and control objects. Just drag and drop |
|
them here, and pull them off when you need to use them later. |
|
|