MigratingfromASP.NET1.xtoASP.NET2.0
Migrating from ASP.NET 1.x to ASP.NET 2.0
jayesh Patel, Bryan Acker, Robert McGovern
Infusion Development
july 2004
applies to:
Microsoft ASP.NET 2.0
Summary: Explores features new to ASP.NET 2.0 from ASP.NET 1.x that offer developers a better set of options for Web development within the .NET Framework. Also discusses how the architecture of ASP.NET has changed to support a greater variety of options for compilation and deployment. (26 printed pages)
Contents
Introduction
computer languages and frameworks evolve as the needs of the development community evolve. Microsoft ASP.NET is certainly no exception. ASP.NET 2.0 is the first major update to the ASP.NET framework and includes solutions to common problems encountered with ASP.NET 1.x as well as new features that greatly increase the flexibility and functionality of Web development on the .NET platform.
this article covers major architectural changes and modifications in the way one develops ASP.NET applications, and also examines several of the important new features offered in ASP.NET 2.0. Most of the topics in this paper assume a background with ASP.NET 1.x. If you have never developed ASP.NET applications before, you may wish to read
Migrating from ASP to ASP.NET 2.0instead.
Goals of ASP.NET 2.0
building on ASP.NET 1.1, the developers of ASP.NET 2.0 chose to focus on four goals:
improve the reliability and usability of Web applications. currently, most ASP.NET 1.x applications run on Microsoft Internet Information Services (IIS) 5.0. ASP.NET 2.0 leverages new IIS 6.0 features for improved performance and scalability. Specifically, IIS 6.0 provides a new process model that greatly enhances the ability of a server to host multiple applications in a truly independent fashion. Each ASP.NET application resides in its own isolated process and cannot accidentally interact with other applications. Simply put, applications can no longer break each other. Each application runs completely separated from every other application. If one application crashes, it doesn't affect other applications.
in terms of usability, new features such as master pages and themes allow you to develop large Web applications using a consistent structure that is manageable and configurable. In addition, changes to ASP.NET 2.0 allow for the automatic creation of Web pages suitable for use on mobile devices. The full integration of the Microsoft Mobile Internet Toolkit into the core ASP.NET 2.0 framework provides a complete platform for creating comprehensive, enterprise-wide applications.
reduce the number of lines of code you have to write in common scenarios. asp.net 2.0 includes wizards and controls that allow you to perform frequent tasks (for instance, data access) without having to write a single line of code. Microsoft Visual Studio 2005 includes designers to layout and configure complex pages with data-bound tables. As a developer, you can use the wizards to work faster and smarter.
asp.net 2.0 also leverages changes in the Microsoft .NET Framework. In particular, the
partial class concept is particularly useful for ASP.NET developers. A partial class lets you write part of the code and lets the ASP.NET compiler write the rest when necessary. You no longer have to see boilerplate code, nor do you have to write any of it.
offer user features to personalize Web applications. asp.net 2.0 includes built-in controls to help you manage user accounts and personalize the content and layout of pages in your application. First, the membership service lets you track users. The new login controls integrate with the membership service to allow you to automate account creation and user login without writing any code. The new Web Parts feature allows you to create Web applications that contain sub-panels known as
web Parts. Users can select and customize the parts that are displayed on a Web page as they see fit. Finally, the Profile service provides long-term persistence of user preferences and data through declarative XML configuration.
provide enhanced design features to generate consistent layouts and design. asp.net 2.0 includes
master pages,
themes, and
skins to build applications with a consistent page layout and design. These new features are easy to implement and modify, and greatly enhance the manageability and maintainability of large applications.
many of the new features in ASP.NET 2.0 are aimed specifically at addressing these goals. Throughout the rest of the paper, we will look at individual technologies and see how ASP.NET 2.0 builds on ASP.NET 1.x to provide a powerful Web application development platform.
Migrating from ASP.NET 1.x
if you have ASP.NET 1.x applications in production, you will be relieved to know that ASP.NET 2.0 is fully backwards compatible. That is, your ASP.NET 1.x applications will run as normal on ASP.NET 2.0 without any changes. However, you will notice many changes when you upgrade to Visual Studio 2005. In particular, the default development model for ASP.NET pages has changed. In addition, with ASP.NET 2.0, you will have a variety of new compilation and deployment options. All of these changes will be discussed in detail in the following sections.
Changes in Architecture
the fundamental architecture of ASP.NET has always been designed for flexibility and extensibility. ASP.NET 2.0 continues this tradition by incorporating a new provider model to support many of the new features. New utilities and API's have been added to improve site maintenance and improve configuration. All of these changes are designed to make developing ASP.NET 2.0 applications a faster and more streamlined process, while still providing the flexibility and extensibility that developers were used to with ASP.NET 1.x.
The Provider Model
many of the new features in ASP.NET 2.0 depend on communication between the Web application and a data store. In order to provide this access in a consistent fashion, ASP.NET 2.0 uses a set of providers. A provider is both a pattern and a point where developers can extend the ASP.NET 2.0 framework to meet specific data-store needs. For example, a developer can create a new provider to support the user identification system, or to store personalization data in an alternate data store.
most custom providers will interact with database backend systems. However, the programmer is free to implement the required provider methods and classes using any medium or algorithm, so long as it meets the model's required interface specification.
ASP.NET 2.0 Providers
the provider model defines a set of interfaces and hooks into the data persistence layer that provides storage and retrieval for specified requests. In this way the provider model acts as a programming specification that allows ASP.NET 2.0 to service unique client concerns.
asp.net 2.0 uses a wide variety of providers, including:
membership. The membership provider supports user authentication and user management.
profile. The profile provider supports storage and retrieval of user-specific data linked to a profile.
personalization. The personalization provider supports persistence of Web Part configurations and layouts for each user.
site Navigation. The site navigation provider maps the physical storage locations of ASP.NET pages with a logical model that can be used for in-site navigation and linked to the various new navigation controls.
data providers. ADO.NET has always used a provider model to facilitate the connection between a database and the ADO.NET API. ASP.NET 2.0 builds upon the data provider by encapsulating many of the ADO.NET data calls in a new object called a data source.
each type of provider acts independently of the other providers. You can therefore replace the profile provider without causing problems with the membership provider.
in the second section of this paper, you will find specific examples of how providers are used with several of the new ASP.NET 2.0 features.
The ASP.NET 2.0 Coding Model
in ASP.NET 1.x, you could develop an ASP.NET page in one of two ways. First, you could put your code directly inline with your ASP.NET tags. The
code inline model is very similar to the coding model that was prevalent with classical ASP and other scripting languages. However, the code inline model has several problems, such as the intermixing of code and HTML. ASP.NET 1.0 introduced the
code-behind model as a replacement. The code-behind model used an external class to house the code, while the ASPX page contained the HTML and ASP.NET tags. The code-behind model thus successfully separated code from content; however, it created some interesting inheritance issues and forced the developer to keep track of two files for each Web page.
although ASP.NET 2.0 still supports both of these models, several significant changes have been made.
Code Inline
the code inline model is now the default model for Visual Studio 2005. Any code you add to the page will automatically be added to a <script> block within the ASPX file instead of to a code-behind class. However, Visual Studio 2005 still displays the code in the code view. In other words, you can keep using Visual Studio like you always have, except that code will be placed directly in the ASPX page instead of a separate class.
figure 1a and 1b. Switching views note that the code is still separated from the content through <script> blocks and placement in the file. However, as a developer, you only have to worry about one file now instead of synchronizing two separate files.
Code Behind
if you want to work with a separate code file, you have to create the file when you create the ASPX page. Fortunately, creating a code-behind file is as simple as clicking a checkbox when you decide to create a new page.
figure 2. Creating a code-behind file the primary difference between a code-behind file in ASP.NET 1.x and ASP.NET 2.0 is that a code-behind file is now a
partial class rather than a full class that inherits from the ASPX page. A partial class is a new .NET construct that allows you to define a single class in multiple source files. In ASP.NET 2.0, a partial class is particularly useful for code-behind files, as it removes the inheritance relationship that is present with the older code behind model.
figure 3. Code behind, old and new the two partial classes (ASPX and code behind) are merged into a single class during compilation. The code-behind file is therefore free of all of the control declarations and inheritance issues associated with the old code-behind model. The most striking difference can be seen in the actual code-behind file, which no longer contains all of the auto-generated code that used to be necessary to maintain the inheritance relationship.
an old code-behind file contained an initialization region, as well as initialization code for every control placed on the page: public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
private void Page_Load(object sender, System.EventArgs e) { }
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Load = new System.EventHandler(this.Page_Load);
}
#endregion
void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Hello ASP.NET 2.0";
}
}
}
a new file removes all of this code since the controls are declared in the ASPX page (the other half of the partial class): namespace ASP {
public partial class Webform1_aspx
{
void Page_Load(object sender, EventArgs e)
{
Label1.Text = "Hello ASP.NET 2.0";
}
}
}
as you can see, the new code-behind file is much cleaner and easier to read. The code-behind file has automatic access to any controls added to the ASP.NET page, and Visual Studio 2005 will provide automatic IntelliSense support and synchronization. Visual Studio also recognizes when you are using a code-behind file, and opens the file rather than the code view when you double-click on a control to access its events.
The /Code directory
the final major change to the coding model is a direct response to a common problem in ASP.NET. Most Web applications require one or more support classes. In ASP.NET, the preferred method was to create a separate project for support classes and then add a reference to the support project within your Web application. Even if you didn't create a separate project, you still had to create a reference to whatever namespace you used within your own application. Although having a separate project made sense for larger and more complicated applications, it was often painful for developers who only needed one or two simple classes.
asp.net 2.0 introduces a new way of adding support code. The /code directory is a special directory that is automatically compiled and referenced by your ASP.NET application. That is, any classes you place inside the /code directory are automatically accessible from any ASPX page in your application. Visual Studio 2005 and the ASP.NET compiler both automatically create an assembly from these classes and place a reference to the assembly in your Web application.
Configuration and Site Maintenance
one of the more difficult challenges as an ASP.NET developer was properly configuring the web.config file. In ASP.NET 2.0 and Visual Studio 2005, you now have several new features to help you with this task.
IntelliSense in web.config
first, Visual Studio's IntelliSense feature has now been extended to any XML file that has a valid schema. In the case of the web.config file, this means that you get full IntelliSense support whenever you edit the web.config file from within Visual Studio.
figure 4. IntelliSense on web.config intellisense helps reduce the chance of misconfigured files. However, ASP.NET 2.0 also includes a new administrative Web site and a Microsoft Management Console to make things even easier.
Administrative Web site
to simplify the process of managing users, ASP.NET 2.0 provides a built in Web site configuration tool. The Web Site Administration Tool is a simple Web site that can only be accessed through a secure connection, or directly on the local host. Through this tool, an administrator can manage the application by configuring services such as user management, the personalization providers, security, and profiles. The tool also allows you to easily configure counters, debugging, and tracing information for your application.
figure 5. Web site administration tool Microsoft Management Console Snap-In
asp.net 2.0 deploys a special Microsoft Management Console (MMC) snap-in for IIS that lets you decide which applications should use which versions of the .NET Framework.
figure 6. MMC display of ASP.NET applications the MMC IIS tab lets you to choose which version of ASP.NET your application uses and displays the Web.config location.
in addition to managing the framework version, the console has an "Edit configuration" button that lets you visually edit most of the Web.config settings without having to directly manipulate the Web.config XML file. As an administrator, you will find that this MMC snap-in provides an incredibly useful tool for configuring and managing multiple ASP.NET applications on a single server.
Enhanced Configuration API's
you can also retrieve and edit configuration information using the
system.configuration.configuration class. These API's let you programmatically access XML configuration files. This lets you to develop custom administration tools. The following code displays the type of authentication enabled for an application on your local machine:
// C# Configuration cfg = Configuration.GetConfigurationForUrl("/Application_name");
Response.Write( cfg.Web.Authentication.Mode.ToString() );
microsoft Visual Basic .NET Dim cfg As Configuration =
Configuration.GetConfigurationForUrl("/Application_name")
Response.Write( cfg.Web.Authentication.Mode.ToString() )
the following code enables Forms-based authentication for a local Web application:
// C# Configuration cfg = Configuration.GetConfigurationForUrl("/MyApp");
cfg.Web.Authentication.Mode = HttpAuthenticationMode.Forms;
cfg.Update();
visual Basic .NET Dim cfg As Configuration = &;amp; _
Configuration.GetConfigurationForUrl("/MyApp")
cfg.Web.Authentication.Mode = HttpAuthenticationMode.Forms cfg.Update()
all four of these features (IntelliSense , administrative Web site, MMC snap-in and configuration API's) help reduce the amount of time and effort you will have to spend configuring your ASP.NET applications.
Changes in Development
asp.net 2.0 and Visual Studio 2005 also change many of the day-to-day aspects of Web application development. In this section, we will look at several of the areas where features available in ASP.NET 1.x have been heavily modified.
Connecting to the Server
in ASP.NET 1.x and older versions of Visual Studio .NET, you had to connect to an IIS instance through Microsoft Front Page Server Extensions. As a developer, you also had to have administrative access to an IIS instance in order to create new Web sites. Many companies were leery of the administrative burden of creating, monitoring, updating, and maintaining extra Web servers running inside the corporate network. In ASP.NET 2.0, connecting to the server has changed.
The Development Server
first, for development, Visual Studio 2005 now comes with a built-in development-only Web server. This lightweight Web server can only respond to local requests and is therefore not a security threat. The server does, however, support full debugging features and can be used as a development tool for new Web applications. When you are ready to test out scalability and performance or deploy for production, simply move the application to IIS.
The Production Server
when you are connecting to an IIS instance, you now have several choices. When you open a Web application project in Visual Studio .NET, you are asked to select a connection method.
figure 7. Connecting to a Web application through FTP you can use Front Page Server Extensions, FTP, Share Point, or several other mechanisms to transfer files to the server and synchronize code.
Compiling ASP.NET 2.0 Applications
another major change to traditional ASP.NET 1.x involves the ways in which Web applications can be compiled. In ASP.NET 1.x, applications were either compiled on first request, or in a batch mode on start up. Both methods had advantages and disadvantages. The most notable shared disadvantage was that you generally had to deploy uncompiled code to a production server. This code could be manipulated directly on the production server, which was either an advantage or a disadvantage depending on your security needs.
asp.net 2.0 offers a new compilation method that pre-compiles your proprietary source code into binary assemblies for deployment. The pre-compiled application consists of assemblies that can be strongly named and signed, and various resource files, such as images that have no significant value to an attacker. The pre-compiled application is therefore much more secure than a normal ASP.NET application.
Site Navigation
web sites require consistent navigation to provide a pleasant user experience. Traditional ASP applications rely on adding hyperlinks, or user controls that encapsulate hyperlinks. Creating these hyperlinks is a time consuming process and often causes problems when pages are moved to new locations within the application. ASP.NET 1.x did not have a useful solution to this problem. You still had to encode links in HTML tags or within ASP.NET control properties. ASP.NET 2.0 offers many new features to improve site navigation and reduce the maintenance tasks of changing the physical locations of Web pages.
asp.net 2.0 allows you to define your application's navigation based on a logical structure. Using a logical structure for navigation allows you to create a navigation path for your application by logically relating Web pages to one another, instead of depending on the physical location of each page on the server. By organizing the navigation logically, you can reorganize your application's navigation without modifying any code. You can also use the logical structure to create navigational aids such as tree views, menus, and "bread crumb" trails.
The web.sitemap File
the ASP.NET 2.0
sitemap class exposes the logical structure of your Web site. You can define the underlying structure using XML with the site navigation provider included in ASP.NET 2.0, or you can use any other data format with a custom provider. Once you have defined the layout, you can use the navigational structure in many different ways.
a logical structure can be created in two easy steps:
Create an XML file named web.sitemap that lays out the pages of the site in a hierarchical fashion. Visual Studio 2005 allows you to create new .sitemap files ("Add New Item" and select the sitemap template), and also provides IntelliSense support when editing sitemap files. <?xml version="1.0" encoding="utf-8" ?>
<siteMap>
<siteMapNode title="Home" url="default.aspx">
<siteMapNode title="Article 1"
url=""/articles/demoarticle1.aspx" />
<siteMapNode title="Article 2"
url=""/articles/demoarticle2.aspx" />
<siteMapNode title="Article 3"
url=""/articles/demoarticle3.aspx" />
</siteMapNode>
<siteMapNode title="Picture Gallery"
url=""/PhotoAlbum/PhotoAlbums.aspx">
<siteMapNode title="Meetings"
url=""/PhotoAlbum/PictureAlbum.aspx?albumid=1"/>
<siteMapNode title="Activities"
url=""/PhotoAlbum/PictureAlbum.aspx?albumid=2"/>
<siteMapNode title="Training"
url=""/PhotoAlbum/PictureAlbum.aspx?albumid=3"/>
</siteMapNode>
</siteMapNode>
</siteMap>
Drag a
sitemapdatasource control from the Toolbox and drop it on a page. The
sitemapdatasource control will automatically bind to the logical site structure contained in the web.sitemap file.
once the
sitemapdatasource control is on a page, you can easily bind it to other controls such as the
treeview control or the
menu control. <%@ page language="VB" master=""/Mysite.master" %>
<asp:content id="Content1" contentplaceholderid="LeftSideContent">
<H2>Navigation </h2>
<asp:treeview id="Navigation tree" runat="server"
datasourceid="NavSource"/>
</asp:content>
the tree view will display site navigation using a hierarchical view based on the definitions in the app.sitemap file.
Navigation Controls
asp.net 2.0 also introduces a set of navigation controls that can be used with the site navigation service. By using the
<asp:treeview> or
<asp:menu> controls, you can create a visual navigation structure for your application. The new
<asp:sitemappath> control can be used to generate a "bread crumb trail" navigation structure.
figure 8. Bread crumb trail for the Home | Articles | Article 2 page URL Mapping
another new navigation-related feature in ASP.NET 2.0 is URL mapping. Web sites often require long, complicated, and convoluted URLs (think of an MSDN reference!). If you wanted to hide your URLs in a classical ASP application, you had to write a custom ISAPI handler to pre-process requests. ASP.NET 2.0 adds the URLMapping configuration section to the Web.config file. A developer can define a mapping that hides the real URL by mapping it to a more user friendly URL. <urlMappings enabled="true">
<add url=""/Home.aspx" mappedUrl=""/Default.aspx?tabid=0" />
</urlMappings>
the configuration shown above would map (and redirect) all requests for Home.aspx to Default.aspx?tabid=0. Users can bookmark the short link, and the short link will be displayed on their browser.
Data Access and Data Sources
asp.net 1.x data access leveraged ADO.NET connections and commands to provide data that could be bound to various ASP.NET controls. ASP.NET 2.0 improves on the relationship with ADO.NET by providing data sources that encapsulate the code to create both connections and commands in a single XML tag within the Web.config file. As with ASP.NET 1.x, you can use a wizard inside of Visual Studio 2005 to create these data sources. ASP.NET 2.0 ships with data sources for:
microsoft Access. The access data source can automatically connect to and query an Access database.
objects. The
ojbectdatasource control is a data-layer abstraction on top of a middle tier or other set of objects. You can use the Object Data Source when you want to treat one or more sets of objects as data that can be linked to data-bound controls.
dataset. The
datasetdatasource links to partially tabular data, such as a comma-separated file or XML file.
xml. The
xmldatasource links to hierarchical data in an XML file. The primary difference between the
xmldatasource and the
datasetdatasource is that the
xmldatasource is better suited for binding to hierarchical controls, like a
treeview, whereas the
datassetdatasource is better suited to tabular controls, like the
datagrid.
site Map. The
sitemapdatasource provides a data source for the site navigation controls, including bread crumbs.
for more details on data sources, see
Data Access in ASP.NET 2.0.
New Data-Bound Controls
asp.net 2.0 also includes two new data-bound controls. The first control,
gridview, expands on the
datagrid by providing configurable code for editing cells, displaying rows on multiple pages, sorting, deletion, selection, and other common behaviors.
figure 9. Configuring a GridView the second control,
detailsview, provides a detail view that complements both
gridview and
datagrid.
figure 10: DetailsView control the
detailsview control displays one record at a time, giving you much greater control over how a record is displayed, edited, deleted, or created.
Web Applications for Mobile Devices
if you have ever developed Web applications for ASP.NET, you have encountered the Microsoft Mobile Internet Toolkit (MMIT) and the special mobile controls. In ASP.NET 2.0, the MMIT and mobile controls have been rolled into the main framework. However, ordinary ASP.NET 2.0 controls now use an adaptive rendering model that provides the same behavior offered by the older mobile controls, only in a more flexible and easy-to-use package.
New Features in ASP.NET 2.0
almost every major update to an API or framework involves both changes to existing features and new features and enhancements. In this section, we will look at several of the new features available in ASP.NET 2.0.
Master Pages
master pages are a new feature introduced in ASP.NET 2.0 to help you reduce development time for Web applications by defining a single location to maintain a consistent look and feel in a site. Master pages allow you to design a template that can be used to generate a common layout for many pages in the application.
the primary goal of master pages is to avoid creating each page from scratch and having to repeat the layout code. Another benefit of using master pages is that if you want to change the layout of the pages in the application, you only have to update the master page rather than each individual page. This feature is somewhat similar to the Microsoft Windows Form technique of
visual Inheritance, available with the original version of the .NET Framework, and is used for desktop application development.
a master page looks like any ordinary ASP.NET Web page, except for the extension (.master instead of .aspx) and some special controls and header fields. Master pages must contain one or more
<asp:contentplaceholder> controls. These controls represent areas of replaceable content. Basically, anything that is not in a
contentplaceholder will appear on any page that uses the master page.
visual Studio 2005 automatically creates most of this code for you, so you don't have to write complex HTML code for your page layout. A master page must also include the following default source code: <%@ master language="C#" compilewith="site.master.cs"
classname="ASP.site_master" %>
<html>
<head runat="server"><title>Untitled Page</title></head>
<body>
<form runat="server">
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</form>
</body>
</html>
other than these key changes, a master page can contain any HTML or control that can be found on a normal ASP.NET page.
although master pages and frames serve a similar purpose, master pages offer much more functionality. Unlike frames, using master pages allow you to:
Bookmark a page and recall all the information on the specific page, not just the default frame page. A master page isn't really a frame. It's a single page that contains collated content from the master page and the content page that builds on the master. Therefore it looks and acts like a single Web page rather than a frame.
Work by means of controls and tags rather than HTML. Thanks to Visual Studio, you don't have to worry about opening and closing frame tags or modifying countless HTML attributes to ensure that each frame displays in the correct fashion. You can simply create the place holder and modify its properties through Visual Studio.
Leverage Visual Studio's code creation to visually design the layout, manage the frames, and provide all of the plumbing to link the content pages into the master page. You can add new content without having to worry that the overall HTML layout of the page will be affected.
in short, master pages greatly simplify the task of making a Web application look consistent, regardless of how many pages are involved.
Content Pages
content pages are attached to a master-page and define content for any
contentplaceholder controls in the master page. The content page contains
<asp:content> controls that reference the
<asp:contentplaceholder> controls in the master page through the
contentplaceholder ID. The content pages and the master page combine to form a single response. <%@ page language="VB" master=""/Mysite.master" %>
<asp:content id="Content1" contentplaceholderid="LeftSideContent">
<H2>Navigation </h2>
<asp:treeview id="Navigation tree" runat="server"
datasourceid="NavSource"/>
</asp:content>
<asp:content id="Content1" contentplaceholderid="RightSideContent">
<asp:label runat="server">Support section</asp:label>
</asp:content>
once again, the user actually makes a request for the content page. ASP.NET notices the reference to the master page and renders the master page content in combination with the content page.
Nested Master Pages
in certain instances, master pages must be nested to achieve increased control over site layout and style. For example, your company may have a Web site that has a constant header and footer for every page, but your accounting department has a slightly different template than your IT department.
figure 11. Nested Master Pages by nesting individual department master pages within the top-level company master page, you can implement high levels of consistency for each department while controlling what can be overridden by content and master pages.
the key point to remember is that the end user requests one page and receives one page, even though the application may be compiling content from multiple master pages and content pages.
Overriding Master Pages
although the goal of master pages is to create a constant look and feel for all of the pages in your application, there may be situations when you need to override certain content on a specific page. To override content in a content page, you can simply use a content control.
to override a master page dynamically, you have to programmatically expose the public properties of the
masterpage class, including those you create. Therefore, if you want to override the
<title> and
<keyword> properties in the
<head> section of your HTML pages, you can build a function to edit the default properties set in the master page: <head runat="server" id="Head1">
<title><% =m_HtmlTitle %></title>
<meta name="keywords" c >
<meta name="description"
c >
<LINK href="<% =Request.ApplicationPath %>/portal.css"
type="text/css" rel="stylesheet">
</head>
Themes and Skins
currently, ASP developers must use Cascading Style Sheets (CSS) and inline styles to enforce a look and feel on a Web site. Although these technologies help, consistency is still primarily a function of developer discipline in using the correct styles. ASP.NET 2.0 rectifies this issue through the use of themes and skins, which are applied uniformly across every page and control in a Web site.
Themes
themes are similar to CSS style sheets in that both themes and style sheets define a set of common attributes that apply to any page where the theme or style sheet is applied. However, themes differ from style sheets in the following ways:
Themes can define many properties of a control or page, not just a specific set of style properties.
Themes can include auxiliary files (for example, graphics) that can't be included in a CSS style sheet.
Themes do not cascade the way style sheets do (for example, theme property values always override local property values).
Themes can include style sheet references.
each application has a themes directory. Each specific theme has its own subdirectory that contains skin files and any other files that apply to the style.
Defining a Theme
you can define themes and skins and deploy them in subdirectories stemming from the Themes directory. Each subdirectory constitutes a theme. A theme subdirectory contains .skin files as well as any other resources used by the theme (that is, image files and style sheets).
figure 12. Themes sub directory the actual skin definitions are contained in the .skin files and look very much like the tags used to declare control instances in ASPX files.
for example, in the Themes directory, create a subdirectory named Pink. To create a .skin file for your theme, simply add the following code and save it in the Pink directory: <asp:DropDownList runat="server" BackColor="hotpink"
ForeColor="white" />
<asp:DataGrid runat="server" BackColor="#CCCCCC" BorderWidth="2pt"
BorderStyle="Solid" BorderColor="#CCCCCC" GridLines="Vertical"
Horiz>
<HeaderStyle ForeColor="white" BackColor="hotpink" />
<ItemStyle ForeColor="black" BackColor="white" />
<AlternatingItemStyle BackColor="pink" ForeColor="black" />
</asp:DataGrid>
this theme can then be applied to pages in your application. Of course, it will turn the background of your data grid hot pink, which may not be a desirable effect.
Using a Theme
themes can be applied:
At page level using a single tag: <% @page language="VB" theme="Pink" %>.
Site-wide using a configuration element inside of the Web.config file: <pages theme="Pink" />. The Web.config file is the master configuration file for each ASP.NET application.
the effects of a theme can be seen if you examine the common calendar control in Figure 13.
figure 13. Calendar with Basic Blue theme this version of the calendar uses the Basic Blue theme. By simply changing the theme attribute, you can completely change the look of the calendar, as shown in Figure 14.
figure 14. Calendar with the Smoke and Glass theme Skins
a skin is a set of properties and templates that can be used to standardize the size, font, and other characteristics of controls on a page. You can use skins to create pre-defined display settings for a control and apply the appropriate skin at run time. You might, for example, select a skin based on a user preference, or determine the appropriate skin based on the browser used to access the page. Optional skins can be applied to pre-defined controls by setting the
skinid property <!- Default Skin -->!>
<asp: label runat="server" Font-names="verdana, arial" font-size="10pt"
ForeColor= "#000066" BackColor="transparent"
/>
<!- Title Skin -->!>
<asp: label runat="server" id="foo" skinid="Title"
Font-names="verdana, arial"
font size="18pt" ForeColor= "#000066"
BackColor="transparent" font-bold="true"
font-underline="true"
/>
once a skin has been defined, you can apply it to all the controls on a page or to a specific control using themes and the
skinid property built into all skinable controls.
Applying a Skin to a Control
if a default skin exists and the page is defined by a theme, the default skin will automatically be applied to a control. If you define the
skinid property for the control, the default skin will be replaced by the skin referenced in the
skinid property. This property can be configured during development or at run time (with a page refresh).
Membership
one of the major drawbacks of ASP development is that user management schemes must be created from scratch. This process is not only complex, but time consuming. The membership provider and login controls in ASP.NET 2.0 provide a unified way of managing user information.
any time a user logs into your application, you can automatically reference their identity as well as basic user information. The user's credentials are securely stored in a backend user database that you can configure in the Web.config file. ASP.NET 2.0 provides both Microsoft Access and Microsoft SQL Server providers, but you can create custom providers for any type of backend data store. This extensibility is extremely helpful if you already have an account database that you want to use with the membership feature in ASP.NET. After you configure membership for your application, you can use Login Controls in ASP.NET 2.0 to automate user registration and password retrieval.
Login Controls
asp.net 2.0 offers new login controls to help create and manage user accounts without writing any code. You can simply drag a
<asp:createuserwizard> control onto a page to create a user registration form that offers a step-by-step wizard to walk users through the registration process.
the new login controls provide an intuitive interface that allows you to format the controls to match the design of your application. The properties window lets you access the label, value, and error message validation elements for each property. By using the
loginname and
loginstatus controls, you can now give each user a personalized greeting as well as create login/logout functionality without writing any code.
the
loginview control allows you to determine which content is displayed to the user, without writing a single line of code. The content is displayed by examining the status and role of each user to determine an appropriate view. In traditional ASP applications, you had to write code to identify the current user, additional code to validate the user's status, and then even more code to display content based on the user.
for a more detailed description of Web parts, please read
Personalization with ASP.NET 2.0.
Profiles
the ASP.NET 2.0 profile features allow you to define, save, and retrieve information associated with any user that visits your Web site. In a traditional ASP application, you would have to develop your own code to gather the data about the user, store it in session during the user's session, and save it to some persistent data store when the user leaves the Web site. ASP.NET 2.0 automates all of this functionality with profiles. A profile is essentially a bucket of information associated with a user, and is directly accessible through the
profile object that is accessible from every ASPX page.
Defining a Profile
within machine.config or Web.config, you can define a profile with
<property> values that represent information such as name, billing address, and e-mail addresses for each user. You can even create groups of logical properties. <profile>
<group name="BillingAddress">
<add name="Street" type="System.String" />
<add name="City" defaultValue="Toronto" type="System.String" />
<add name="StateProv" type="System.String" />
<add name="ZipPostal" type="System.String" />
</group>
</profile>
once you have defined the profile, ASP.NET and the profile provider automatically take care of managing this information, including loading it on request and storing it when the user leaves your site.
Using Profiles
once you have defined a profile, Visual Studio 2005 automatically exposes the profile properties through the
profile object.
figure 15. Using profiles visual Studio 2005 also provides full IntelliSense support for profiles. If you ever make a change to the profile definition, Visual Studio will automatically provide the correct IntelliSense as soon as you have saved your Web.config file.
Web Parts
one of the major differences between a Web application and a desktop application has been the ease with which a desktop application can contain multiple configurable components. For example, consider the Visual Studio IDE itself. A user can decide which windows to display and how they are arranged. Developing similar functionality in a Web site is a daunting prospect.
asp.net 2.0 introduces a solution to this problem in the form of Web Parts. Web Parts are modular components that can be included and arranged by the user to create a productive interface that is not cluttered with unnecessary details. The user can:
Choose which parts to display.
Configure the parts in any order or arrangement.
Save the view from one Web session to the next.
Customize the look of certain Web Parts.
all of these features are practically impossible to implement with ordinary Web applications.
you can think of Web Parts as modular Web page blocks. Each block can be added or removed from the Web page dynamically, at run time. Code for organizing and manipulating Web Parts is built into ASP.NET 2.0. All of the functionality for adding, removing, and configuring layout is automatically handled by the Web Parts system. The programmer simply builds Web Parts and assigns them to Web Part Zones. A user can mix and match Web Parts, display them in any order, and expect the configuration to be saved between site visits.
Using Web Parts
for example, a Web Part application for a hospital may let users choose from a variety of display components ranging from current patient status to alerts on drug interactions. Each user can choose which parts to display from a catalog:
figure 16. A Web Parts catalog the user can then drag the controls into an arrangement that makes the most sense for his or her particular needs.
figure 17. Arranging Web Parts the layout and configuration are automatically stored for the user's next visit. For a more detailed description of Web Parts, please read
Personalization with ASP.NET 2.0.
Conclusion
asp.net 2.0 continues in the footsteps of ASP.NET 1.x by providing a scalable, extensible, and configurable framework for Web application development. The core architecture of ASP.NET has changed to support a greater variety of options for compilation and deployment. As a developer, you will also notice that many of your primary tasks have been made easier by new controls, new wizards, and new features in Visual Studio 2005. Finally, ASP.NET 2.0 expands the palette of options even further by introducing revolutionary new controls for personalization, themes and skins, and master pages. All of these enhancements build on the ASP.NET 1.1 framework to provide an even better set of options for Web development within the .NET Framework.
Related Books
A First Look at ASP.NET V. 2.0ASP.NET 2.0 Revealed about the authors Jayesh Patel is a developer in both .NET and Java Technologies. Jay's research focuses on pattern-based programming and agile methodologies.
Bryan Acker is a technical writer for Infusion Development. Bryan has a strong background in ASP and ASP.NET Web development and Web hosting.
Robert McGovern is a senior writer, developer, and project manager for Infusion Development. Rob has worked on several different ASP.NET projects, including CodeNotes for ASP.NET and the JSP to ASP.NET migration guide.
infusion Development Corporation is a Microsoft-Certified Solutions Provider offering customized software-development, training, and consulting services for Fortune 1000 Corporations, with an emphasis on the financial-services industry. With offices in New York and Toronto, Infusion Development has established an international client base, including some of the world's largest companies in the financial service, securities brokerage, and software development industries. The employees of Infusion Development are also the authors and creators of the CodeNotes book series. Add by : Huobazi (2005-5-09:07:48)
| 感谢原创者的辛勤劳动,希望对您有所帮助,转载请注明原出处。 |