Export (0) Print
Expand All
Expand Minimize

WebPartManager.DisconnectWebPart Method

Removes a WebPart or server control that is being closed or deleted from any connections it is participating in.

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

protected virtual void DisconnectWebPart(
	WebPart webPart
)

Parameters

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

A WebPart control that is to be disconnected.

The DisconnectWebPart method is called internally by the Web Parts control set when a control is either closed on a page or deleted from a page. In such a scenario, the method is called to remove the control from any connections where it is involved as a consumer or provider. If the control is removed from any connection, this method also calls the DisconnectWebParts method to end any connections in which webPart was involved.

When the DisconnectWebPart method is called, it raises the WebPartsDisconnecting event. Normally this event can be cancelled, but in two cases it cannot be cancelled. One case occurs during requests to the page, when the ActivateConnections method is called. If there is a conflict among existing connections, the DisconnectWebPart method will be invoked to close one of the conflicting connections, and in this instance the WebPartsDisconnecting event cannot be cancelled, because the conflict must be resolved.

The other case occurs when a WebPart or server control that is currently connected is either closed or deleted. In this case, because of the control is being removed from the page, its connection needs to be terminated as well, therefore by design it is not possible to cancel the WebPartsDisconnecting event to interrupt the process of ending a connection. For more information, see the WebPartsDisconnecting event.

The following code example demonstrates how to use the DisconnectWebPart 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. If you close one of the controls while the page is in browse mode and the controls are connected, an override of the DisconnectWebPart method disconnects the closed control, ends the connection, and displays a message.

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 the file containing the source code for the two custom WebPart controls that will be connected, and a custom WebPartManager control. 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.

In the source code, notice the inherited control MyWebPartManager that overrides the DisconnectWebPart method. This method checks each connection in a page to see whether the control being closed participates in the connection and, if so, calls the DisconnectWebParts method to end the connection. This is identical to the base implementation of the method in the WebPartManager control. The overridden method then customizes the base implementation by writing a message to the page.

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;

  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  public interface IZipCode
  {
    string ZipCode { get; set;}
  }

  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    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.
    [Personalizable()]
    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()
    {
      Controls.Clear();
      input = new TextBox();
      this.Controls.Add(input);
      send = new Button();
      send.Text = "Enter 5-digit Zip Code";
      send.Click += new EventHandler(this.submit_Click);
      this.Controls.Add(send);
    }

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

  }

  [AspNetHostingPermission(SecurityAction.Demand,
    Level = AspNetHostingPermissionLevel.Minimal)]
  [AspNetHostingPermission(SecurityAction.InheritanceDemand,
    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)
    {
      EnsureChildControls();

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

    protected override void CreateChildControls()
    {
      Controls.Clear();
      DisplayContent = new Label();
      this.Controls.Add(DisplayContent);
    }

  }
}

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, while the Button2_Click method disconnects the controls.

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

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


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

After you load the page, click the Connect button to connect the controls. Then click the verbs menu in one of the controls (the downward arrow in the header of the control), and select Close from the verbs menu. When you try to close the control, the overridden method is called, the connection is ended, and the message is written to the page. If you want to reset the page to restore the closed control and experiment with other options, click the Reset User State link to remove personalization data and restore the page's original state.

.NET Framework

Supported in: 4.5.2, 4.5.1, 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