11.4. Communicating Between Web Parts

Problem

You have a web part in your application that needs to notify another web part when changes are made by the user. For example, suppose you have one web part that reads data from a database and acts a filter for a second web part displaying the filtered data. When the user changes the filter settings in the first web part, you want the second web part to be notified accordingly so it can display the new data.

Solution

Create a provider web part control, a consumer web part control, an interface to use as the communication contract between the web parts, and a page that uses both web part controls. In the provider web part control, add a method that returns the interface and is marked as the connection provider. In the consumer web part control, add a method that is passed the interface and is marked as the connection consumer. In the page that uses the web part controls, add a WebPartConnection control to the <StaticConnections> element of the WebPartManager defining the connection between the web parts.

The application we have implemented to demonstrate the solution is shown in Examples 11-17, 11-18, 11-19, 11-20, 11-21, 11-22 through 11-23. Examples 11-17 (VB) and 11-18 (C#) show the interface used for messages between the provider and consumer web parts. Examples 11-19 (VB) and 11-20 (C#) show the provider web part. Examples 11-21 (VB) and 11-22 (C#) show the consumer web part. Example 11-23 shows the .aspx for a page that demonstrates ...

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.