Compartir a través de


How to: Declare a Static Connection between Two Web Parts Controls

Developers can create a static Web Parts connection between two server controls by declaring the connection in page persistence format. A static connection becomes a permanent object on a Web page, like a declared control. All users can see a static connection (because it is a shared object), and they can never delete the connection object, although they can disconnect it if provided with the user interface (UI) options for doing so. Static connections are a good option when you want a connection that is always available to all users, and you do not want users to have the option of deleting it from the page. For details about connections, see WebPartConnection and Web Parts Connections Overview. For details about static connections, see IsStatic. This topic demonstrates how to create a static connection between two WebPart controls.

NoteNote

This topic focuses on showing how to declare a static connection in the markup of a Web page. To declare a static Web Parts connection between two server controls, the controls must be properly designed to handle connections, and they must reside in a WebPartZoneBase zone. For a full description of the requirements for controls that participate in connections, see WebPartConnection. To obtain the example code for the two WebPart controls and the Web page used to host the static connection that is demonstrated in this topic, see the Example section for the IsStatic property.

To declare server controls within a zone

  1. Add a <webpartzone> element within the body section of the Web page that will contain your connection, and add a child <zonetemplate> element to contain the server controls that you add to the zone. The code should look like the following example.

    <asp:WebPartZone ID="WebPartZone1" runat="server">
      <ZoneTemplate>
      </ZoneTemplate>
    </asp:WebPartZone>
    
  2. Between the tags of the <zonetemplate> element, declare the server controls that will act as the provider and the consumer controls in a Web Parts connection. These controls must already be designed to participate in connections. You can also add other server controls to the zone. As noted previously in this topic, the controls declared in this example are obtained from the example code found in the IsStatic property. The code for the entire zone with the server controls looks like the following.

    <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> 
    

To declare a static connection

  1. Your Web page should already have an <asp:webpartmanager> element, which is required on pages that use Web Parts controls. Add a <staticconnections> element as a child of the <asp:webpartmanager> element to contain one or more declared static connections. The code should look like the following example.

    <asp:WebPartManager ID="mgr" runat="server" >
      <StaticConnections>
      </StaticConnections> 
    </asp:WebPartManager> 
    
  2. Within the <staticconnections> element, declare an <asp:webpartconnection> element. For the connection, you must specify the following required attributes in addition to the id and runat attributes:

    • ConsumerID - Indicates the ID of the consumer control in the connection.

    • ConsumerConnectionPointID - Indicates the ID of a special callback method in the consumer used to establish the connection. This attribute is required only if the consumer has more than one connection point. For details on connection points, see ConnectionPoint.

    • ProviderID - Indicates the ID of the provider control in the connection.

    • ProviderConnectionPointID - Indicates the ID of a special callback method in the provider used to establish the connection. This attribute is required only if the provider has more than one connection point.

    The completed code for the <asp:webpartmanager> element and the child static connection should look like the following example.

    <asp:WebPartManager ID="mgr" runat="server" >
      <StaticConnections>
        <asp:WebPartConnection ID="conn1"
          ConsumerConnectionPointID="ZipCodeConsumer"
          ConsumerID="weather1" 
          ProviderConnectionPointID="ZipCodeProvider" 
          ProviderID="zip1" />
      </StaticConnections>      
    </asp:WebPartManager>
    

See Also

Reference

WebPartConnection
IsStatic

Concepts

Web Parts Connections Overview
ASP.NET Web Parts Overview