Introduction
Connections Are A Very Powerful Feature Of WebParts That Allows Interchanging Data Between Two WebParts, Allowing Creating More Interesting Non-Monolitic WebParts.
Creating a connection is as easy as defining the interface that you want to connect through. Think of it as the contract you are saying your WebPart supports and that someone could at runtime request that interface and use it.
To Have A Connection One WebPart SHOULD ACT ASOSESS SOUGH A WELL Defined Interface, That The Consumer Knows How To Use.
Lets Start Creating The Simplest Connection Sample Possible, and as with all samples we will start with the "Hello World" Connection Sample.
Using WebPart ConnectionsThe Steps We Need to Follow To Establish A Connection Are:
Define The Interface That Will Connect Both WebParts Create a Provider WebPart
a. EXPOSE A Provider Connection Point To Return THIS Interface in A WebPart Create a Consumer WebPart
a. EXPOSE A Consumer Connection Point To Request The Interface Establish THE Connection Use The Interface in Prerender
Sample Scenario
The scenario that we will create is as simple as it gets. We will create a Provider WebPart that will expose a single string property (called Message) that will be consumed by another WebPart through a Connection. The consumer WebPart will only display the Message in a label.
To make it.......................
Create a New Project
Create a New Web Application In Visual Web Developer or Visual Studio .NET.
Define the Interface for the connectionThe first step is to define the interface through which the WebParts will communicate. The ASP.NET WebPart Framework ships several interfaces to try to standardize a little bit the creation of new webparts to try to enhance the intercommunication between vendors, for example IField, IParameter, IRow, ITable, etc. you can also define your own interface if you want to.In this sample since all the data we need to interchange is a string property we will define our own interface.
Create The Code Directory
SELECT The Project Node in The Solution Explorer. Right Click It and SELECT New Folder Type Code as The Name of The Folder
Note. The Code directory is new to ASP.NET 2.0 that allows developers to place code inside this directory with the .cs or .vb extension and the asp.net build infrastructure will compile the code on demand only when needed.
Add A New Interface in The Code Directory.
Sales code.........................
C # cote:
public
Interface iMessage {
String message {
get
}
}
VB.Net Code:
Public
Interface iMessage
Readonly
Property Message ()
AS
String
End
Interface
Create The Provider WebPartAfter Defining The Interface, We Need To Create The WebPart That Will Expose It as a provider.
To Simplify The Sample We Well Create a UserControl That We Well Use As a WebPart, this is a really easy way to understand the whole webpart Infrastructure.
Side Note: The WebPart framework only knows how to process WebParts, if you add a control that does not inherit from WebPart directly or indirectly (say UserControls, WebControls, etc) then the WebParts Framework will "wrap" the control in a especial type of WebPart called GenericWebPart, this GenericWebPart class will try to look for specific WebPart attributes (ie those defined in IWebPart interface like Title, SubTitle, Description, etc) and extract the values to make it behave just as any other WebPart.Create the User Control
Select the Project node Right click it and select the option Add New Item Select the option Web User Control Type ProviderWebPart.ascx as the name for the UserControl Replace the content of the UserControl so that it looks as the following code
C # code
<% @ Control Language = "C #" classname = "providerwebpart"%>
<% @ IMplements Interface = "iMessage"%>
<
Script
Runat
= "Server"
>
[ConnectionProvider ("Message")]]]]]
Public imssis getMessage () {
Return
THIS
;
}
public
String message {
Get {
Return_MessageTextBox.Text
;
}
}
<
/
Script
>
<
ASP: TEXTBOX
Runat
= "Server"
Id
= "_ MessageTextBox"
/>
<
ASP: Button
Runat
= "Server"
Id
= "_ postbackbutton"
TEXT
= "Change the text"
/>
VB.NET CODE
<% @ Control language = "vb" classname = "providerwebpart"%>
<% @ IMplements Interface = "iMessage"%>
<
Script
Runat
= 'Server'
>
Public
Readonly
Property Message ()
AS
String_
Implements IMESSAGE.MPLEments IMESSAGE.MPLEments
Get
Return_MessageTextBox.Text
End
Geted
Property
"")> _ _ _ Public Function getMessage () As iMessage Return ME End Function < / Script > < ASP: TEXTBOX Runat = "Server" Id = "_ MessageTextBox" /> < ASP: Button Runat = "Server" Id = "_ postbackbutton" TEXT = "Change the text" /> This Code Defines A Control That Implements The Interface iMessage That Was previously defined. IT Exposed A Method Called GetMessage () That Returns An Instance of Such Interface, Since In This Case The Control Itself Implements The Interface, IT IS SAFE TO RETURN This. You Could Potentially Create An Instance of Another Object And Returned It if Needed. This Method Has A Custom Attribute That Signals The ASP.NET WebPart Framework That This Method Is To Expose A Provider Connection Point So Other WebParts CAN Connect To IT. THEN ITUALLY IMPLEMENTS The Message Property of The IMESSAGE INTERFACE TO RETURN The value of the textbox. Finally I Just Added a Button So We can change The text and click the button to see the connection. Create The Consumer WebPartNow We will create the consumer of sowebpart. To Simplify The Sample We Will Create It Using Again A UserControl. Create The User Control Select the Project node Right click it and select the option Add New Item Select the option Web User Control Type ConsumerWebPart.ascx as the name for the UserControl Replace the content of the UserControl so that it looks as the following code C # code <% @ Control Language = "C #" classname = "consumerwebpart"%> < Script Runat = "Server" > Private iMessage_Message; [ConnectionConsumer " "")] Void setMessage (iMessage message) { THIS._MESSAGE = Message ; } protected Override Void onprender (Eventargs E) { IF (_MESSAGE! = NULL) _MessageLabel.Text = _MESSAGE.MESSAGE ; Base.onPrender (e) ; } < / Script > < ASP: Label Runat = "Server" Id = "_ messagelabel" /> VB.NET CODE <% @ Control language = "VB" classname = "consumerwebpart"%> < Script Runat = 'Server' > Private _Message As iMessage "")> _ _ _ Sub setmessage Byval Message Ask iMESSAGE Me._message = Message End Sub Protected OVERRIDES Sub onprender ByVal E As Eventargs) IF Not (_MESSAGE) IS Nothing) THEN _MessageLabel.Text = _MESSAGE.MESSAGE End IF Mybase.onprender (e) End Sub < / Script > < ASP: Label Runat = "Server" Id = "_ messagelabel" /> This Code Defines A WebPart That ACTS AS A Consumer of The Previous WebPart. The Way IT Works Is Marked with The Attribute for THE WIY ITHOD THAT ................................. At Runtime The WebPart Framework Will Request The interface from the provider and kilore.............. Note: One important thing to notice here is that you should not use the interface at this moment, it is only safe to use in the PreRender due to the fact that connections might depend on other connections and you can only be sure everything is set up . Creating The Pagenow We Well Create The page where we act loading. Create the page Select the Project node Right click it and select the option Add New Item Select the option Web Form Type Sample.aspx as the name for the Page Replace the content of the Page so that it looks as the following code C # code <% @ Page language = "c #"%> <% @ Register tagprefix = "uc1" tagname = "providerWebPart" src = "providerwebpart.ascx"%>%> <% @ Register tagprefix = "uc2" tagname = "consumerwebpart" src = "ConsumerWebPart.ascx"%> < HTML > < HEAD > < Title > Sample Page < / Title > < / HEAD > < Body > < FORM id = "Form1" Runat = "Server" > < ASP: WebPartManager Id = "WebPartManager1" Runat = "Server" > < StaticConnections > < ASP: Connection Id = "SampleConnection" Consumerid = "ConsumerWebPart1" ProviderID = "Providerwebpart1" /> < / StaticConnections > < / ASP: WebPartManager > < ASP: WebPartZone Id = "WebPartzone1" Runat = "Server" > < ZoneTemplate > < UC1: Provincewebpartrunat = "Server" Id = "Providerwebpart1" /> < UC2: ConsumerWebpart Runat = "Server" Id = "ConsumerWebPart1" /> < / ZoneTemplate > < / ASP: WebPartZone > < / FORM > < / Body > < / HTML > VB.NET CODE <% @ Page language = "vb"%> <% @ Register tagprefix = "uc1" tagname = "providerWebPart" src = "providerwebpart.ascx"%>%> <% @ Register tagprefix = "uc2" tagname = "consumerwebpart" src = "ConsumerWebPart.ascx"%> < HTML > < HEAD > < Title > Sample Page < / Title > < / HEAD > < Body > < FORM id = "Form1" Runat = "Server" > < ASP: WebPartManager Id = "WebPartManager1" Runat = "Server" > < StaticConnections > < ASP: Connection Id = "SampleConnection" Consumerid = "ConsumerWebPart1" ProviderID = "Providerwebpart1" /> < / StaticConnections > < / ASP: WebPartManager > < ASP: WebPartZone Id = "WebPartzone1" Runat = "Server" > < ZoneTemplate > < UC1: providerWebPart Runat = "Server" Id = "Providerwebpart1" /> < UC2: ConsumerWebpart Runat = "Server" Id = "ConsumerWebPart1" /> < / ZoneTemplate > < / ASP: WebPartZone > < / FORM > < / Body > < / HTML > Running the Sample To Run The Sample Just Browse to Sample.aspx. Type Some Text In The TextBox and the Click The Button.notice How The Conspeer WebPart Will Automatically Display The Data from The provider webpart. Summary Connections Are A Very PowerFull Feature In WebParts That Allow WebParts to Interact Data Between The Theware. In this tutorial we just scratched the surface of Connections, they are as complicated and rich as the whole WebPart Framework, they support several features, including multiple connections, Transformers (to adapt incompatible connections), one to many connections, secondary interfaces and many many More.