June 27, 2005 12:06 AM

The New ASP.NET 2.0 Controls

Microsoft Has Outdone Itself
DevConnections
Rating: (0)

ControlFreak

LANGUAGES:VB.NET | C#

ASP.NETVERSIONS: 2.0 (Beta 2)

 

The New ASP.NET 2.0 Controls

Microsoft Has Outdone Itself

 

 

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

 

Let?s Get Started

The new ImageMapcontrol extends the Image control with a collection of clickable hot spots. Youcan create (and configure the dimensions of) as many rectangle, circle, andpolygon hotspots as you need. The HotSpotMode property can be used to specifywhether the user?s click should cause a postback or navigate the user directlyto a URL.

 

The new BulletedListcontrol is nothing revolutionary, but when you need to display a bulleted liston your page, this control is a perfect fit. In addition to the databindingcapabilities 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 berepresented by standard text, HyperLinks, or LinkButtons.

 

The HiddenFieldand FileUpload controls are littlemore than empty shells around the standard old HTML controls. If you?ve usedthe equivalent HTML controls you already know how to use these. If you areintimidated by the HTML tab of your Visual Studio toolbox, then these new Webcontrols will let you stay at home in the Web Controls tab.

 

The new MultiViewcontrol works in concert with embedded Viewcontrols. Each View control acts as a content container, and behaves very muchlike a Panel control at design time. At run time the MultiView control managesthe View controls to ensure only one is visible at a time. Use theActiveViewIndex property of the MultiView control to specify which View shouldbe visible at any particular time. By keeping each of the views on the samepage you can negate the need to persist data between pages, which can betroublesome 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: Viewcontrols automatically decide which sets of controls to display based on runtime circumstances. They support data binding and editing.

 

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

 

The new Substitutioncontrol works in cooperation with the OutputCache directive that?s been aroundsince ASP.NET 1.x. Specifically, it acts as an exception to the OutputCachedirective. Set the MethodName property to a valid function name in the page. Everytime the page is requested, this function will be called and the return valuewill be automatically displayed in the Substitution control. This way you canbe sure the contents displayed in the control will always be fresh, even thoughthe 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 timeis over. It still exists for backward compatibility purposes, but it?s nolonger listed in the Visual Studio 2005 toolbox by default. The replacement isthe new GridView control, which isbetter in every way. The paging and sorting functionality is now far moreautomated, and yet still extensible in case you want to get your hands dirty. TheGridView control renders more efficiently than the DataGrid control did, and itsupports a wider variety of column types: BoundField, ButtonField,CheckboxField, CommandField, HyperLinkField, ImageField, and TemplateField.

 

Unfortunately, the GridView control still performs most ofits functions via postbacks and server-side code, which can result inless-than-stellar performance. However, the GridView control does provide anEnableSortingAndPagingCallback property that enables the server-side sorting andpaging routines to be called without a full-page postback; only the GridViewcontrol calls back to the server and gets updated while the rest of the pagestays put.

 

The GridView can also provide editing capabilities, withappropriate controls automatically chosen for every field and the ability tocustomize them when you see fit.

 

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

 

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

 

Powerful New Data Controls

Data binding has taken on a whole new dimension in ASP.NET2.0. In most cases, controls can be fully data bound declaratively, with nocode required. This is made possible by the new DataSource controls. The SqlDataSource control pulls data from aSQL Server database; the AccessDataSourcecontrol 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 newDataSource controls eliminate reams of data access code by taking care oftedious 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 codethat you?ve likely written time and time again. Although data binding in ASP.NET1.x was nice, every data-bound control still required many lines of code fordoing routine things, such as retrieving connection strings, openingconnections, creating SQL parameters, managing DataAdapters, etc. In manyprojects that kind of code will be completely eliminated because each of thesenew DataSource controls basically combines the functionality of the oldConnection, Command, and DataAdapter components (which are still included, butare no longer in the toolbox by default). They can also handle caching andfiltering.

 

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

 

Finishing off the DataSource controls is SiteMapDataSource. This control managesthe portrayed navigational structure of a Web site. By default the sitestructure is stored in an XML file, although the SiteMapDataSource control canbe configured to load from a database or other custom data store if you prefer.The SiteMapDataSource control works in conjunction with the navigationcontrols.

 

Navigation Controls

The new SiteMapPathcontrol displays the user?s current location in the navigational hierarchy ofthe 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 sitestructure (which doesn?t necessarily need to match the Web site?s physicalstructure) 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 control, and now we finally haveone! This high-quality hierarchical menu control acts just like you?d expect,and can appear vertically or horizontally. This exceptionally configurablecontrol can be bound to a SiteMapDataSource control or edited independently.

 

Treeviewcontrols are a staple of most modern languages, but mysteriously one was notincluded in ASP.NET 1.x. That oversight has been corrected in ASP.NET 2.0. TheImageSet property lets you automatically display a variety of useful andattractive images in front of each node. While the default behavior is to postback between each node click and raise a SelectedNodeChanged event, the Targetproperty lets you specify another frame in which to display the address in theNavigateURL property, so no postback is required. You can also use theSelectAction property to specify other behaviors, such as expanding the nodewithout a postback, doing nothing, or expanding the node and raising a postbackevent. Each node can optionally have a checkbox.

 

The TreeView nodes can be configured at design time, boundto a SiteMapDataSource control, or, for larger tree structures, sub nodes canbe loaded on demand to conserve server resources. This requires a postbackafter many of the user?s mouse clicks ? except for uplevel browsers, which cantake advantage of the automatically generated client-side code to call back tothe 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.NET1.x because it eliminated much of the boilerplate code needed for basicauthorization tasks. The new Login controls of ASP.NET 2.0 take it to the nextlevel. In many cases, not a single line of code will need to be written tofully secure a Web site. All the required user management, admin pages, anddatabase management is handled automatically by the new controls and theirunderlying infrastructure. Even a database is provided automatically, althoughyou can extend the authentication system in nearly any way imaginable for caseswhere you prefer to take control.

 

The new Logincontrol provides a user name and password screen that you may have createddozens 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 provideWeb sites with a fully functional user management system, with no code required.The Login and PasswordRecovery controls are pictured here.

 

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

 

The LoginViewcontrol is similar to the LoginStatus control, except instead of displaying asimple link, it will display whatever set of controls you?d like based on theuser?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 varyingsets of controls based on the current user?s role.

 

Any decent authentication system must provide users with away to change their password. The ChangePasswordcontrol provides such functionality by providing the standard three-textboxuser interface for old password, new password, and verify new password. Bydefault it accepts only strong passwords, and you can configure it to acceptwhatever combination of letters, numbers, and symbols your requirements demand.

 

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

 

The LoginNamecontrol displays the person?s user name when they?re logged in (or when it?savailable automatically through Windows Authentication); it?s invisible whenthe user is not logged in.

 

The CreateUserWizardcontrol is a functionally rich control that allows a user to enter all the standardinformation a site generally requires to create a new user, such as theirdesired username, password, e-mail address, etc. It?s fully extensible,allowing you to add whatever custom fields you require. Because it inheritsfrom the Wizard Web control, you can easily divide the user registrationprocess into as many steps as you desire.

 


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

 

WebPart Controls

WebParts are all about personalization. There is an entireinfrastructure underlying ASP.NET 2.0 that can handle personalization andauthentication. The WebPartManagercontrol?s purpose is to manage and orchestrate all the WebPart controls on thepage. It is not visible at run time.

 

Each instance of the WebPartZonecontrol 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 theuser. Each user can configure the page to their own liking and thecustomizations will be remembered and persisted between each of their visits.

 

The CatalogZonecontrol contains CatalogPart controls, such as DeclarativeCatalogPart, PageCatalogPart,and ImportCatalogPart. Thesecontrols are used to create a catalog of WebParts that users can choose to addto their page.

 

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

 

The ConnectionZonecontrol contains ConnectionPartcontrols. These are used to create connections between WebParts on a page. Theypermit one WebPart to send information to another WebPart.

 

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

 

New Report Controls

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

 

If you?re already using the CrystalReportViewer controlfrom ASP.NET 1.x, you?ll be pleased to know that it has been joined by two newcompanion controls: CrystalReportPartsViewerand CrystalReportSource. Both ofthese controls are invisible at run time. The CrystalReportSource controlmanages report data sources, parameters, and caching.

 

In the End

That covers all the new Web controls that appear bydefault in the Visual Studio 2005 (Beta 2) toolbox. There are, however, manyother new classes in the .NET Framework that inherit from System.Web.UI.Controland System.Web.UI.WebControl. The limited space here dictates that theirintroductions will have to wait for future articles.

 

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

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

 

Quite a few of the controls listed in this article deservefull articles of their own, so keep an eye out ? they?ll soon be appearing in thepages of asp.netPRO magazine.

 

Steve C. Orr is anMCSD and a Microsoft MVP in ASP.NET. He?s been developing software solutionsfor leading companies in the Seattlearea for more than a decade. When he?s not busy designing software systems orwriting about them, he can often be found loitering at local user groups andhabitually lurking in the ASP.NET newsgroup. Find out more about him at http://SteveOrr.netor e-mail him at mailto:Steve@Orr.net.

 

 

 

Add a Comment

There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement




Comments from the DevConnections Community

Join our community of development pros.

Windows problem

I all, I have a problem on my Windows Vista that began afetr the purchase of an external Hard Disk Freecom. A few days afetr the purchase I discon...

Most Recent Posts

 

 

GOOGLE LINKS
SPONSORED LINKS
FEATURED LINKS