Excerpt from ASP.NET 2.0 Beta Preview
ASP.NET 2.0: A New Compilation System
In ASP.NET 2.0, the code is constructed and compiled in a new way. Compilation in ASP.NET 1.0 was always a tricky scenario. With ASP.NET 1.0, you could build an application using ASP.NET, deploy it, and then watch as it was compiled page by page as each page was requested. If you made any changes to the code-behind file in ASP.NET 1.0, it was not reflected in your application until the entire application was rebuilt. That meant that the same page-by-page request had to be done again before the entire application was recompiled.
Everything about how ASP.NET 1.0 worked with classes and compilation is changed with the release of ASP.NET 2.0. The mechanics of the new compilation system actually begin with how a page is structured in ASP.NET 2.0. In ASP.NET 1.0, you either constructed your pages using the code-behind model or by placing all the server code inline between
<script> tags on your
.aspx page. Most pages were constructed using the code-behind model because this was the default when using Visual Studio .NET 2002 or 2003. It was quite difficult to create your page using the inline style in these IDEs. If you did, you were then deprived of the use of IntelliSense, which can be quite the lifesaver when working with the tremendously large collection of classes that the .NET Framework offers.
ASP.NET 2.0 offers a new code-behind model mostly because the .NET Framework now offers the capability to work with partial classes (also called partial types). Upon compilation, the separate class files are combined into a single offering. This gives you much cleaner code-behind pages. The code that was part of the
Web Form Designer Generated section of your classes is now separated from the code-behind classes that you create yourself.
ASP.NET 2.0 applications can include a
\Code directory where you place your classes. Any class placed here is dynamically compiled and reflected in the application. You do not use a separate build process when you make changes as you did with ASP.NET 1.0. This is a "just save and hit" deployment model like the one in ASP 3.0. Visual Studio Web Developer also automatically provides IntelliSense for any objects that are placed in the \Code directory, whether you are working with the code-behind model or are coding inline.
ASP.NET 2.0 also provides you with tools that enable you to precompile your ASP.NET applications so that no page within your application has latency when it is retrieved for the first time. It is also a great way to figure out if you have made any errors in the pages without invoking every page yourself.
Precompiling your ASP.NET 2.0 applications is as simple as calling the
precompile.axd imaginary file in the application root of your application after it has been deployed. This one call causes your entire application to be precompiled. You receive an error notification if any errors are found anywhere within your application. It is also possible to precompile your application and only deliver the created assembly to the deployment server, thereby protecting your code.
Additions to the Page Framework
The ASP.NET page framework has some dramatic new additions that you can include in your applications. One of the most dramatic ones is the capability to build ASP.NET pages based upon visual inheritance. This was possible in the Windows Forms world, but it was harder to achieve with ASP.NET. You also gain the capability to easily apply a consistent look and feel to the pages of your application by using themes. Many of the difficulties in working with ADO.NET in the past have now been removed with the addition of a new series of data source controls that take care of accessing and retrieving data from a large collection of data stores. Although these are not the only new controls, the great number of new server controls create a larger ASP.NET page framework.
With the introduction of master pages in ASP.NET 2.0, you can now use visual inheritance within your ASP.NET applications. Because many ASP.NET applications have a similar structure throughout their pages, it is logical to build a page template once and use that same template throughout the application.
In ASP.NET 2.0, you do this by creating a
.master page, as shown in Figure 1.
An example master page might include a header, footer, and any other elements that all the pages will share. Besides these core elements, which you might want on every page that inherits and uses this template, you can place
<asp:ContentPlaceHolder> server controls within the master page itself for the subpages to use in order to change specific regions of the master page template. The editing of the subpage is shown in Figure 2.
When an end user invokes one of the subpages, he is actually looking at a single page compiled from both the subpage and the master page that the particular subpage inherited from. This also means that the server and client code from both pages are enabled on the new single page.
The nice thing about master pages is that you now have a single place to make any changes that affect the entire site. This eliminates making changes to each and every page within an application.
The introduction of themes in ASP.NET 2.0 has made it quite simple to provide a consistent look and feel across your entire site. Themes are simple text files where you define the appearance of server controls that can be applied across the site, to a single page, or to a specific server control. You can also easily incorporate graphics and Cascading Style Sheets, in addition to server control definitions.
Themes are stored either on the server for all applications to use or in the
/Theme directory within the application root for use within that particular application. The server-wide, pre-installed themes can be found at:
One cool capability of themes is that you can dynamically apply them based on settings that use the new personalization service provided by ASP.NET 2.0. Each unique user of your portal or application can have her own personalized look and feel that she has set from your offerings.
New Objects for Accessing Data
One of the more code-intensive tasks in ASP.NET 1.0 was in the retrieval of data. In many cases, this meant working with a number of objects. If you have been working with ASP.NET for a while, you know that it was an involved process to display data from a Microsoft SQL Server table within a DataGrid server control. For instance, you first had to create a number of new objects. They included a
SqlConnection object followed by a
SqlCommand object. When those objects were in place, you then created a
SqlDataReader to populate your DataGrid by binding the result to the DataGrid. In the end, a table appeared containing the contents of the data you were retrieving (such as the Customers table from the Northwind database).
ASP.NET 2.0 eliminates this intensive procedure with the introduction of a new set of objects that work specifically with data access and retrieval. These new data controls are so easy to use that you access and retrieve data to populate your ASP.NET server controls without writing any code. You saw an example of this in Listing 1-2, where an
<asp:SqlDataSource> server control retrieved rows of data from the Customers table in the Northwind database from SQL Server. This SqlDataSource server control was then bound to the new GridView server control via the use of simple attributes within the GridView control itself. It really couldn't be any easier!
The great news about this new functionality is that it is not limited to just Microsoft's SQL Server. In fact, a good number of data source server controls are at your disposal. You also have the capability to create your own. In addition to the SqlDataSource server control, ASP.NET 2.0 introduces AccessDataSource, XmlDataSource, ObjectDataSource, DataSetDataSource, and SiteMapDataSource server controls. You use all these new data controls later in this book.
New Server Controls
So far, you have seen a number of new server controls that you can use when building your ASP.NET 2.0 pages. For instance, I just spoke of all the new data source server controls that you can use to access different kinds of data stores. You also saw the use of the new GridView server control, which is an enhanced version of the previous DataGrid control that you used in ASP.NET 1.0.
Besides the controls presented thus far, ASP.NET 2.0 provides more than 40 additional new server controls! In fact, so many new server controls have been introduced that the next IDE for building ASP.NET applications, Visual Studio 2005, had to reorganize the Toolbox where all the server controls are stored. They are now separated into categories instead of being displayed in a straight listing as they were in Visual Studio .NET or the ASP.NET Web Matrix. The new Visual Studio 2005 Toolbox is shown in Figure 3.
A New IDE for Building ASP.NET 2.0 Pages
With ASP.NET 1.0/1.1, you can build your ASP.NET application using Notepad, Visual Studio .NET 2002 and 2003, as well as the ASP.NET Web Matrix. ASP.NET 2.0 also introduces another IDE to the Visual Studio family - Visual Studio 2005.
Visual Studio 2005 offers some dramatic enhancements that completely change the way in which you build your ASP.NET applications. Figure 4 shows you a screen shot of the new Visual Studio 2005.
The most exciting change to the IDE is that Visual Studio 2005 builds applications using a file-based system, not the project-based system used by Visual Studio .NET. When using Visual Studio .NET, you had to create new projects (for example, an ASP.NET Web Application project). This process created a number of project files in your application. Because everything was based on a singular project, it became very difficult to develop applications in a team environment.
Visual Studio 2005, on the other hand, is based on a file system approach. No project files are included in your project, and this makes it very easy for multiple developers to work on a single application together without bumping into each other. Other changes are those to the compilation system, which I discussed earlier. You can now build your ASP.NET pages using the inline model or the new code-behind model. Whether you build pages inline or with the new code-behind model, you have full IntelliSense capabilities no matter what model you use. This, in itself, is powerful and innovative. Figure 5 shows IntelliSense running from an ASP.NET page being built using the inline model.
Another feature of Visual Studio 2005 that has come over from the ASP.NET Web Matrix is that you don't need ISS on your development machine. Visual Studio 2005 has a built-in Web server that enables you to launch pages from any folder in your system with relative ease.