WebPartManager.CanConnectWebParts 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.

Checks the WebPart controls that will be participating in a connection to determine whether they are capable of being connected, when the consumer and provider controls have compatible interfaces and a WebPartTransformer object is not needed.

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

public bool CanConnectWebParts(
	WebPart provider,
	ProviderConnectionPoint providerConnectionPoint,
	WebPart consumer,
	ConsumerConnectionPoint consumerConnectionPoint


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

The control that provides data to consumer when the controls are connected.

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

A ConnectionPoint that enables provider to participate in a connection.

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

The control that receives data from provider when the controls are connected.

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

A ConnectionPoint that acts as a callback method so that consumer can participate in a connection.

Return Value

Type: System.Boolean

A Boolean value that indicates whether provider and consumer can be connected.

This method is used to connect provider and consumer when both controls have compatible connection point types, so that a WebPartTransformer object is not needed. You might want to use this method to verify that two controls can be connected before calling ConnectWebParts to create a programmatic connection.

This overload uses the same implementation as the CanConnectWebParts(WebPart, ProviderConnectionPoint, WebPart, ConsumerConnectionPoint, WebPartTransformer) method overload, with the only exception being that this overload does not require a transformer.

The following code example demonstrates how to use this method.

The code example has four parts:

  • A user control that enables you to change display modes on a Web Parts page.

  • A Web page that contains two custom WebPart controls that can be connected, an <asp:webpartmanager> element, and some event-handling code that creates a connection using the CanConnectWebParts method.

  • A source code file that contains two custom WebPart controls, and a custom interface.

  • An explanation of how the example works in a browser.

The first part of the code example is the user control for changing display modes. You can obtain the source code for the user control from the Example section of the WebPartManager class overview. For more information about display modes and how the user control works, see Walkthrough: Changing Display Modes on a Web Parts Page.

The declarative markup for the Web page contains Register directives for both the user control and the custom controls. There is an <asp:webpartmanager> element, an <asp:webpartzone> element to contain the custom controls, and an <asp:connectionszone> element. Notice that in the Page_Load method, the code checks whether the connection can be made and, if so, defines a provider, a consumer, and their respective connection points, and then adds a new connection to the set of static connections referenced by the StaticConnections property.

<%@ 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 Page_Load(object sender, EventArgs e)

    // Define provider, consumer, and connection points.
    WebPart provider = mgr.WebParts["zip1"];
    ProviderConnectionPoint provConnPoint =
    WebPart consumer = mgr.WebParts["weather1"];
    ConsumerConnectionPoint consConnPoint =

    // Check whether the connection already exists.
    if (mgr.CanConnectWebParts(provider, provConnPoint,
      consumer, consConnPoint))
      // Create a new static connection.
      WebPartConnection conn = new WebPartConnection();
      conn.ID = "staticConn1";
      conn.ConsumerID = "weather1";
      conn.ConsumerConnectionPointID = "ZipCodeConsumer";
      conn.ProviderID = "zip1";
      conn.ProviderConnectionPointID = "ZipCodeProvider";


<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
    <title>ASP.NET Example</title>
    <form id="form1" runat="server">
      <!-- Reference the WebPartManager control. -->
      <asp:WebPartManager ID="mgr" runat="server" />   
      <uc1:DisplayModeMenuCS ID="displaymode1" 
        runat="server" />
      <!-- Reference consumer and provider controls 
           in a zone. -->
      <asp:WebPartZone ID="WebPartZone1" runat="server">
          <aspSample:ZipCodeWebPart ID="zip1" 
            Title="Zip Code Control"/>
          <aspSample:WeatherWebPart ID="weather1" 
            Title="Weather Control" />
      <hr />
      <!-- Add a ConnectionsZone so users can connect 
           controls. -->
      <asp:ConnectionsZone ID="ConnectionsZone1" 
        runat="server" />

The third part of the example is the source code for the controls. It contains an interface and two custom WebPart controls, one acting as a provider, and the other as a consumer. Because they have compatible connection points (both of them recognize the IZipCode interface), a transformer is not needed to make the connection. For the code example to run, you must compile this source code. You can compile it explicitly and put the resulting assembly in your Web site's Bin folder or the global assembly cache. Alternatively, you can put the source code in your site's App_Code folder, where it will be dynamically compiled at run time. For a walkthrough that demonstrates how to compile, see Walkthrough: Developing and Using a Custom Web Server Control.

namespace Samples.AspNet.CS.Controls
  using System;
  using System.Web;
  using System.Web.Security;
  using System.Security.Permissions;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Web.UI.WebControls.WebParts;

    Level = AspNetHostingPermissionLevel.Minimal)]
    Level = AspNetHostingPermissionLevel.Minimal)]
  public interface IZipCode
    string ZipCode { get; set;}

    Level = AspNetHostingPermissionLevel.Minimal)]
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class ZipCodeWebPart : WebPart, IZipCode
    string zipCodeText = String.Empty;
    TextBox input;
    Button send;

    public ZipCodeWebPart()

    // Make the implemented property personalizable to save 
    // the Zip Code between browser sessions.
    public virtual string ZipCode
      get { return zipCodeText; }
      set { zipCodeText = value; }

    // This is the callback method that returns the provider.
    [ConnectionProvider("Zip Code", "ZipCodeProvider")]
    public IZipCode ProvideIZipCode()
      return this;

    protected override void CreateChildControls()
      input = new TextBox();
      send = new Button();
      send.Text = "Enter 5-digit Zip Code";
      send.Click += new EventHandler(this.submit_Click);

    private void submit_Click(object sender, EventArgs e)
      if (input.Text != String.Empty)
        zipCodeText = Page.Server.HtmlEncode(input.Text);
        input.Text = String.Empty;


    Level = AspNetHostingPermissionLevel.Minimal)]
    Level = AspNetHostingPermissionLevel.Minimal)]
  public class WeatherWebPart : WebPart
    private IZipCode _provider;
    string _zipSearch;
    Label DisplayContent;

    // This method is identified by the ConnectionConsumer 
    // attribute, and is the mechanism for connecting with 
    // the provider. 
    [ConnectionConsumer("Zip Code", "ZipCodeConsumer")]
    public void GetIZipCode(IZipCode Provider)
      _provider = Provider;

    protected override void OnPreRender(EventArgs e)

      if (this._provider != null)
        _zipSearch = _provider.ZipCode.Trim();
        DisplayContent.Text = "My Zip Code is:  " + _zipSearch;

    protected override void CreateChildControls()
      DisplayContent = new Label();


After you have loaded the Web page in a browser, click the Display Mode drop-down list control and select Connect to switch the page to connect mode. Connect mode uses the <asp:connectionszone> element to enable you to create connections between controls. In connect mode, click the downward arrow in the title bar of the ZIP Code control to activate its verbs menu, and then click Connect. After the connection user interface (UI) appears, notice that a connection has already been created by the code contained in the Page_Load method.

.NET Framework
Available since 2.0
Return to top