concept WebPartManager in category asp.net

appears as: WebPartMager, WebPartManager, WebPartMager, The WebPartMager
ASP.Net 2.0 Web Parts in Action: Building Dynamic Web Portals

This is an excerpt from Manning's book ASP.Net 2.0 Web Parts in Action: Building Dynamic Web Portals.

While viewing the page in the browser, we can see that what was seemingly a large mess of CSS and HTML has become the basis for our portal. Notice also that the base layout logic which we put into the master page has merged seamlessly with the content we put into the content page. The left panel in the content section of the page is where the web parts will live, whereas the right panel will contain the editor zone for managing the properties of web parts and the catalog zone, so that users can choose which parts to add or remove from their page. Of course, before we go ahead and add those elements to the page, we must add the WebPartManager control to the page and ensure that it is the first web part control in the control hierarchy.

After adding the WebPartManager to the content page, we can go ahead and add two WebPartZones, an EditorZone and a CatalogZone. After adding those controls, the page should now look similar to figure A.3 when it is displayed in design mode within the Visual Studio Editor.

What are the many hats a WebPartManager can wear? Tracking, managing personalization, controlling lifecycle events, switching page displays, and importing and exporting web parts, to name a few. That’s a stack of hats. To get some sense of the importance of the WebPartManager’s role, consider the following five categories of tasks and activities that are displayed in table 4.1.

Table 4.1. The WebPartManager is a versatile control that manages a wide range of activities

Category

Activities

Tracking web parts Adding web parts to zones; closing web parts; moving web parts; tracking which web parts belong in which zones.
Managing Personalization Data Initializing web parts; loading personalization data; saving personalization data.
Controlling Lifecycle Events Creating web parts; initializing connections.
Switching Page Display Modes Switching the mode of the page.
Importing and Exporting Web Parts Serializing and de-serializing web parts for the export process.

The WebPartManager manages each of these activities and exposes event notifications when they occur. As you can see, there’s really not a great deal that goes on without the involvement of the WebPartManager. Let’s take a high-level tour through each of these categories to get a better sense of them.

We’ve already seen that the mode of the page is affected by setting the DisplayMode property of the current WebPartManager instance to one of the modes listed above. The manager class also exposes a property named SupportedDisplayModes that allows us to obtain a listing of all modes available at any particular moment. In a typical web part application we would display a list of the available modes to the user, allowing them to switch modes so that they could perform a specific set of tasks. For example, it might be desirable for the administrators of a portal to be able to log in and change certain features of the web parts, such as the back color or their display title. By picking from a list of available modes, the users could switch their web page from its default browse mode and into edit mode. Once in edit mode, the user can make changes to web parts and then switch back into browse mode to continue using the site as normal. Listing 4.1 illustrates how to present a list of valid modes in a dropdown list.

Listing 4.1. Using the SupportedDisplayModes property of the WebPartManager to present the user with a list of modes to choose from.

4.6. Using WebPartManager with master pages

At your first meeting with master pages, you’ll do little more than shake hands, but you’ll see how to use them and also gain an understanding for the effect that they have on the interactions between web parts and the WebPartManager, and that’s a good start. Master pages are new in ASP.NET 2.0 and will be a very popular feature because they enable us to easily apply a consistent layout to an entire web application. To achieve this, one or more master pages is created that defines the layout for the common user interface elements. If you’ve ever used master pages in PowerPoint presentations, this should sound familiar, because an ASP.NET master page also contains behaviors that are common to all pages. Each actual page contains only unique content; and all the common elements such as menus, headers, and footers are contained within the master page file.

For web part applications, we can use master pages and content placeholders to declare the WebPartManager and other common controls within the master page file—this could include controls such as the EditorZone and CatalogZone controls as well. When the WebPartManager is declared in a master page, it cannot declare the StaticConnections for parts within content pages. As you will remember from the last chapter, the WebPartManager allows you to explicitly declare static connections between two parts on a web page within the body of the WebPartManager tag itself through the use of a StaticConnections element:

<asp:WebPartManager ID="WebPartManager1" runat="server">
        <StaticConnections>
               <asp:WebPartConnection
                      ID="cnn"
                      ConsumerID="NumberConsumer1"
                      ProviderID="NumberProvider1"
                      />
        </StaticConnections>
</asp:WebPartManager>

So that static connections can be declared when the web part manager is contained within a master page, the web part framework provides us with the ProxyWebPartManager which can be declared in the content pages and used to define the static connections. You actually create the ProxyWebPartManager in the same way that you would create a WebPartManager, and at runtime the ProxyWebPartManager communicates information through to the WebPartManager on our behalf. If you don’t actually have any static connections to declare, then you can get away without having to add a ProxyWebPartManager.

<asp:ProxyWebPartManager ID="ProxyWebPartManager1" runat="server">
        <StaticConnections>
               <asp:WebPartConnection ... />
        </StaticConnections>
</asp:ProxyWebPartManager>

Well that’s it; well done. It’s been quite a journey exploring the WebPartManager; but it’s time well spent because you’ve shored up the knowledge you need to dig in and work the deep behavioral customizations that your customers will demand. Armed with this knowledge, let’s re-visit the Adventure Works portal to see how the application of these WebPartManager customizations will affect our code base.