Introducing ASP.net 2.0 Web Part connections

Source: Internet
Author: User

When you start using the Microsoft. NET Framework 2.0 and ASP.net, you will find that the new WEB parts infrastructure adds some very powerful features to the underlying platform. For this article, I assume you have an understanding of the basics of Web parts, such as how to use WebPartManager controls, Web parts zones, editors, directories, and persistence properties.

To create a Web Part for ASP.net 2.0 applications

You can create WEB parts in two ways. The first approach involves creating a custom Web part class that inherits from the WebPart class defined in the System.Web.UI.WebControls.WebParts namespace. When you use this method, it is usually meaningful to package your custom WEB part classes into an assembly DLL, as this provides additional control over reuse, versioning, and Visual Studio 2005. If you are familiar with building custom controls with previous versions of ASP.net, many of the same techniques apply to building custom Web parts into DLL assemblies.

The second method used to create the ASP.net 2.0 Web part involves the use of user controls. Although this method does not produce the same level of reuse and versioning, it does allow you to use the Visual Studio Forms Designer to create a user interface section of a WEB part. This method is appropriate for you if you want to create an application by dragging and dropping controls for user input, validation, and data binding into the design interface. Of course, if you've taken the time to create a user control that you want to use as a Web part, it's also a good way to go.

When you create a user control that is specifically designed as a Web part, it is recommended that you implement the IWebPart interface. This way, the code in the background of the Web Part can programmatically assign several of its own internal WEB part properties, such as its Title and Titleiconurl.

The code example that is included with this article uses a custom base class named Webpartbase that inherits from UserControl and implements IWebPart. The definition of the base class is deployed in the source file named Webpartbase.vb in the App_Code directory. Whenever you create a new Web part by using a user control, you simply change the base class in the code-behind file to take advantage of the technology:

Partial Class WebParts_Customers
Inherits WebPartBase
  Sub New()
Title = "Northwind Customer List"
TitleIconImageUrl = "~\img\Customers.gif"
  End Sub
End Class

Designing a WEB part that you can connect to

With Web parts connections, you can make it easier for users to visualize the relationships that exist between data items. For example, a Web Parts connection can model a master-from scenario where a Web Part that displays a list of customers is connected to another Web Part that displays the details of the currently selected customer. The example in Figure 1 shows the user interface appearance that this design might generate.

Figure 1

Web part connections can also be used to model a one-to-many relationship. For example, a Web Part that displays a list of customers can connect to another Web Part that displays all orders for the currently selected customer.

Another scenario that is typically modeled using a WEB parts connection is a form query. In this scenario, a WEB part provides a user interface that allows the user to select the search or filter criteria to use when querying data, such as a database table. The Web Part then connects to another Web Part that displays the results of the query. A Web Parts connection is used to pass filter criteria from one Web Part to another before running the query.

Web parts connections are based on the concepts of providers and consumers. The provider Web Part provides information to one or more consumer Web Parts through a programming interface. The information exchanged between the provider and the consumer can be a simple data item (such as a number or string), or it can be a more specific content, such as a reference to a complex array or custom object collection.

If you have written a Web Part for Windows SharePoint Services 2.0 (WSS), you may already be familiar with the model it uses to connect Web Parts. In WSS, a WEB part can only use a set of predefined interfaces for a connection. Examples of these interface pairs include ICellProvider and ICellConsumer, as well as IRowProvider and IRowConsumer.

The WEB parts connectivity model in ASP.net 2.0 is easier and more flexible than the legacy model in WSS because you can use your own custom interfaces. This means that you do not need to use interface definitions created by Microsoft personnel. Furthermore, you do not need to do anything with the interface pairs, they must be implemented by both the provider and the consumer. With ASP.net 2.0, only the provider needs to implement an interface.

To understand how it works, we first create a connection between the two Web parts. For the example I'm going to provide in this month's column, I decided to use the Northwind database because it has a Customers table and an Orders table. This allows me to show you how to design a WEB part for a master-and one-to-many relationship. It is important to note that if you use SQL Server 2005, the sample Northwind database is not installed during product installation. To install it, you must download and run the scripts available on the Microsoft Web site.

Now, suppose you want to establish a Web Part connection between a Web Part that displays a list of customers and a customer Web Part that displays the details of the currently selected customer, as shown in Figure 1. A Web Part that displays a list of customers will play the role of a provider, while a Web Part that displays the details of the currently selected customer is used as the consumer. In this case, you want the provider to provide the consumer with the CustomerID field for the currently selected customer.

First, create a simple interface named Icustomeridprovider:

Public Interface ICustomerIDProvider
ReadOnly Property CustomerID() As String
End Interface

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.