Web Parts Static Connection
On this page we'll look at an ASP.NET Web Parts Static Connection.
Provider and Consumer
In ASP.NET Web Parts it is possible to allow separate web parts controls to communicate with each other, such that we can get one web part control to act as a 'Provider' and another web parts control to act as a 'Consumer'. The provider control can send messages of data to the consumer, so it's a one way conversation.
The [ConnectionProvider] attribute is used to mark a method inside your 'Provider' web parts control which is responsible for sending your message of data. We could make a custom control or user control which will act as our Provider web part, we'll need to put our control into a WebPartZone. Here is a very simple method with the [ConnectionProvider] attribute applied which simply returns a String value to be sent as the message data:
[ConnectionProvider("Message provider", "GetMessage")]
public String GetMessage()
The [ConnectionConsumer] attribute is used to mark a method inside our 'Consumer' web parts control which will be responsible for receiving our message of data sent from our 'Provider' web parts control. Again, the we could make a custom control or user control to act as the Consumer control, we'll need to make sure it's in a WebPartZone on the same page as the Provider. Here is a method with the [ConnectionConsumer] applied so that is acts to work as a 'Consumer' and is expecting to receive a String parameter which will contain the message in the communication from the 'Provider'
[ConnectionProvider("Message consumer", "DisplayMessage")]
public void DisplayMessage(String message)
Literal_Message.Text = message;
A StaticConnection is a type of connection which we create declaratively using a WebPartManager, such that we set this up before the website runs and will be available to the user without them having to configure any connections. We need to add a StaticConnections element to our WebPartManager, and then inside of our StaticConnections element we need to add a WebPartConnection which specifies our static connection.
The WebPartConnection needs to have properties set which point to the Provider and Consumer controls and their methods which act as the 'Provider' and 'Consumer' methods. So ProviderID points to out web parts control acting as the Provider, and ProviderConnectionPointID points to it's 'Provider' method. The ConsumerID property points to the web parts control acting as the Consumer and the ConsumerConnectionPointID property points to the consumer's method which is the 'Consumer' method.
Here is the markup our static connection:
<asp:WebPartManager ID="WebPartManager1" runat="server">
With our Provider and Consumer controls on the page inside of WebPartZones and our WebPartManager with the StaticConnection set, we use the Provider to send messages to our Consumer using some event handler in the Provider to initiate the message sending and the consumer will be wired up behind the scenes by ASP.NET to receive the message and process it such as displaying it on the control.