Export (0) Print
Expand All

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

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
)

Parameters

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

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

providerConnectionPoint
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.

consumer
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.

consumerConnectionPoint
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.

ExceptionCondition
InvalidOperationException

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" 
    TagName="DisplayModeMenuCS"
    Src="~/displaymodemenucs.ascx" %>
<%@ Register TagPrefix="aspSample" 
    Namespace="Samples.AspNet.CS.Controls" 
    Assembly="ConnectionSampleCS"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  protected void Button1_Click(object sender, EventArgs e)
  {
    ProviderConnectionPoint provPoint = 
      mgr.GetProviderConnectionPoints(zip1)["ZipCodeProvider"];
    ConsumerConnectionPoint connPoint = 
      mgr.GetConsumerConnectionPoints(weather1)["ZipCodeConsumer"];
    WebPartConnection conn1 = mgr.ConnectWebParts(zip1, provPoint,
      weather1, connPoint);
  }

  protected void mgr_DisplayModeChanged(object sender, 
    WebPartDisplayModeEventArgs e)
  {
    if (mgr.DisplayMode == WebPartManager.ConnectDisplayMode)
      Button1.Visible = true;
    else
      Button1.Visible = false;
  }
</script>

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

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

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft