Export (0) Print
Expand All
1 out of 2 rated this helpful - Rate this topic

ConnectionsZone Class

Provides a user interface (UI) that enables users to form connections between WebPart and other server controls that reside in WebPartZoneBase zones.

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

public class ConnectionsZone : ToolZone
public class ConnectionsZone extends ToolZone
public class ConnectionsZone extends ToolZone
Not applicable.

Using the Web Parts control set, you can enable two server controls to form a connection and share data, with one control acting as the provider, and the other acting as the consumer of the data. The two controls can be WebPart controls or any other type of server control, provided that they are designed to handle connections, and that they are residing in a WebPartZoneBase zone. To learn more about Web Parts connections, see the WebPartConnection and ConnectionPoint class overviews, as well as Web Parts Connections Overview.

Provided that the requisite controls and conditions exist to form a Web Parts connection, it is still necessary to actually connect the controls. There are three ways to form a connection between server controls: declaring a connection in a Web page, creating a connection in code, or adding a ConnectionsZone control to the page so that users can connect the controls on demand. The ConnectionsZone control generates a UI that enables users to connect or disconnect any server controls on a page that meet the conditions necessary to form a connection. It is an optional control that is not necessary to form connections, but is useful in the case where you want to give users control over which server controls are connected or disconnected.

The ConnectionsZone control is one of the Web Parts tool zone controls that inherit from the ToolZone base class. As a tool zone, the ConnectionsZone control is designed to be visible only when its Web page is in a certain display mode. In this case, the display mode is named connect mode (a page is in this mode when the WebPartManager control on the page has its DisplayMode property value set to ConnectDisplayMode). After users switch a page into connect mode, they must click a connect verb on the verbs menu of one of the server controls, and then the connection UI becomes visible.

As a Web Parts zone control, the ConnectionsZone control is a type of WebZone zone (which inherits from the CompositeControl class) designed to contain other controls. In general, a ConnectionsZone zone has most of the same elements as other Web Parts tool zones: a header, a body or contents area, and a footer. For a complete discussion of what a Web Parts zone is, and the different parts of a zone, see the WebZone class overview.

NoteImportant:

Unlike most other Web Parts zones, it is important to note that a ConnectionsZone zone does not contain a unique type of server control associated with it. For a list of zones and the associated controls they contain, see the chart in the WebZone class overview. But the ConnectionsZone zone does not contain WebPartConnection controls. Instead, it serves a very limited purpose of providing a UI for users to connect or disconnect server controls that exist in some WebPartZoneBase zone on the page. The only controls contained in a ConnectionsZone control are the standard ASP.NET server controls that it generates as part of its UI for forming connections.

When the ConnectionsZone control is rendered, it generates a UI based on the server controls on the page that are capable of forming connections. The ConnectionsZone control determines which server controls in the WebPartZoneBase zone on the page are providers, which are consumers, which connection points are available, and whether the server controls are currently connected or disconnected, and then it generates the UI accordingly.

For example, suppose there is one WebPart control capable of being a provider, one WebPart control capable of being a consumer, they are declared in a WebPartZone on the page, and they are currently disconnected. When a user switches the page to connect mode and clicks the connect verb on one of the controls, the ConnectionsZone control generates a UI with a link that, when clicked, displays a form in which users can choose options to create a connection. (If the controls were previously connected, the initial view instead presents users with a button to disconnect the controls). In the connection UI for creating new connections, users are shown which control is a provider, and which control is a consumer. A drop-down list control appears below each server control, listing the available ConnectionPoint objects for the control. From the respective drop-down lists, users must select one ProviderConnectionPoint object for the provider (to determine what interface and data will be shared with consumers), and one ConsumerConnectionPoint object for each consumer (to determine what interface and data the consumer will consume) that will be connected to the provider.

NoteNote:

In the Web Parts control set default implementation, one provider can connect to many consumers, but a consumer can have only one provider.

To use the ConnectionsZone control, you can declare it within the <form> element on a Web page (but not nested within another Web Parts zone element), using the <asp:connectionszone> element, or you can add it to the page programmatically. If you declare the element in a page, unlike the other Web Parts zones, you cannot declare any other type of server controls between the tags of the <asp:connectionszone> element. You can declare elements within it that relate to its own properties and style details, but it is a standalone element and is not a template control within which other server controls can be declared.

NoteNote:

To improve accessibility, the ConnectionsZone control is rendered within a <fieldset> element. The <fieldset> element groups the related set of controls used for establishing connections in the ConnectionsZone control, and it facilitates tabbed navigation among those controls for both visual user agents (such as ordinary Web browsers) and speech-oriented user agents (such as screen-reading software).

The ConnectionsZone control has a number of properties it uses for rendering the connection UI. One set of properties includes several verbs--used only in relation to connections--that carry out actions in the UI: ConfigureVerb, ConnectVerb, and DisconnectVerb. A large set of properties, used especially for the connections zone UI, consists of text strings displayed at various places (or in certain situations, such as when errors occur) in the UI: ConfigureConnectionTitle, ConnectToConsumerInstructionText, ConnectToConsumerText, ConnectToConsumerTitle, ConnectToProviderInstructionText, ConnectToProviderText, ConnectToProviderTitle, ConsumersInstructionText, ConsumersTitle, ExistingConnectionErrorMessage, GetFromText, GetText, InstructionTitle, NewConnectionErrorMessage, NoExistingConnectionInstructionText, NoExistingConnectionTitle, ProvidersInstructionText, ProvidersTitle, SendText, and SendToText. The ConnectionsZone class also contains a number of common properties found in other Web Parts zones: CancelVerb, CloseVerb, Display, EmptyZoneText, HeaderText, InstructionText, and PartChromeType. Finally, the WebPartToConnect property is unique to the class, referencing the control that initiates the connection (this is the control on which the user clicks the connect verb in its verbs menu, which is also the control referenced in the WebPartManager control's SelectedWebPart property).

The ConnectionsZone class also has a number of methods, all of which are inherited and overridden from base classes, and most of them come from the base Web Parts zone classes. For details, see the individual methods.

Notes to Inheritors: The ConnectionsZone class can be extended, if developers want to change its behavior or the default UI that it provides for working with connections.

The following code example demonstrates how to use the ConnectionsZone control on a Web Parts page. The example has four parts:

  • A user control that enables you to switch display modes on the Web page.

  • A source file that contains the code for a ZIP Code interface, and two WebPart controls acting as the provider and the consumer for a connection.

  • A Web page that hosts all the controls, demonstrates how to declare an <asp:connectionszone> element, and sets a number of properties on the connections zone declaratively and programmatically.

  • An explanation of how the example works in a browser.

The first part of this code example is the user control that enables users to switch display modes on a Web page. For details about display modes and a description of the source code in this control, see Walkthrough: Changing Display Modes on a Web Parts Page.

<%@ control language="C#" classname="DisplayModeMenuCS"%>
<script runat="server">
  
 // Use a field to reference the current WebPartManager.
  WebPartManager _manager;

  void Page_Init(object sender, EventArgs e)
  {
    Page.InitComplete += new EventHandler(InitComplete);
  }  

  void InitComplete(object sender, System.EventArgs e)
  {
    _manager = WebPartManager.GetCurrentWebPartManager(Page);

    String browseModeName = WebPartManager.BrowseDisplayMode.Name;

    // Fill the dropdown with the names of supported display modes.
    foreach (WebPartDisplayMode mode in _manager.SupportedDisplayModes)
    {
      String modeName = mode.Name;
      // Make sure a mode is enabled before adding it.
      if (mode.IsEnabled(_manager))
      {
        ListItem item = new ListItem(modeName, modeName);
        DisplayModeDropdown.Items.Add(item);
      }
    }

    // If shared scope is allowed for this user, display the scope-switching
    // UI and select the appropriate radio button for the current user scope.
    if (_manager.Personalization.CanEnterSharedScope)
    {
      Panel2.Visible = true;
      if (_manager.Personalization.Scope == PersonalizationScope.User)
        RadioButton1.Checked = true;
      else
        RadioButton2.Checked = true;
    }
    
  }
 
  // Change the page to the selected display mode.
  void DisplayModeDropdown_SelectedIndexChanged(object sender, EventArgs e)
  {
    String selectedMode = DisplayModeDropdown.SelectedValue;

    WebPartDisplayMode mode = _manager.SupportedDisplayModes[selectedMode];
    if (mode != null)
      _manager.DisplayMode = mode;
  }

  // Set the selected item equal to the current display mode.
  void Page_PreRender(object sender, EventArgs e)
  {
    ListItemCollection items = DisplayModeDropdown.Items;
    int selectedIndex = 
      items.IndexOf(items.FindByText(_manager.DisplayMode.Name));
    DisplayModeDropdown.SelectedIndex = selectedIndex;
  }

  // Reset all of a user's personalization data for the page.
  protected void LinkButton1_Click(object sender, EventArgs e)
  {
    _manager.Personalization.ResetPersonalizationState();
  }

  // If not in User personalization scope, toggle into it.
  protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
  {
    if (_manager.Personalization.Scope == PersonalizationScope.Shared)
      _manager.Personalization.ToggleScope();
  }

  // If not in Shared scope, and if user is allowed, toggle the scope.
  protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
  {
    if (_manager.Personalization.CanEnterSharedScope && 
        _manager.Personalization.Scope == PersonalizationScope.User)
      _manager.Personalization.ToggleScope();
  }
</script>
<div>
  <asp:Panel ID="Panel1" runat="server" 
    Borderwidth="1" 
    Width="230" 
    BackColor="lightgray"
    Font-Names="Verdana, Arial, Sans Serif" >
    <asp:Label ID="Label1" runat="server" 
      Text="&nbsp;Display Mode" 
      Font-Bold="true"
      Font-Size="8"
      Width="120" 
      AssociatedControlID="DisplayModeDropdown"/>
    <asp:DropDownList ID="DisplayModeDropdown" runat="server"  
      AutoPostBack="true" 
      Width="120"
      OnSelectedIndexChanged="DisplayModeDropdown_SelectedIndexChanged" />
    <asp:LinkButton ID="LinkButton1" runat="server"
      Text="Reset User State" 
      ToolTip="Reset the current user's personalization data for the page."
      Font-Size="8" 
      OnClick="LinkButton1_Click" />
    <asp:Panel ID="Panel2" runat="server" 
      GroupingText="Personalization Scope"
      Font-Bold="true"
      Font-Size="8" 
      Visible="false" >
      <asp:RadioButton ID="RadioButton1" runat="server" 
        Text="User" 
        AutoPostBack="true"
        GroupName="Scope" OnCheckedChanged="RadioButton1_CheckedChanged" />
      <asp:RadioButton ID="RadioButton2" runat="server" 
        Text="Shared" 
        AutoPostBack="true"
        GroupName="Scope" 
        OnCheckedChanged="RadioButton2_CheckedChanged" />
    </asp:Panel>
  </asp:Panel>
</div>

The second part of the example is the source file with the interface and custom controls. Notice that the ZipCodeWebPart control implements the IZipCode interface, adding a ConnectionProvider attribute so that the control can act as a provider for a connection. The WeatherWebPart control has a method marked with a ConnectionConsumer attribute, where it consumes the IZipCode interface, so it can act as a consumer in a connection.

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. For a walkthrough that demonstrates how to compile, see Walkthrough: Developing and Using a Custom 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 Provider", "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 Consumer", "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 example code is the Web page. Near the top are Register directives for the user control and the custom controls used in the connection. An <asp:connectionszone> element is declared in the page as an example of using the ConnectionsZone control declaratively. Within the element, a number of properties are set declaratively. Other properties on the connections zone are set programmatically in the <script> section of the page.

<%@ 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 Page_PreRender(object sender, EventArgs e)
  {
     // Set properties on verbs.
     connectionsZone1.CancelVerb.Description = 
       "Terminates the connection process";
     connectionsZone1.CloseVerb.Description = 
       "Closes the connections UI";
     connectionsZone1.ConfigureVerb.Description =
       "Configure the transformer for the connection";
     connectionsZone1.ConnectVerb.Description =
       "Connect two WebPart controls";
     connectionsZone1.DisconnectVerb.Description =
       "End the connection between two controls";
    
     // Set properties for UI text strings.
     connectionsZone1.ConfigureConnectionTitle = 
       "Configure";
     connectionsZone1.ConnectToConsumerInstructionText = 
       "Choose a consumer connection point";
     connectionsZone1.ConnectToConsumerText = 
       "Select a consumer for the provider to connect with";
     connectionsZone1.ConnectToConsumerTitle = 
       "Send data to this consumer";
     connectionsZone1.ConnectToProviderInstructionText =
       "Choose a provider connection point";
     connectionsZone1.ConnectToProviderText =
       "Select a provider for the consumer to connect with";
     connectionsZone1.ConnectToProviderTitle =
       "Get data from this provider";
     connectionsZone1.ConsumersInstructionText = 
       "WebPart controls that receive data from providers";
     connectionsZone1.ConsumersTitle = "Consumer Controls";
     connectionsZone1.GetFromText = "Receive from";
     connectionsZone1.GetText = "Retrieve";
     connectionsZone1.HeaderText = 
      "Create and Manage Connections";
     connectionsZone1.InstructionText = 
      "Manage connections for the selected WebPart control";
     connectionsZone1.InstructionTitle = 
       "Manage connections for consumers or providers";
     connectionsZone1.NoExistingConnectionInstructionText = 
       "No connections exist. Click the above link to create "
       + "a connection.";
     connectionsZone1.NoExistingConnectionTitle = 
       "No current connections";
     connectionsZone1.ProvidersInstructionText =
       "WebPart controls that send data to consumers";
     connectionsZone1.ProvidersTitle = "Provider controls";
     
  }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Connection Zone Sample</title>
</head>
<body>
  <form id="form1" runat="server">
  <asp:webpartmanager runat="server" id="mgr">
    <staticconnections>
      <asp:webpartconnection id="connection1" 
        consumerconnectionpointid="ZipCodeConsumer"
        consumerid="zipConsumer"
        providerconnectionpointid="ZipCodeProvider" 
        providerid="zipProvider" />
    </staticconnections>
  </asp:webpartmanager>
  <uc1:displaymodemenucs id="menu1" runat="server" />
  <div>
  <asp:webpartzone id="WebPartZone1" runat="server">
    <zonetemplate>
      <aspsample:zipcodewebpart id="zipProvider" runat="server" 
        Title="Zip Code Provider"  />
      <aspsample:weatherwebpart id="zipConsumer" runat="server" 
        Title="Zip Code Consumer" />
    </zonetemplate>
  </asp:webpartzone>
  <asp:connectionszone id="connectionsZone1" runat="server" >
    <cancelverb text="Terminate" />
    <closeverb text="Close Zone" />
    <configureverb text="Configure" />
    <connectverb text="Connect Controls" />
    <disconnectverb text="End Connection" />
  </asp:connectionszone>
  </div>
  </form>
</body>
</html>

Load the Web page in a browser. Use the Display Mode drop-down list control to switch the page to connect mode. On the verbs menu of the ZIP Code Provider control (the verbs menu is indicated by the downward arrow in the control's title bar), click the connect verb. A ConnectionsZone control appears. Note that in the connection UI, an End Connection button appears; the connection has already been declared in the markup of the page, so the controls are already connected. Click End Connection, and then use the Display Mode control again to return the page to browse mode. Next, return the page to connect mode again, click the connect verb on one of the controls, and note that the connection UI now displays a hyperlink that enables you to form a connection between the controls. Click the link, and use the connection UI to select connection points and establish the connection.

System.Object
   System.Web.UI.Control
     System.Web.UI.WebControls.WebControl
       System.Web.UI.WebControls.CompositeControl
         System.Web.UI.WebControls.WebParts.WebZone
           System.Web.UI.WebControls.WebParts.ToolZone
            System.Web.UI.WebControls.WebParts.ConnectionsZone
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.