Working with the ASP.NET Controls


Visual Studio 2005 and the new version of ASP.NET introduce a large number of control enhancements and a similar number of new controls. Wherever you turn in the product, there is something new, designed to make you more productive. We have already covered a few of these new items in this chapter. Our intent for the following sections is to call out some of the major enhancements as well as walk you through the new controls.

ASP.NET Control Enhancements

Most control enhancements apply to the entire group of ASP.NET controls. This is due to the fact that they are all built on the same control framework. Control enhancements include

  • XHTML Compliance The ASP.NET controls are sent back to the user's browser as standard HTML. In this release, all controls generate XHTML 1.1compliant markup. This ensures there are fewer surprises when viewing your pages in different browsers.

  • Data Binding There is a new data-binding model for ASP.NET 2.0. This model allows you to bind to many data sources such as a database, a business object, XML, and so on. In most scenarios, the binding requires no coding on your part. In addition, the controls still support the old binding model.

  • Adaptive Rendering The ASP.NET 2.0 controls can now adapt their markup output based on the requesting browser. Therefore, they are browser-compatible by default for most modern browsers. This can save a lot of time when you're testing your application.

  • Skins We have already described how themes and skins can be used to change the look of a site. Each ASP.NET 2.0 control has support for skins built in.

  • Data Entry ASP.NET controls now support, by default, some of the data entry features of Windows applications. You can now define a tab order, set the focus to a given control, and assign a hotkey (or access key) to a given control. All of this is done through properties on the control. The controls themselves generate script on the client to enable this feature.

  • Validation The validation controls are also improved in this release. For example, you can now group a set of validation controls and control validation for groups of controls (or sections of your page).

This list represents some of the big enhancements. However, each control has its own set of new features and enhancements. If you are familiar with prior versions of .NET, you should be sure to look for any feature you previously felt was missing.

The New Controls Inside ASP.NET

The Visual Studio Toolbox continues to grow. In fact, there are whole new sets of controls. For example, we have already looked at the new Web Part family of controls.

Another set of controls exist for managing user logins and security. In addition, Visual Studio now has controls that help you better define site navigation. There are also new data source controls and a new grid in which to display data. There is a new wizard control for defining multistep processes in the UI, a new control for uploading files, an image map control, and so on. The list of new (and improved) controls is a large one. The following sections examine a number of these new controls.

Note

If you still can't find the perfect control, Visual Studio provides you with the framework to create your own controls. You can take the simple approach and define a user control. This is a file that you can design like a page and then use across other pages. User controls are made up of one or more existing ASP.NET controls. They also have their own code-behind file for processing their own events. In this latest version of Visual Studio, user controls are also shown inside the designer (previously, they were just gray boxes).

If a user control is still not right and you want to provide design-time support for the Toolbox, configuration, and the Properties window, then you can create a custom control. Custom controls follow the same framework as the existing ASP.NET controls. You can even subclass and extend an existing control and turn it into your own custom version.


Login Controls

ASP.NET has a built-in set of login controls. These controls are meant to provide a complete low-code (sometimes no-code) solution for managing and authenticating users inside web applications.

By default, the login controls use what is called ASP.NET Membership. This feature allows these controls to work with an authentication database and related features without your writing code. Membership allows for the creation of users and groups and the management of user data (including passwords). The membership services inside ASP.NET can work with a SQL Express database or Active Directory. You can also write your own custom provider that can be plugged into the model.

We will look at configuring membership in a moment. First, let's examine the many login controls. Figure 13.41 shows a list of all these controls in the Toolbox. Each control has a purpose and is aptly named. Table 13.5 provides a brief overview of these many controls.

Figure 13.41. The login controls for ASP.NET.


Table 13.5. The Login Controls

Control

Description

Login

This control provides the primary interface for challenging users for their credentials (usernames and passwords). You can format the look of the control as well as display other links and messages such as authentication errors.

The control is set up to work with ASP.NET Membership by default. If you configure it, you do not need to write code. However, if you want to write your own code, you can use the Authenticate event to write your custom scheme.

LoginView

This control allows you to define two views: a view for users who are logged in and a view for anonymous users. You add controls to each view to define what users see based on their current status.

PasswordRecovery

This control is used for users to recover their passwords. Typically, you configure this control to email users their passwords. However, there are a number of other options.

LoginStatus

This control shows the authentication status of the current user. Users are either logged in or not. If they are, the control enables them to log out. If they are not logged in, the control gives them the opportunity to do so.

LoginName

This control displays the username of the currently logged-in user.

CreateUserWizard

This control allows users to create their own accounts or helps in password recovery. Users can request an account (and fill in their details) with this control.

ChangePassword

This control allows users to enter their current passwords and new passwords. The control can then validate the passwords and make the change if successful.


Configuring User Authentication

You can create a login page (or control) by dropping the Login control directly on a form. After you place it, you can begin to set the properties that define your application's security. Figure 13.42 shows an example. Notice that the login control provides access to the Administer Website link. This link takes you to the Web Site Administration Tool (WSAT) for your site where you can begin to define your authentication.

Figure 13.42. The Login control.


Note

By default, user data is sent to the server from the client as plain text. Therefore, you should enable SSL and HTTPS for securing your site.


The WSAT is a web-based tool that allows you to configure your site, including security. Figure 13.43 shows the home page of the tool. From here, you can access the Security tab, define application configuration (turn on tracing, for example), and select an administration provider. The default administration provider is configured for SQL Server or SQL Express. This is the place where the configuration data (such as users) for your site is stored.

Figure 13.43. The ASP.NET Web Site Administration Tool (WSAT).


You can use the WSAT to change from Windows security to Internet security. The former is best when working on a LAN environment. The latter is required for most public-facing, secure sites. After you configure this model, ASP switches you over to using membership. As a result, you get a membership database.

You can use the Security tab, shown in Figure 13.44, inside WSAT to configure the users, roles, and access for this database. Notice the three groups at the bottom of the screen: Users, Roles, and Access Rules. These groups provide links for managing the accounts in your system. Your login control will automatically respect the information configured here.

Figure 13.44. Managing users and roles for your site.


Site Navigation Controls

It can be easy to become lost on a lot of websites out there. If you don't provide good user navigation, then chances are users will complain (or stop visiting). ASP provides a few controls to help deal with defining and controlling navigation. The controls include the following:

  • MenuYou can now create menus for your web page out-of-the-box with Visual Studio. The menus support submenus and flyout menus. You can even bind your menus to an XML data source.

  • SiteMapPathThis control allows you to leave cookie crumbs as users navigate your site. That is, you can tell them where they came from and where they are. When you do so, users can use this list to jump backward to a place they just were.

  • treeViewThis control could always show hierarchical data. However, it can now be bound to an XML representation of your site called a site map. In this way, you can quickly define a navigation structure for your site that is updated in a single place.

Using the SiteMapPath Control

Recall that the SiteMapPath control is used to orient users in your site. You control this orientation definition through the use of a .sitemap file. You add this file to your site through the Add New Item dialog box. Inside it, you define the logical hierarchy of your site by nesting pages inside siteMapNode elements.

For example, if users start at a home page, this would be your outer node. As they navigate into your site, you create nested nodes. Listing 13.4 shows a simple example that includes a three-tier definition: Home, Find Customer, Edit Customer. This makes a logical progression through the sample site.

Listing 13.4. A .sitemap File

<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >   <siteMapNode url="Default.aspx" title="Home" description="">     <siteMapNode url="FindCustomer.aspx" title="Find Customer"  description="">       <siteMapNode url="EditCustomer.aspx" title="Edit Customer" description="" />     </siteMapNode>   </siteMapNode> </siteMap>

Figure 13.45 shows the results users see in their browser. In this case, the SiteMapPath control was added to the master page so that it appears throughout the site.

Figure 13.45. The SiteMapPath control in action.


Data Controls

ASP.NET has a full set of controls that you can use for working with, displaying, and binding to data. These controls are meant to work with little to no additional code. Instead of writing code, you should be able to configure the controls to behave as you want. Figure 13.46 shows a list of all the data controls in the Toolbox. Table 13.6 provides a brief overview of each of these controls.

Figure 13.46. The data controls in ASP.NET.


Table 13.6. The Data Controls

Control

Description

GridView

This is the new control in ASP.NET for binding to and working with tabular data. The control works with multiple data sources. It also allows sorting, paging, edit, add, and delete features.

DataList

You use this control when you want to control how your data is displayed and formatted. You can use this control with templates to gain control over when and how your data is displayed.

DetailsView

This is another new control in ASP.NET. It lets you display a single row of data (or row detail). You can display this row as an editable set of fields inside a table. The DetailsView control can be used in conjunction with the GridView control to obtain a master-detail editing structure.

FormView

This control is also new to ASP.NET. It offers the same features as the DetailsView control with the added benefit of being able to define the templates that make up the display of a given row.

Repeater

This control is a container for repeating data. You use the Repeater control with a template to show the contents.

Data Source Controls

Several data source controls are new to ASP.NET. These sources can be configured to work with the source data and execute select, update, new, and delete methods. You use a data source control to bind to other controls (such as a GridView). The ASP.NET data source controls allow access to SQL Server data, Microsoft Access data, data contained in an object, XML data, and data defined as a .sitemap file.


Note

We will cover ways to work with data and databases in Chapter 15, "Working with Databases."





Microsoft Visual Studio 2005 Unleashed
Microsoft Visual Studio 2005 Unleashed
ISBN: 0672328194
EAN: 2147483647
Year: 2006
Pages: 195

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net