Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I got two webpart instances on page. I need to pass data from one webpart to other. Data that i need to pass will be loaded in CreateChildControls.

When i am debugging its looks like nothing is passed! For example i set Value = 10; And in other webpart i got aintValue = 0;

How to pass data properly?

protected override void CreateChildControls()
        {
            var aintValue = provider == null ? -1 : provider.Value;

            if (Mode == DepartmentAndLocationWebpartMode.Departments)
            {
                this.Value = 10;
            }
            else
            {

            }
        }

        [ConnectionProvider("Provider of data to skip news.", "SkipNewsProvider")]
        public ISkipNewsProvider SkipNewsProvider()
        {
            return this;
        }

        protected ISkipNewsProvider provider;

        [ConnectionConsumer("Consumer of news to skip.", "SkipNewsConsumer")]
        public void SkipNewsConsumer(ISkipNewsProvider Provider)
        {
            provider = Provider;
        }
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Do you have an interface, a provider and a receiver?

If so you then you connect the receiver and provider through the web part menu of the provider web part.

public interface example
{
    string foo { get; set; }
}

public class Provider : WebPart, example
{
    //webpart components

    public string bar
    {
        get{return _bar;}
        set{_bar = value;}
    }      

    protected override void CreateChildControls()
    {
        // Some control creation stuff
    }
    protected override void Render(HtmlTextWriter writer)
    {
        // Might want to override the renderer 
    }
    // Button events etc ie to set bar to the input text

    [ConnectionProvider("example provider")]
    public example ProvideCommunication()
    {
        return this as example;
    }  
  }

public class Consumer : WebPart
{
    private example providerWebPart;

    [ConnectionConsumer("example consumer")]
    public void ReceiveName(example provider)
    {
        providerWebPart = provider;
    }
    protected override void Render(HtmlTextWriter writer)
    {
        // Write out the value
        if (providerWebPart != null)
            writer.Write(providerWebPart.bar);
        base.Render(writer);
    }
}

I had to rip this out of one I had made and cut out all the gubbins, but I left the core parts there for you.

share|improve this answer
    
I have a problem with this because when you will set bar in method CreateChildControls consumer cannot get it. –  Evgeny Dec 5 '12 at 17:50
1  
It would be set on a click event, or another event, not there. It wouldn't fire off anything to pass the information. –  Hugh Wood Dec 5 '12 at 17:52
    
after which event data is passing thru? –  Evgeny Dec 5 '12 at 20:13
1  
It will work when the webpart is on the page, because the javascript is loaded. So things like button click events will work. But if you set things before they won't update. That's been my obversation, there ways I have seen but never tried one so I can't comment. –  Hugh Wood Dec 5 '12 at 20:32
add comment

please refer to my answer in this post as its got all the info you need to make a connectable webpart in detail!

Connectable WebPart Problems......................?

and

Connection between 2 web parts

and

how to have connectable web part on two pages?

hope it helps :)

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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