An Introduction To: HEN Icrosoftreleased
An Introduction To: HEN Icrosoftreleased
An Introduction to
ASP.NET 2.0
1
2 AN INTRODUCTION TO ASP.NET 2.0
identify and stop rogue applications will give hosters more control
over their current environment. More control can also be given to
hosted companies by use of the new Web-based administration tool,
allowing users to easily control the configuration of applications
remotely.
• Provide easier and more sophisticated management features. Ad-
ministration of ASP.NET applications under version 1.x required
manual editing of the XML configuration file, which is not a great
solution for administrators. Version 2.0 brings a graphical user in-
terface–based administration tool that is integrated with the Internet
Information Services (IIS) administration tool.
• Ease implementation of entire scenarios. The better management
features are built on top of a management application programming
interface (API), allowing custom administration programs to be
created. Along with application packaging this will provide support
for easily deployable applications, with or without source.
Even from this broad set of aims you can see that ASP.NET 2.0 is a great
advance from 1.x for both developers and administrators.
New Features
This chapter isn’t an in-depth look at any specific feature—instead we are
going to give you a taste of what’s to come so you can see how much eas-
ier Web development is going to be. For this outlook we’ve broken down
the new features into rough end-to-end scenarios.
• Creating a custom class object that inherits from Page and having
this custom page preload controls
• Creating a templated server control, where the templates provide
the layout areas for each page, and using this control on every page
• Having User Controls for common areas of the site, such as head-
ings, menus, and footers
4 AN INTRODUCTION TO ASP.NET 2.0
Of these, the first two require knowledge of creating server controls, and
while this is a topic most ASP.NET developers could master, it may not be
one they’ve had experience with. Therefore a solution using custom server
controls tends to be avoided. The last option, though, is a simple solution,
easy to create and implement. User Controls were created to provide
reusable functionality, and this is a great use for them. However, to apply
a consistent look and feel you need to first place the User Controls on each
page, then ensure that they are placed in the same place on each page. In
other words, you really need a page template, and in reality this manifests
itself as an ASP.NET file that you simply copy for each new page. The dan-
ger of this approach is that it’s too easy to modify a page and change the
layout for that single page.
<asp:Content <asp:Content
runat="server" runat="server"
ContentName="Main"> ContentName="Main">
<asp:ContentPlaceHolder
runat="server"
ContentName="Main" />
</asp:Content>
</asp:Content>
Resulting
Pages
default.aspx
\Themes
\MyTheme
MySkin.skin
\YourTheme
YourSkin.skin
Each theme consists of a directory under the Themes directory. Within each
theme there is a file with a .skin suffix, which contains the skin details for
that theme. For example, MySkin.skin might contain:
This defines two skins for the Label control, each with different visual prop-
erties. The theme can be chosen by setting a page-level property, and the
skin is chosen by setting a control-level property, as demonstrated below.
<form runat="server">
</form>
1.2 NEW FEATURES 7
Both of these can be set at runtime as well as design time, so this provides
an extremely powerful solution, especially when connected with the new
Personalization features.
For example, to add login features to your page all you need to do is
add the following code:
<form runat="server">
<asp:Login runat="server" />
</form>
8 AN INTRODUCTION TO ASP.NET 2.0
End Sub
What’s even better is that when using the Login control you don’t even
have to do this—the control handles it for you.
The great strength of the Membership API is that it is built on the idea of
Membership Providers, with support for Microsoft SQL Server and Access
supplied by default. To integrate custom membership stores you simply
need to provide a component that inherits from the Membership interface
and add the new provider details to the configuration file.
The Membership API has some simple goals.
Name.Text = Profile.Name
Address.Text = Profile.Address
Theme.Text = Profile.Theme
End Sub
Profile.Name = Name.Text
Profile.Address = Address.Text
Profile.Theme = Theme.Text
End Sub
</script>
<form runat="server">
Name: <asp:TextBox id="Name" runat="server" /> <br />
Address: <asp:TextBox id="Address" runat="server" /> <br />
Theme: <asp:TextBox id="Theme" runat="server" /> <br />
<asp:Button Text="Update" onClick="Update_Click" runat="server" />
</form>
The simplicity of this method means we only have to deal with the user
profile. We don’t need to know how it stores the data—we just deal with
the properties each profile has. This personalization also allows us to eas-
ily use the theming capabilities, changing the theme when the page is cre-
ated, as demonstrated below.
Me.Theme = Profile.Theme
End Sub
To ensure that the theme customization is applied before the controls are
created we use the new PreInit event.
Creating Portals
As if customization of a site’s look weren’t enough, ASP.NET 2.0 also
brings a way to alter the structure with its new portal framework.
The success of the ASP.NET IBuySpy portal application and its off-
shoots shows that customized sites are popular. The trouble has always
been how to provide a consistent look while still allowing user customiza-
tion not only of the style but also of the content and placement of content.
Microsoft has already implemented solutions to provide this functionality,
including SharePoint Server and Outlook Web Parts.
In ASP.NET 2.0, Web Parts become the underlying technology for all Mi-
crosoft portal applications, allowing a single easy-to-use, extensible frame-
work. The concept revolves around two key controls—the WebPartZone
and the WebPart. The WebPartZone identifies areas on the page in which
content can be changed, and the WebPart identifies the part (or module)
within the zone. There are different types of WebPart controls for different
purposes, for example:
<table>
<tbody>
<tr>
<td valign="top" align="left">
<asp:WebPartZone id="LeftZone" runat="server"
Title="Left Zone" partFrameType="TitleOnly"
lockLayout="False" borderColor="White">
<ZoneTemplate>
<asp:ContentWebPart id="ContentWebPart1"
title="Welcome" runat="server">
<ContentTemplate>
continues
12 AN INTRODUCTION TO ASP.NET 2.0
This project …
</ContentTemplate>
</asp:ContentWebPart>
<uc1:Announcements id="Announcements1" runat="server" />
</ZoneTemplate>
</asp:WebPartZone>
</td>
<td valign="top" align="left">
<asp:WebPartZone id="RightZone" runat="server"
title="Right Zone" width="100%"
partFrameType="TitleOnly" lockLayout="False"
borderColor="White">
<ZoneTemplate>
<sample:WeatherWebPart runat="server" id="WeatherWebPart"
Title="My Weather" width="250px" />
<uc1:dailylinkswebpart id="DailyLinksWebPart1"
runat="server" title="Daily Links" />
</ZoneTemplate>
</asp:WebPartZone>
</td>
<td valign="top" align="left">
</td>
</tr>
</tbody>
</table>
Here you can see two WebPartZone controls separating the left and right
content. Within each there is a mixture of content, including static text,
user controls, and custom server controls.
At first glance this doesn’t look like much improvement over existing
layout methods such as user controls—in fact, it looks more complex.
However, the framework on which Web Parts is built is great for develop-
ers and users alike. Developers only have to drop user controls or server
controls into a ZoneTemplate to automatically receive Web Parts function-
ality. To enhance this functionality you can add verbs to the WebPartZone
to indicate which features the framework should add to each part within
the template. Listing 1.4 shows an example.
<WebPartRestoreVerb checked="False"
imageUrl="images/RestoreVerb.gif"
enabled="True" text="Restore"
description="Restores the WebPart"
visible="True" />
<WebPartMinimizeVerb checked="False"
imageUrl="images/MinimizeVerb.gif"
enabled="True" text="Minimize"
description="Minimizes the WebPart"
visible="True" />
<WebPartHelpVerb checked="False"
enabled="True" text="Help"
description="Shows help for the WebPart"
visible="True" />
<WebPartEditVerb checked="False"
imageUrl="images/EditVerb.gif"
enabled="True" text="Edit"
description="Edits the WebPart"
visible="True" />
Here there are verbs that allow minimizing and maximizing the WebPart
controls, editing, help, and so on.
For the user, the Personalization features allow each WebPart to be
moved to other WebPartZone controls or edited. For example, moving a
14 AN INTRODUCTION TO ASP.NET 2.0
</ZoneTemplate>
</asp:EditorZone>
This indicates that there are two editor parts—one for the appearance, and
one for the property grid—for properties of the WebPart that are marked
as personalizable. Selecting the edit button invokes the editing features
and the EditorZone is made visible, as shown in Figure 1.6. Once edited
for zip code 02116, the WebPart shows the weather for Boston (Figure 1.7).
XML schema before you can modify it. Second, you need some way to actually
fetch the file, edit it, and then upload it. This is a problem particularly for
hosted scenarios, where users are always remote, and administration of many
sites can become a management nightmare. Finally, you cannot create a Web
Application, which is required for sites that require security.
Three features in ASP.NET 2.0 help solve these issues. The first is the
Microsoft Management Console (MMC) Snap-in for configuration, as shown
in Figure 1.8.
The second feature is a Management API, providing a programmable in-
terface to manage a site. For example, Listing 1.6 sets the authorization mode
using the API.
cfg = Configuration.GetConfigurationForUrl(Request.ApplicationPath)
ms = CType(cfg.GetConfigurationSection("system.web/authentication"),
AuthenticationSection)
ms.Mode = HttpAuthenticationMode.Windows
cfg.Update()
1.2 NEW FEATURES 17
Here you have a simple Web interface that allows configuration of all
aspects of a site. The interface is designed to be customized, so corpora-
tions and hosts can give it a company look.
<asp:ImageMap runat="server"
onClick="Map_Click"
ImageUrl="images/states.jpg">
<asp:CircleHotSpot X="100" Y="100" Radius="25"
Value="Other State" />
<asp:RectangleHotSpot Top="200" Left="150" Right="200" Bottom="150"
Value="More State"/>
<asp:PolygonHotSpot Coordinates="3,4, 15,18, 45,18, 15,70, 3,4"
Value="State 1" />
</asp:PolygonHotSpot>
End Sub
<form runat="server">
<asp:DynamicImage DynamicImageType="ImageFile"
ImageFile="car.gif" runat="server" />
</form>
For a standard Web browser the image is rendered as expected, but for a
Wireless Access Protocol (WAP) phone, the image is rendered as a Wireless
Bitmap (WBMP). This removes any need for the developer to specifically
target images to browser types.
This just encapsulates the code everyone used to put in the Page_Load
event—it connects to the database, fetches the data, and binds the grid.
The contents of the SelectCommand can be a stored procedure as well as a
SQL command, thus preserving the separation of data access from the
page itself. There are commands for updating, inserting, and deleting.
This model is extended by use of a parameter collection, allowing pa-
rameters to be passed into the command from a variety of sources. For ex-
ample, the code in Listing 1.7 automatically takes the value from the
TextBox control txtState and feeds this into the parameter @state.
Data Binding
Data binding in ASP.NET 1.x was simple, but it did cause confusion in
some areas. For example, should you use early binding, for which you
have to know the underlying data structure? Or should you take the de-
velopment shortcut and use late binding, like this:
There is also an equivalent XPath syntax for XPath expressions when bind-
ing to XML documents:
Binding to Objects
One of the most requested features has been the ability to bind data di-
rectly to objects. Good design dictates that you separate your data access
layer from your presentation layer, and this is often done as a set of classes.
The new ObjectDataSource allows you to simply bind directly to existing
objects, such as classes, thus allowing you to have a strongly typed data
layer but still participate in the easy data binding that ASP.NET 2.0 brings.
Device Filters
This architecture is taken further by allowing adapter-specific attributes for
controls, enabling the page designer to provide different content for spe-
cific devices. For example, the following code shows how different text
and cascading style sheet (CSS) styling can be defined for a mobile device.
Device Templates
Along with modified attributes, we also have the ability to provide tem-
plates for specific devices. We know that mobile devices have a small
screen size, so repeated controls such as grids and lists either aren’t ap-
propriate or need different output. By using specific templates for devices
we can now provide different content to different devices, as shown in
Listing 1.8.
22 AN INTRODUCTION TO ASP.NET 2.0
<HtmlBrowsers:HeaderTemplate>
<table>
<tr><td>UserName</td><td>Address</td><td>Phone</td></tr>
</HtmlBrowsers:HeaderTemplate>
<HtmlBrowsers:ItemTemplate>
<tr>
<td><%# Container.DataItem("UserName") %></td>
<td><%# Container.DataItem("Address") %></td>
<td><%# Container.DataItem("Phone") %></td>
</tr>
</HtmlBrowsers:ItemTemplate>
<WmlBrowsers:ItemTemplate>
<asp:Panel runat="server">
<%# Container.DataItem("UserName") %>
<%# Container.DataItem("Phone") %>
</asp:Panel>
</WmlBrowsers:ItemTemplate>
<HtmlBrowsers:FooterTemplate>
</table>
</HtmlBrowsers:FooterTemplate>
</asp:Repeater>
the compilation step for you, but stand-alone tools (such as Web Matrix)
don’t, so you have to handcraft a batch file to make your assemblies.
ASP.NET 2.0 provides automatic compilation for satellite code by sup-
porting a code directory. All files within this directory will be compiled on
the first run, thus removing the need for separate compilation scripts. Files
within the code directory don’t have to be just pure code, such as Visual
Basic .NET or C# files. Support is also included for Web Services Descrip-
tion Language (WSDL) files and strongly typed DataSets (XSD) files. For
WSDL files the proxy will automatically be created, and for XSD files the
appropriate classes will be created.
Along with automatic compilation comes pre-compilation—an entire
site (Web pages and code) can be pre-compiled. This not only provides a
way to deploy compiled applications but also removes the performance hit
taken by the compilation process on the first run. In addition, since only
compiled files are deployed, intellectual property is protected.
Another automatic feature is that of resources, such as those used for
globalization. The resources directory provides a place for these, which are
included as part of the compilation process.
Development Tools
Having a whole raft of new features in ASP.NET is great, but what about
design tools? Version 2.0 of the .NET Framework will introduce the latest
version of Visual Studio .NET—Visual Studio .NET “Whidbey.” When
ASP.NET 1.0 was released it quickly became apparent that a development
tool targeted at Web developers was required. Visual Studio .NET provides
great project and design features targeted at corporate developers. Web
Matrix was released to appeal to ASP.NET developers who don’t have ac-
cess to Visual Studio .NET. It’s a small stand-alone tool, specifically tar-
geted at ASP.NET development, and provides some features that aren’t in
Visual Studio .NET.
With ASP.NET 2.0, Visual Studio .NET “Whidbey” has undergone
some major enhancements and now provides a far superior environment
for developing Web applications than previous versions. While the design
environment is very familiar, the feature set has improved, making it a pre-
mier Web development tool.
24 AN INTRODUCTION TO ASP.NET 2.0
Key design features for Visual Studio .NET “Whidbey” include the fol-
lowing:
• Traditional in-line coding approach, plus a new code-behind model
• Support for all managed languages
• Ability to edit any file anywhere (FTP, File System, Front Page Ex-
tensions, and so on)
• Support for data controls, drag and drop, and database access, with
a rich design surface
• Support for visual inheritance through master pages
• No project files, allowing projects to be manipulated outside of the
tool
• Integrated Web Administration Tool
• IntelliSense included
• Debugging support
• No “build” step—ability to compile on first run
This feature set is really a combination of features from Visual Studio .NET
and Web Matrix.
SUMMARY
Of course, there are many changes within ASP.NET 2.0—too many to men-
tion in this introduction, although some highlights were covered in this
chapter. The remainder of the book covers these changes (including items
such as changes to existing controls, changes to page attributes, new con-
trols, and so on) in detail.
It’s important to remember that this is a preview technology, still evolv-
ing and still in testing. Despite that, the initial feature set is extremely im-
pressive and provides a leap in productivity for Web site developers.