0

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; } 

2 Answers 2

1

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.

4
  • I have a problem with this because when you will set bar in method CreateChildControls consumer cannot get it. Commented Dec 5, 2012 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. Commented Dec 5, 2012 at 17:52
  • after which event data is passing thru? Commented Dec 5, 2012 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. Commented Dec 5, 2012 at 20:32
1

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 :)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.