Here are simple provider and consumer Web Parts. The provider UI accepts a text field that it passes to the consumer Web Part which simply outputs it. The connection between the Web Parts is the following interface:
namespace ConnectedWebParts
{
public interface IParcel
{
string ID { get; }
}
}
The Provider Web Part implements this interface and must have a method with the attribute ConnectionProvider that returns itself (since it implements the interface):
namespace ConnectedWebParts
{
public class ProviderWebPart : WebPart, IParcel
{
protected TextBox txtParcelID;
protected Button btnSubmit;
private string _parcelID = "";
protected override void CreateChildControls()
{
txtParcelID = new TextBox() { ID = "txtParcelID" };
btnSubmit = new Button() { ID = "btnSubmit", Text="Submit"};
btnSubmit.Click += btnSubmit_Click;
this.Controls.Add(txtParcelID);
this.Controls.Add(btnSubmit);
}
void btnSubmit_Click(object sender, EventArgs e)
{
_parcelID = txtParcelID.Text;
}
[ConnectionProvider("Parcel ID")]
public IParcel GetParcelProvider()
{
return this;
}
string IParcel.ID
{
get { this.EnsureChildControls(); return _parcelID; }
}
}
}
The Consumer Web Part must define a method with a ConnectionConsumer attribute that accepts an object that implements the connection interface (the provider Web Part) as a parameter:
namespace ConnectedWebParts
{
public class ConsumerWebPart : WebPart
{
protected IParcel _provider;
protected Label lblParcelID;
protected override void CreateChildControls()
{
lblParcelID = new Label();
if (_provider != null && !String.IsNullOrEmpty(_provider.ID))
lblParcelID.Text = _provider.ID;
this.Controls.Add(lblParcelID);
}
[ConnectionConsumer("Parcel ID")]
public void RegisterParcelProvider(IParcel provider)
{
_provider = provider;
}
}
}