Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Connections Property

WebPartManager.Connections Property

Gets a reference to the collection of all current connections on a Web page.

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

[BrowsableAttribute(false)]
public WebPartConnectionCollection Connections { get; }

The Connections property provides a way to access the current set of connections on a page. The collection itself is read-only, and developers who want to manipulate a specific connection from the collection should use WebPartManager methods such as ConnectWebParts and DisconnectWebParts.

The following code example demonstrates declarative and programmatic use of the WebPartManager control.

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, and an <asp:webpartmanager> element.

  • 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 following code contains only the Web page portion of the example. You will also need the custom user control and the source code for the custom controls mentioned above. Obtain these two items from the Example section of the WebPartManager class overview.

The following Web page code demonstrates how to use the Connections property programmatically to get the count of current connections on a page. Note that in the <script> tag section, the code to handle two events for the WebPartManager control accesses the Connections property to obtain the count.

<%@ 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">

  private void UpdateLabelData(int wpCount, int connCount)
  {
    Label1.Text = "WebPart Control Count:  " + wpCount.ToString();
    Label2.Text = "Connections Count: " + connCount.ToString();
  }

  protected void WebPartManager1_WebPartsConnected(object sender, WebPartConnectionsEventArgs e)
  {
    UpdateLabelData(WebPartManager1.WebParts.Count,
      WebPartManager1.Connections.Count);
  }

  protected void WebPartManager1_WebPartsDisconnected(object sender, WebPartConnectionsEventArgs e)
  {
    UpdateLabelData(WebPartManager1.WebParts.Count,
      WebPartManager1.Connections.Count);
  }

</script>

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

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) displays, click the Create a connection to a Consumer link. A cell appears that has a drop-down list control. Select Weather Control in the drop-down list, and click Connect to complete the connection of the two controls. Click Close, and then use the Display Mode drop-down list to return the page to normal browse mode. Note that a label now displays the number of connections and the number of WebPart controls. If you now return to connect mode and disconnect the two controls, when you return to browse mode, the label's content should be updated and there should be no connections.

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0
Show:
© 2015 Microsoft