您的位置:首页 > 编程语言 > ASP

The New Controls of ASP.NET 2.0

2006-02-24 14:00 399 查看
ASP.NET VERSIONS: 2.0 (Beta 2)

The New Controls of ASP.NET 2.0

Microsoft has outdone itself

By Steve C. Orr

The number of controls included in ASP.NET 1.x was impressive, but Microsoft has really outdone itself with ASP.NET 2.0 — so much so that I predict you’ll be more productive than ever with the dozens of new controls at your disposal. Following is a brief introduction to the new controls in ASP.NET 2.0 so you can be ready to dive in and start working with all these time-saving goodies.

Let’s Get Started

The new ImageMap control extends the Image control with a collection of clickable hot spots. You can create (and configure the dimensions of) as many rectangle, circle, and polygon hotspots as you need. The HotSpotMode property can be used to specify whether the user’s click should cause a postback or navigate the user directly to a URL.

The new BulletedList control is nothing revolutionary, but when you need to display a bulleted list on your page, this control is a perfect fit. In addition to the databinding capabilities you’d expect, there are also such useful properties as BulletStyle, which can be set to circles, squares, numbers, roman numerals, custom images, etc. With the DisplayMode property you can specify that the list items be represented by standard text, HyperLinks, or LinkButtons.

The HiddenField and FileUpload controls are little more than empty shells around the standard old HTML controls. If you’ve used the equivalent HTML controls you already know how to use these. If you are intimidated by the HTML tab of your Visual Studio toolbox, then these new Web controls will let you stay at home in the Web Controls tab.

The new MultiView control works in concert with embedded View controls. Each View control acts as a content container, and behaves very much like a Panel control at design time. At run time the MultiView control manages the View controls to ensure only one is visible at a time. Use the ActiveViewIndex property of the MultiView control to specify which View should be visible at any particular time. By keeping each of the views on the same page you can negate the need to persist data between pages, which can be troublesome in ASP.NET (compared to Windows Forms applications).

New View Controls

Description

MultiView

The MultiView control manages multiple View controls to ensure only one is visible at a time.

View

Each View control can contain any combination of controls you desire.

FormView

The FormView control contains seven templates, which can be modified and displayed in varying combinations to support a variety of functionality.

DetailsView

The DetailsView control displays the details of a database record on a page. The automated display and editing capabilities provide the potential to eliminate lots of boilerplate code.

GridView

A feature-rich replacement for the DataGrid.

LoginView

Displays different sets of controls based on the user’s login status.

TreeView

One of the most feature-rich controls of ASP.NET 2.0, but not truly a View control in the same sense as these other View controls.

Figure 1: View controls automatically decide which sets of controls to display based on run time circumstances. They support data binding and editing.

The new Wizard control takes the MultiView concept to the next level. This rich control lets you create a fully functional wizard to lead users through a sequential set of steps. You can create and configure as many steps as you need in the wizard at design time via the new SmartTag technology of the Visual Studio 2005 designer. The designs you can create are highly customizable because of a flexible object model that permits the use of Templates to represent one or more of the steps. The wizard manages the Back and Next buttons appropriately and persists all control values between each step so you don’t have to worry about such mundane details.

The new Substitution control works in cooperation with the OutputCache directive that’s been around since ASP.NET 1.x. Specifically, it acts as an exception to the OutputCache directive. Set the MethodName property to a valid function name in the page. Every time the page is requested, this function will be called and the return value will be automatically displayed in the Substitution control. This way you can be sure the contents displayed in the control will always be fresh, even though the rest of the page may be pulled from the cache.

Say Goodbye to the DataGrid

The DataGrid control was good for its time, but its time is over. It still exists for backward compatibility purposes, but it’s no longer listed in the Visual Studio 2005 toolbox by default. The replacement is the new GridView control, which is better in every way. The paging and sorting functionality is now far more automated, and yet still extensible in case you want to get your hands dirty. The GridView control renders more efficiently than the DataGrid control did, and it supports a wider variety of column types: BoundField, ButtonField, CheckboxField, CommandField, HyperLinkField, ImageField, and TemplateField.

Unfortunately, the GridView control still performs most of its functions via postbacks and server-side code, which can result in less-than-stellar performance. However, the GridView control does[/i] provide an EnableSortingAndPagingCallback property that enables the server-side sorting and paging routines to be called without a full-page postback; only the GridView control calls back to the server and gets updated while the rest of the page stays put.

The GridView can also provide editing capabilities, with appropriate controls automatically chosen for every field and the ability to customize them when you see fit.

The GridView control is great for displaying multiple rows of data, but how should a developer handle the common drilldown scenario of providing a detailed single record view when the user clicks on a row in the grid? In ASP.NET 1.x this requires quite a bit of code and probably a whole new page. In ASP.NET 2.0, the new DetailsView control comes to the rescue. Like the GridView, it automatically displays appropriate controls when in edit mode and allows for detailed customizations via templates. The DetailsView control can be used by itself, but when combined with the GridView control it provides an elegant solution to many common Master/Detail scenarios.

The new FormView control provides similar, less automated functionality. The view must be configured via templates at run time or else nothing will appear on the form at run time. This isn’t such a bad thing now that templates can be edited visually in the designer (a new feature provided by Visual Studio 2005). The FormView control provides seven templates that can be customized, resulting in a wide range of potential functionality for you to mold as needed.

Powerful New Data Controls

Data binding has taken on a whole new dimension in ASP.NET 2.0. In most cases, controls can be fully data bound declaratively, with no code required. This is made possible by the new DataSource controls. The SqlDataSource[/b] control pulls data from a SQL Server database; the AccessDataSource control pulls data from an Access database. Likewise, the XMLDataSource control retrieves data from an XML file or stream.

DataSource Controls

Description

SqlDataSource

Used for data binding to a Microsoft SQL Server (or SQL Server Express) database.

AccessDataSource

Used for data binding to a Microsoft Access database.

XMLDataSource

Used for data binding to XML files and streams.

ObjectDataSource

Used for data binding to custom middle-tier business objects.

SiteMapDataSource

Used for binding to a Site Map, which is stored in XML by default.

Figure 2: The new DataSource controls eliminate reams of data access code by taking care of tedious details, such as creating connections and parameterizing queries.

In keeping with the ASP.NET 2.0 theme of “productivity,” these controls will eliminate the need for a lot of boring, boilerplate code that you’ve likely written time and time again. Although data binding in ASP.NET 1.x was nice, every data-bound control still required many lines of code for doing routine things, such as retrieving connection strings, opening connections, creating SQL parameters, managing DataAdapters, etc. In many projects that kind of code will be completely eliminated because each of these new DataSource controls basically combines the functionality of the old Connection, Command, and DataAdapter components (which are still included, but are no longer in the toolbox by default). They can also handle caching and filtering.

Those DataSource controls can certainly be useful, but scalable applications require middle tiers, and therefore controls often cannot be bound directly to a database. That’s where the ObjectDataSource control comes in. It can retrieve data from custom middle-tier objects you may have created to encapsulate database access, business rules, and other such details.

Finishing off the DataSource controls is SiteMapDataSource[/b]. This control manages the portrayed navigational structure of a Web site. By default the site structure is stored in an XML file, although the SiteMapDataSource control can be configured to load from a database or other custom data store if you prefer. The SiteMapDataSource control works in conjunction with the navigation controls.

Navigation Controls

The new SiteMapPath control displays the user’s current location in the navigational hierarchy of the Web site. Each level is represented with a hyperlink (as illustrated below) so the user can quickly jump to their desired destinations:

Computers > Software > Utilities > Virus Detection

It’s easy to alter the user’s view of the Web site structure (which doesn’t necessarily need to match the Web site’s physical structure) because the data is retrieved from a SiteMapDataSource control, which loads the current structure from an XML file by default.

ASP.NET has always been in desperate need of a Menu[/b] control, and now we finally have one! This high-quality hierarchical menu control acts just like you’d expect, and can appear vertically or horizontally. This exceptionally configurable control can be bound to a SiteMapDataSource control or edited independently.

Treeview controls are a staple of most modern languages, but mysteriously one was not included in ASP.NET 1.x. That oversight has been corrected in ASP.NET 2.0. The ImageSet property lets you automatically display a variety of useful and attractive images in front of each node. While the default behavior is to post back between each node click and raise a SelectedNodeChanged event, the Target property lets you specify another frame in which to display the address in the NavigateURL property, so no postback is required. You can also use the SelectAction property to specify other behaviors, such as expanding the node without a postback, doing nothing, or expanding the node and raising a postback event. Each node can optionally have a checkbox.

The TreeView nodes can be configured at design time, bound to a SiteMapDataSource control, or, for larger tree structures, sub nodes can be loaded on demand to conserve server resources. This requires a postback after many of the user’s mouse clicks — except for uplevel browsers, which can take advantage of the automatically generated client-side code to call back to the server and fill in the nodes without requiring a full-page postback.

Secure Your Site with Login Controls

Forms Authentication was a great advancement in ASP.NET 1.x because it eliminated much of the boilerplate code needed for basic authorization tasks. The new Login controls of ASP.NET 2.0 take it to the next level. In many cases, not a single line of code will need to be written to fully secure a Web site. All the required user management, admin pages, and database management is handled automatically by the new controls and their underlying infrastructure. Even a database is provided automatically, although you can extend the authentication system in nearly any way imaginable for cases where you prefer to take control.

The new Login control provides a user name and password screen that you may have created dozens of times before. However, you’ll never need to create one ever again, now that it’s been done in such a thoroughly reusable and extensible way.



Figure 3: New Login controls provide Web sites with a fully functional user management system, with no code required. The Login and PasswordRecovery controls are pictured here.

The new LoginStatus control displays in two different states. If the user is not logged in, it provides a hyperlink to the login page. If the user is logged in, it provides a hyperlink that logs them out. In either case, you can configure the hyperlink text that is displayed.

The LoginView control is similar to the LoginStatus control, except instead of displaying a simple link, it will display whatever set of controls you’d like based on the user’s login status. It can display one set of controls for logged-out users, and another set of controls for logged-in users. It can also display varying sets of controls based on the current user’s role.

Any decent authentication system must provide users with a way to change their password. The ChangePassword control provides such functionality by providing the standard three-textbox user interface for old password, new password, and verify new password. By default it accepts only strong passwords, and you can configure it to accept whatever combination of letters, numbers, and symbols your requirements demand.

Any decent authentication system must also provide a way to deal with forgotten passwords. That’s where the new PasswordRecovery control comes in handy. This flexible control can handle a variety of common techniques for dealing with forgotten passwords, such as secret question/answer pairs, e-mailing a person their password, and/or generating a new password.

The LoginName control displays the person’s user name when they’re logged in (or when it’s available automatically through Windows Authentication); it’s invisible when the user is not logged in.

The CreateUserWizard control is a functionally rich control that allows a user to enter all the standard information a site generally requires to create a new user, such as their desired username, password, e-mail address, etc. It’s fully extensible, allowing you to add whatever custom fields you require. Because it inherits from the Wizard Web control, you can easily divide the user registration process into as many steps as you desire.



Figure 4: You may never need to open another web.config file. Built-in administration screens make it easy to configure Web site settings and manage users.

WebPart Controls

WebParts are all about personalization. There is an entire infrastructure underlying ASP.NET 2.0 that can handle personalization and authentication. The WebPartManager control’s purpose is to manage and orchestrate all the WebPart controls on the page. It is not visible at run time.

Each instance of the WebPartZone control can hold one or more WebParts. Each WebPartZone can be minimized, moved, deleted, or added by the developer and (if configured to do so) by the user. Each user can configure the page to their own liking and the customizations will be remembered and persisted between each of their visits.

The CatalogZone control contains CatalogPart controls, such as DeclarativeCatalogPart, PageCatalogPart, and ImportCatalogPart. These controls are used to create a catalog of WebParts that users can choose to add to their page.

EditorPart controls (such as the AppearanceEditorPart, BehaviorEditorPart, LayoutEditorPart, and PropertyGridEditorPart[/b]) can only be placed within an EditorZone control. These controls are used to allow users to edit and personalize pages.

The ConnectionZone control contains ConnectionPart controls. These are used to create connections between WebParts on a page. They permit one WebPart to send information to another WebPart.

Finally, the ProxyWebPartManager control provides a way to specify static connections inside a content page when the WebPageMaster class has been added to a Master Page.

New Report Controls

Reporting options were limited in ASP.NET 1.x, so it’s good to see a wider selection in this area. The new ReportViewer control allows you to design reports visually from a DataSet or other data source. It can cache reports, generate reports on demand, retrieve pre-generated reports from SQL Server 2005 Reporting Services, and more.

If you’re already using the CrystalReportViewer control from ASP.NET 1.x, you’ll be pleased to know that it has been joined by two new companion controls: CrystalReportPartsViewer and CrystalReportSource. Both of these controls are invisible at run time. The CrystalReportSource control manages report data sources, parameters, and caching.

In the End

That covers all the new Web controls that appear by default in the Visual Studio 2005 (Beta 2) toolbox. There are, however, many other new classes in the .NET Framework that inherit from System.Web.UI.Control and System.Web.UI.WebControl. The limited space here dictates that their introductions will have to wait for future articles.

And don’t forget about the old ASP.NET 1.x controls, because many of them have enhancements worthy of note. For example:

Button controls now support posting to other pages and have a declarative client-side OnClientClick event.
The Panel control now has scrollbar support so you can easily create scrolling areas within your page.
The Validation controls have been enhanced to support multiple independent validation groups within a single page.

Quite a few of the controls listed in this article deserve full articles of their own, so keep an eye out — they’ll soon be appearing all over the web.

This article was originally published in ASP.NET Pro Magazine.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐