0

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. enter image description here

  • Please remember to add the solution as an answer as soon as you are able to – Robert Lindgren Apr 3 '14 at 17:09
0

I might be totally off on this but I think you would have to make sure your object (class) supports serialization.

Basic serialization would require marking the class with the [Serializable] attribute but you might need something more advanced. See here for more details: http://msdn.microsoft.com/en-us/library/4abbf6k0%28v=vs.90%29.aspx

In any case using basic serialization would be quick enough to try.

  • Thanks for the suggestion, Jared. But it didn't solve the problem. – Stoic Developer Apr 3 '14 at 16:09
0

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. enter image description here

  • Good job. I love that feeling when making things simpler eliminates the problem. – Jared McGrath Apr 9 '14 at 2:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.