WebPartManager.ConnectWebParts Method (WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on docs.microsoft.com to see the new experience.

Creates a connection between two WebPart or GenericWebPart controls using only the references to the controls and their specified ConnectionPoint objects.

Namespace:   System.Web.UI.WebControls.WebParts
Assembly:  System.Web (in System.Web.dll)

public WebPartConnection ConnectWebParts(
	WebPart provider,
	ProviderConnectionPoint providerConnectionPoint,
	WebPart consumer,
	ConsumerConnectionPoint consumerConnectionPoint


Type: System.Web.UI.WebControls.WebParts.WebPart

A WebPart control that has the role of furnishing data to another connected control.

Type: System.Web.UI.WebControls.WebParts.ProviderConnectionPoint

A method that serves as a callback method for the connection. As implemented in the Web Parts control set, this is a public method in provider that is marked with a ConnectionProvider metadata attribute.

Type: System.Web.UI.WebControls.WebParts.WebPart

A WebPart control that has the role of receiving data from provider, and then processing or displaying it.

Type: System.Web.UI.WebControls.WebParts.ConsumerConnectionPoint

A method that connects with providerConnectionPoint to receive the data for the connection. As implemented in the Web Parts control set, this is a public method in consumer that is marked with a ConnectionConsumer metadata attribute.

Return Value

Type: System.Web.UI.WebControls.WebParts.WebPartConnection

A WebPartConnection that contains the various information about the provider and the consumer needed for a connection.

Exception Condition

The WebPartManager control's collection of dynamic collections is read-only.

This overload is used to connect controls when their connection points are sufficiently compatible that they can connect without using a WebPartTransformer object. When this overload of the method is called, it simply passes the call to the other overloaded version of the method, and passes null for the parameter that requires a WebPartTransformer object.

When you attempt to connect two controls programmatically, you can use the CanConnectWebParts method in a conditional check to determine whether the controls can be directly connected.

The following code example demonstrates how to use this method to create a connection programmatically. For the full code required to run the example, see the Example section of the WebPartManager class overview. From that example, you will need the source code for the user control that allows you to change display modes on the page, and the source code for the two custom WebPart controls.

The code for the Web page that hosts the two controls follows. The page uses Register directives at the top to declare the user control and the custom controls. The custom controls are then referenced declaratively within an <asp:webpartzone> element. The code that handles the Button1_Click method creates a connection between the controls by using the ConnectWebParts method.

<%@ Page Language="C#" %>
<%@ Register TagPrefix="uc1" 
    Src="~/displaymodemenucs.ascx" %>
<%@ Register TagPrefix="aspSample" 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<script runat="server">

  protected void Button1_Click(object sender, EventArgs e)
    ProviderConnectionPoint provPoint = 
    ConsumerConnectionPoint connPoint = 
    WebPartConnection conn1 = mgr.ConnectWebParts(zip1, provPoint,
      weather1, connPoint);

  protected void mgr_DisplayModeChanged(object sender, 
    WebPartDisplayModeEventArgs e)
    if (mgr.DisplayMode == WebPartManager.ConnectDisplayMode)
      Button1.Visible = true;
      Button1.Visible = false;

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <form id="form1" runat="server">
      <asp:WebPartManager ID="mgr" runat="server" 
      <uc1:DisplayModeMenuCS ID="menu1" runat="server" />
      <asp:WebPartZone ID="WebPartZone1" runat="server">
          <aspSample:ZipCodeWebPart ID="zip1" runat="server"
            Title="Zip Code Provider" />
          <aspSample:WeatherWebPart ID="weather1" runat="server" 
            Title="Zip Code Consumer" />
      <asp:ConnectionsZone ID="ConnectionsZone1" runat="server">
      <asp:Button ID="Button1" runat="server" 
        Text="Connect WebPart Controls" 
    Visible="false" />

After you load the page in a browser, click the Connect WebPart Controls button to form the connection. You can then enter some data in the text box, and click the Enter 5-digit ZIP Code button to demonstrate that the controls are connected, and that data entered in the first control is updated in the second.

.NET Framework
Available since 2.0
Return to top