WebPartManager.DisconnectWebParts Method (WebPartConnection)


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

Carries out the process of disconnecting server controls that are connected on a Web page.

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

public virtual void DisconnectWebParts(
	WebPartConnection connection


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

A WebPartConnection that represents the connection between server controls.

Exception Condition

connection is null.


connection is not contained in either StaticConnections or DynamicConnections.


StaticConnections is read-only.

- or -

connection has already been disconnected from StaticConnections.

- or -

DynamicConnections is read-only.

- or -

connection has already been disconnected from DynamicConnections.

The DisconnectWebParts method carries out the complete process of ending a connection between WebPart or server controls, when you pass it the connection parameter.

This method is used to disconnect controls when you place an <asp:connectionszone> element in a Web page, to provide a user interface (UI) for managing connections. When a page is in connect display mode (ConnectDisplayMode), and a current connection exists, users can click a button that calls the DisconnectWebParts method to end the connection.

You can also call the DisconnectWebParts method directly from your code, if you want to disconnect controls programmatically and without having to add an <asp:connectionszone> element to a page.

Notes to Inheritors:

You can override the DisconnectWebParts method if you want to change the default implementation for disconnecting WebPart controls. If you do override the method, and you simply want to add some implementation to the existing method, you can call the base method prior to executing your own code.

The following code example demonstrates how to use the DisconnectWebParts method. Using two custom WebPart controls, the Web page enables you to create a connection between the controls by clicking a button, while another button enables you to disconnect the controls.

The code example has four parts:

  • A user control for changing display modes.

  • A source file containing custom WebPart controls.

  • A Web page to host the controls.

  • 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 second part is a file containing the source code for the two custom WebPart controls that will be connected. 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. This example uses dynamic compilation, so the Register directive that references these components in the Web page is declared accordingly at the top of the Web page. For a walkthrough that demonstrates compiling options, 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();


The third part of the code example is the Web page. Notice that near the top, it contains Register directives to register the user control and the dynamically compiled assembly with the WebPart controls. The page has two primary methods. The Button1_Click method creates a connection between the controls, and the Button2_Click method disconnects the controls.

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

<!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 Button2_Click(object sender, EventArgs e)
    if (mgr.Connections.Count >= 1 && mgr.Connections[0] != null)


<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" 
        OnClick="Button1_Click" />
      <asp:Button ID="Button2" runat="server" 
        Text="Disconnect WebPart Controls" 
        OnClick="Button2_Click" />

After you load the page, you can click the Connect button to connect the controls. If you enter some text in the text box control, and then click the Enter button, the text will be displayed in the connected control (it would not be displayed if the controls were disconnected). If you click the Disconnect button, the controls will be disconnected. You can verify the connection status of the controls by using the Display Mode drop-down list control to switch the page into Connect mode. After doing that, click the verbs menu (represented by an arrow) in the title bar of one of the controls, and select the Connect item. The connection UI is displayed; it is available because there is an <asp:connectionszone> element declared in the page. You can also connect and disconnect the controls from this UI.

.NET Framework
Available since 2.0
Return to top