.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Communication between Web Parts

Posted By:nobodysomebody3       Posted Date: October 01, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  
 

Communication between Web Parts. Creating a Provider Web Part

There are three steps to creating a Web Part that can provide data to another Web Part:

1.     Define an interface that specifies what data is to be passed between the Web Parts.

2.     Have the Web Part that will be providing the data implement the interface, including writing the code for the interface's methods and properties.

3.     Write the routine/method that handles the connection in the Provider.

 

Define an Interface

For a Web Part to communicate with other Web Parts, the Web Parts must implement an interface that data can be passed through. In the interface, you specify the methods and properties that must be supported by any object that implements the interface.

In C#:

public interface IBookInfo:

{

string ISBN

{

get;

set;

}

}

 

 

Tip 

The convention is to give interfaces a name that begins with the letter I (for example, IBookInfo).

 

Implement the Interface

The next step is to modify a Web Part to provide data through the interface. Begin by having the Web Part implement the interface that you just defined.

In C#, you add the interface name to the end of the class definition preceded by a comma, as this code shows:

   public class ProviderPart : System.Web.UI.WebControls.WebControl, IBookInfo

§  Now that you've specified what interfaces you're implementing in your Web Part, you have to create all the properties or methods specified in the interface (for the IBookInfo interface, that's only a single property).

 

 

 

In C# you don't assign an alias to an interface routine, but the name of the routine must consist of the interface name and the routine name, separated by a period:

public class ProviderPart : WebControl, IBookInfo
{

string IBookInfo.ISBN

{

get

{

return MyData;

}

set

{

MyData = value;

}

}

}

Create a Connection Point

Finally, you need to provide a connection point method for the interface. The connection point method is a function that returns a reference to the provider Web Part. The function's return value is the provider's implementation of the interface.

You can give the method any name that you want but, to indicate that the method is a connection point, you need to add the ConnectionProvider attribute to the routine. The ConnectionProvider must be passed a display name (this is the name that is used in the ConnectionsZone user interface).

In C#, the code looks like this:

[WebControls.WebParts.WebParts.ConnectionProvider("Provides ISBN", "ISBNProvider")]
{
public IBookInfo IBookInfoProvider()
return this;
}

. You can see that this gives the caller access to the interface methods on the object, without giving access to any other functionality

 

Tip 

One convention for naming the connection routine is to use the name of the interface with the word "Provider" appended to it.

Once you've filled in the routine tied to the ISBN property with some real business logic you have a Web Part that can provide data to another Web Part. Now you need to create a consumer Web Part that is able to pull data from your Web Part provider through the interface.

The ConnectionProvider attribute takes two parameters, which are often identical. The first is a display name to label the connection, and the second is a unique ID to identify this connection.

 

Note 

If a class exposes more than one interface via connection points, you have to specify a third parameter that gives the type of the interface, for example typeof(ITempUnits). This is not necessary when you expose only one interface.

 

Creating a Consumer Part

Creating a consumer Web Part is considerably easier than creating a provider: All that you have to do is create a connection point method. The connection point method is a subroutine that accepts a single parameter declared as the interface type (such as IBookInfo). The method must be given the ConnectionConsumer attribute (again, this attribute can be passed the display name to be used in the ConnectionsZone user interface). Using the parameter passed to the method, code in the routine can access a provider through the methods and properties in the interface. This below code accesses the provider through the IBookInfo interface to retrieve the value of the ISBN property:

In C#:

[WebControls.WebParts.WebParts.ConnectionConsumer("IBookInfo Consumer")]

public void IBookInfoConsumer(IBookInfo bk)

{

string strISBN;

strISBN = bk.ISBN;

}

To access the provider from routines outside the connection routine, just declare a variable at the class level as the interface type. You can set this variable in the connection routine and use that variable outside the connection routine. However, if the consumer part has not been connected to the provider, then the class variable is set to null in C#. To handle this, you should check that the class variable has been set before using it.

The below code declares a variable called ibk as an IBookInfo, sets ibk in the connection routine, and (in the Render routine) accesses the provider using the ibk variable:

private IBookInfo ibk;

[WebControls.WebParts.WebParts.ConnectionConsumer("IBookInfo Consumer")]

public void IBookInfoConsumer(IBookInfo bk)

{

ibk = bk;

}

protected override void Render(System.Web.UI.HtmlTextWriter writer)

{

if (ibk == null)

{

writer.Write("No book information available.");

}

else

{

writer.Write("ISBN: " + ibk.ISBN + "");

}

}

The runtime creates a consumer connection point as a callback. If the property on the IBookInfo interface changes, the runtime will invoke this method, so the consumer will instantly see-and respond to-the change in the provider.

You can't take advantage of the interface to the provider until after the connection point routine has run in the consumer.

Note :In the consumer Web Part, both when the connection is first made and on subsequent page requests, the connection point routine runs after the consumer's Load event (and the host page's LoadComplete event), and before the page's PreRender event.

 

Tip 

The provider's connection routine runs just before the consumer's connection routine rather than at a specific point in the provider's life cycle.

 

 

 

Note :

 

A Web Part that is going to act as a consumer has to implement a consumer connection point for the interface it wants to use. It goes without saying that a consumer can connect to a number of interfaces if necessary, defining a connection point for each

 

Anyways, the above bits should get you started with inter webpart communication. Once you have deployed the above two webparts, use the design mode of the zone to setup the source and destination connections.

 

How Connection works

Web Parts connections are based on a "pull" model of connectivity, where the consumer gets data from the provider

1. Web Part Manager request interface to the provider by calling [ConnectionProvider] method.
2. Web Part Manager get an interface from a provider
3. Web Part manager give the interface to the consumer  by calling [
ConnectionConsumer
] method
4. Consumer call provider via interface

In each page request, the WebPartManager enumerates the connections that are defined. Then, for each connection, it calls the connection provider's [ConnectionProvider] method to retrieve an interface and passes the interface to the corresponding connection consumer by calling the consumer's [ConnectionConsumer] method. The connection consumer then makes calls through the supplied interface to fetch data from the connection provider, as shown in figure above.


 Subscribe to Articles

     

Further Readings:

Responses
Author: Venkat         Company URL: http://www.dotnetspark.com
Posted Date: October 01, 2009

Nice article with excellent explanation for the web parts...

Regards
Venkat

Post Comment

You must Sign In To post reply

    Read also another Resources from the same Author

Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend