Welcome Guest!
Create Account | Login
Locator+ Code:

Search:
FTPOnline Channels Conferences Resources Hot Topics Partner Sites Magazines About FTP RSS 2.0 Feed

Free Trial Issue of Visual Studio Magazine

email article
printer friendly
more resources

Negotiate Between Web Parts
Learn how to create a dynamic application where Web Parts negotiate what data they will share with each other.
by Peter Vogel

January 27, 2006

Technology Toolbox: Visual Basic; ASP.NET

Consider this (not uncommon) scenario under ASP.NET 2.0. Your user clicks on a button on your ASP.NET 2.0 Web Page, and the page transforms, revealing a list of Web Parts the user can add to the page. As the user adds Web Parts to the page, the page connects compatible Web Parts to each other. The connected Web Parts reach out to each other and start exchanging information. The user has created a personalized application from the components on the page.

ADVERTISEMENT

In an ideal world, a developer could create Web Parts for this page without having to consult with other developers creating Web Parts to be used on the same page. As the page developer, you add the resulting Web Parts to your page and know the Web Parts will interrogate each other or even negotiate with each other through a set of common interfaces to determine what information they will share. ASP.NET 2.0’s Web Parts let you do precisely this by taking advantage of .NET’s predefined communication interfaces. I’ll show you how to implement a pair of simplified Web Parts that enable you to take advantage of such predefined communication interfaces (see the Go Online box).

The Web Part framework includes four predefined interfaces: IWebPartField, which allows a Web Part to pass a single piece of information; IWebPartRow, which supports passing a set of data; IWebPartTable, for passing a collection; and IWebPartParameters, which allows one Web Part to signal what data it wants and the other Web Part to respond by listing what data can be provided. A Web Part that implements any of these interfaces can communicate with any other Web Part with the same interface.

Implementing these interfaces requires a handful of steps. You begin by using the IWebPartField interface (IWebPartTable and IWebPartRow are almost identical to IWebPartField) as a foundation for creating Web Parts that can interrogate each other, then move onto the more powerful—and slightly more complicated—IWebPartParameters interface which supports negotiation.

All of these interfaces allow the Web Part that requires information (the consumer) to tell the Web Part that has the data (the provider) what function to use to pass data. Implementing IWebPartField, IWebPartRow, or IWebPartTable interfaces in the provider requires adding two members: a method that accepts a single parameter of type delegate and a read-only property that returns a PropertyDescriptor object. These two members work together to provide to the consumer both data and a description of that data from the provider.

The process that the Web Parts follow is simple. In all the interfaces, the consumer calls a method on the provider, passing a reference to a method in the consumer that accepts data from the provider. The provider then calls that method in the consumer to pass data to the consumer. The key distinction between the IWebPartField and the IWebPartRow and IWebPartTable interfaces is the datatype of the parameter passed to the consumer’s routine.

Build a Provider
You begin the process of building these dynamic Web Parts by creating a provider that can be interrogated by another Web Part about the data it provides. Start by having the provider implement the IWebPartField interface and the GetFieldValue method required by the interface. The GetFieldValue method accepts a reference to the method in the consumer that will be used to receive data from the provider. This code implements GetFieldvalue to accept a delegate named fld from the consumer. The code also sets a class-level variable (called ConsumerDelegate) to the delegate passed from the consumer:

Public Class WebPartFieldProvider
   Inherits WebControls.WebParts.WebPart
   Implements WebControls.WebParts.IWebPartField
Dim fld As WebControls.WebParts.FieldCallback
Sub GetFieldValue(ByVal fld As _    WebControls.WebParts.FieldCallback) _    Implements WebControls.WebParts. _    IWebPartField.GetFieldValue
   ConsumerDelegate = fld
End Sub
End Class



Back to top














Java Pro | Visual Studio Magazine | Windows Server System Magazine
.NET Magazine | Enterprise Architect | XML & Web Services Magazine
VSLive! | Thunder Lizard Events | Discussions | Newsletters | FTP Home