I am having some difficulty transmitting a class object between two web parts (Provider and Consumer). Basically whenever I try to build my project I get this error:
'ConnectableWebParts.LinkButtonProvider.LinkButtonProvider' does not implement interface member 'ConnectableWebParts.ISalObject.SalItem'
I have achieved this using primitive objects such as strings, but for some reason it has problems with transmitting a class that has 3+ string variables.
I have looked over some of my previous examples to see if there is something I am missing, but it all looks correct. So I assume there is some unique difference between transferring primitive objects and transferring class objects.
Could really use a second pair of eyes, or if someone has done this before that would be great. Thank you for your time.
Solution below.
Interface
namespace ConnectableWebParts
{
public interface ISalObject
{
SalObject SalItem { get; set; }
}
}
Class
namespace ConnectableWebParts
{
public class SalObject
{
private string name;
private string description;
private string code;
public string Name { get { return name; } set { name = value; } }
public string Description { get { return description; } set { description = value; } }
public string Code { get { return code; } set { code = value; } }
public SalObject() {
name = "";
description = "";
code = "000000";
}
public void storeDate()
{
Name = "John Doe";
Description = "Good Omen";
Code = "111111";
}
}
}
Provider
namespace ConnectableWebParts.LinkButtonProvider
{
[ToolboxItemAttribute(false)]
public partial class LinkButtonProvider : WebPart, ISalObject
{
private SalObject _salvage = new SalObject();
[Personalizable()]
public SalObject Salvage {
get { return _salvage; }
set { _salvage = value; }
}
[ConnectionProvider("Provider for SalObject From LinkButton", "SalObjectProvider")]
public ISalObject SalObjectProvider()
{
return this;
}
public LinkButtonProvider()
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitializeControl();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
Salvage = new SalObject();
}
}
}
Consumer
namespace ConnectableWebParts.LinkButtonConsumer
{
[ToolboxItemAttribute(false)]
public partial class LinkButtonConsumer : WebPart
{
private ISalObject _myProvider;
[ConnectionConsumer("SalObject Consumer", "SalObjectConsumer")]
public void SalvageConsumer(ISalObject Provider)
{
_myProvider = Provider;
}
public LinkButtonConsumer()
{
}
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
InitializeControl();
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected override void OnPreRender(EventArgs e)
{
EnsureChildControls();
if (_myProvider != null)
{
NameCell.Text = _myProvider.SalItem.Name;
DescCell.Text = _myProvider.SalItem.Description;
CodeCell.Text = _myProvider.SalItem.Code;
}
}
}
}
EDIT: Haha hah!
Turns out when dealing with class objects I don't even need to use an Interface.
It would seem an interface is required when dealing with primitive objects (string, int, long, etc.) or OOTB Web Parts.
So I just removed the interface from the project, replaced all my calls to the class name itself, and set my Provider to only inherit from WebPart.
Now it works exactly like I wanted it to. I hope other people can use this information.