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

Does anyone know of an example that demonstrate how to use the SPWebPartConnection to connect two web parts on a standard page?

All I'm trying to do is have two different web parts exchange a single string value. I'm getting a bit confused between WebPartConnection and SPWebPartConnection. My code is below which I've been fiddling but so far haven't managed to get a breakpoint in the provider web part SetConnection method to fire. I'm assuming that it's the first thing that has to work.

On my aspx page I have (unrelated attributes removed for brevity):

<WebPartPages:spproxywebpartmanager runat="server" id="spproxywebpartmanager">
<SPWebPartConnections>
    <WebPartPages:SPWebPartConnection 
        id="connection1" 
        ProviderID="viewer" 
        ConsumerID="details" 
        ConsumerConnectionPointID="consumer" 
        ProviderConnectionPointID="provider"/>
     </SPWebPartConnections>
</WebPartPages:spproxywebpartmanager>

<WebPartPages:WebPartZone runat="server" ID="TopColumnZone">                       
    <ZoneTemplate>
   <WpNs0:MyViewer runat="server" ID="viewer" ...">
        </WpNs0:MyViewer>
    </ZoneTemplate>
</WebPartPages:WebPartZone>

<WebPartPages:WebPartZone id="g_0029D1EFB02B409DBDF4EFDB8E0560B8" runat="server">
    <ZoneTemplate>
    <WpNs0:MyDetails runat="server" ID="details" ...>
         </WpNs0:ClassifiedDetails>
    </ZoneTemplate>
</WebPartPages:WebPartZone>

Provider web part class:

public class ClassifiedsViewer : WebPart, System.Web.UI.WebControls.WebParts.IWebPartField
{
    ...

    public void GetFieldValue(System.Web.UI.WebControls.WebParts.FieldCallback callback)
    {
        callback("some string");
    }

    [System.Web.UI.WebControls.WebParts.ConnectionProvider("FieldProvider", "provider")]
    public IFilterValues SetConnection(System.Web.UI.WebControls.WebParts.IWebPartField provider)
    {
        return this;
    }
}

Consumer web part class:

public class ClassifiedDetails : WebPart
{
    protected strign myProperty;

    ...

    private void GetFieldValu(object fieldValue)
    {
        myProperty = fieldValue as string;
    }

    [System.Web.UI.WebControls.WebParts.ConnectionConsumer("FieldConsumer", "consumer", typeof(SPWebPartConnection), AllowsMultipleConnections = true)]
    public void SetConnectionInterface(System.Web.UI.WebControls.WebParts.IWebPartField provider)
    {
        m_provider = provider;
    }
}
share|improve this question

2 Answers 2

Finally found an exmaple that sorted me out Connect Web Parts in SharePoint 2010 . Now to sort out why the connection is happening after the consumer part has already loaded.

share|improve this answer

If this is like what I ran into it 'happens' because of the Page cycle - in other words the WebPart that is supplying the value has to exist before it can supply that value to the WebPart that is consuming it.

As a result you have to handle the data results in the PreRender override.

I may have misunderstood, however, the problem you were having....

share|improve this answer

Your Answer

 
discard

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