Export (0) Print
Expand All

WebPartManager.ConnectDisplayMode Field

Represents the display mode used for displaying a special user interface (UI) for users to manage connections between WebPart controls. This field is read-only.

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

public static readonly WebPartDisplayMode ConnectDisplayMode

The ConnectDisplayMode field references a custom WebPartDisplayMode object that is created and contained by the WebPartManager control. Because this is a static object, you can refer to it directly through the WebPartManager class without needing an instance of the control.

When users want to manage connections between WebPart controls on a Web page, if a ConnectionsZone zone has been declared on the page, they can switch the page into the ConnectDisplayMode mode. The connect display mode displays a special UI for managing connections, which includes the ability to connect or disconnect controls, and to edit the details of existing connections.

If you want to provide users with the ability to manage connections with the UI provided by the Web Parts control set, you must declare an <asp:connectionszone> element in the markup of a page. Unlike the elements for the other types of WebZone zones, you do not need to add any other tags within this element; you simply declare the element by itself.

The following code example demonstrates the usage of the ConnectDisplayMode mode.

The code example has three parts:

  • A source file that contains an interface and custom WebPart controls that can form a connection.

  • A Web page that provides a connection UI and demonstrates working with the ConnectDisplayMode mode.

  • An explanation of how to run the example.

The first part of the code example is a source file that contains an interface and two custom WebPart controls that are designed so they can 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 code example uses the dynamic compilation approach. 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;

  [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 second part of the example is a Web page that hosts the custom controls. Within the server <script> tags on the page are several methods that populate a drop-down list with the display modes available on the page. A user can select these from the drop-down list to change the page's display mode. One of the available display modes is connect display mode, because an <asp:connectionszone> element is declared in the page's markup. Notice that this element does not contain any other child elements; it exists only to enable the connection management UI for users.

The ConnectDisplayMode mode appears in this example in two places. First, in the Page_Init method, the connect display mode is added to the drop-down list of display modes, as the code loops through the collection referenced in the SupportedDisplayModes property. Second, the Page_PreRender method checks the current display mode on the page, and if the current mode is ConnectDisplayMode, a message is displayed in a Label control.

<%@ Page Language="C#" %>
<%@ 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 Page_Init(object sender, EventArgs e)
  {
    foreach (WebPartDisplayMode mode in mgr.SupportedDisplayModes)
    {
      string modeName = mode.Name;
      if (mode.IsEnabled(mgr))
      {
        ListItem item = new ListItem(modeName, modeName);
        DisplayModeDropdown.Items.Add(item);
      }      
    }
  }

  protected void DisplayModeDropdown_SelectedIndexChanged(object 
    sender, EventArgs e)
  {
    String selectedMode = DisplayModeDropdown.SelectedValue;
    WebPartDisplayMode mode = 
      mgr.SupportedDisplayModes[selectedMode];
    if (mode != null)
      mgr.DisplayMode = mode;
  }

  protected void Page_PreRender(object sender, EventArgs e)
  {
    if (mgr.DisplayMode == WebPartManager.ConnectDisplayMode)
      Label1.Visible = true;
    else
      Label1.Visible = false;
  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
      <asp:WebPartManager ID="mgr" runat="server" />    
      <asp:Label ID="Label1" runat="server" 
        Text="Currently in Connect Display Mode" 
        Font-Bold="true"
        Font-Size="125%" />
      <br />
      <asp:DropDownList ID="DisplayModeDropdown" 
        runat="server" 
        AutoPostBack="true"
        Width="120"
        OnSelectedIndexChanged=
        "DisplayModeDropdown_SelectedIndexChanged">
      </asp:DropDownList>
      <hr />
      <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" />
    </div>
    </form>
</body>
</html>

After you load the page in a browser, click the drop-down list and select Connect to switch the page into connect display mode. Notice that a message appears, telling you that the page is in connect display mode. Now click the verbs menu (an arrow symbol) in the title bar of one of the WebPart controls, and then click Connect in the verbs menu. After the connection UI is displayed, click the link to create a connection. Use the drop-down list within the connection UI that appears, select the other control that will participate in the connection, and click the Connect button. The connection is established. Click the Close button, and then use the drop-down list at the top of the page to return the page to browse display mode.

.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