Managing Web Parts with the WebPartManager Control


Managing Web Parts with the WebPartManager Control

This final section of this chapter takes a closer look at the WebPartManager control. The WebPartManager control is responsible for tracking all the Web Parts in a page. It exposes the primary application programming interface for working with Web Parts.

The WebPartManager control supports the following particularly useful properties (this is not a complete list):

  • AvailableTransformers Represents all the transformers available on the page. Transformers are used when you connect incompatible Web Parts.

  • Connections Represents all the connections between Web Parts in a page.

  • DisplayMode Represents the current Display mode.

  • DisplayModes Returns a collection of all the Display modes associated with the WebPartManager.

  • Personalization Represents the WebPartPersonalization class, which tracks personalization data.

  • SelectedWebPart Represents the Web Part that is currently selected on the page. For example, when you select a Web Part control's Edit menu option, the Web Part becomes the selected Web Part.

  • SupportedDisplayModes Represents all the Display modes supported by the current page,

  • WebParts Represents all the Web Parts on the page,

  • Zones Represents of the Web Part Zones on the page,

Some of these properties require additional explanation. Both the DisplayModes and the SupportedDisplayModes properties return a collection of Web Part Display Modes. However, in some situations, the DisplayModes property returns more Display Modes than the SupportedDisplayModes property. For example, if a page does not include a Catalog Zone, then the SupportedDisplayModes property doesn't return CatalogDisplayMode as one of its values.

You can take advantage of the SupportDisplayModes property to automatically populate a Menu control with the list of available Display Modes like this:

Sub Page_Load()   If Not Page.IsPostBack Then     For Each mode As WebPartDisplayMode in WebPartManager1.SupportedDisplayModes       Menu1.Items.Add(new MenuItem(mode.Name))     Next   End If End Sub


The Zones property represents all the WebPartZone controls on a page. However, it does not include tool zones such as Catalog or Editor Zones.

The WebPartManager control also supports a number of useful methods:

  • AddWebPart Enables you to add a new Web Part to a Web Part Zone.

  • CanConnectWebParts Enables you to determine whether two Web Parts can be connected in a page. This method optionally enables you to specify a Transformer for the connection.

  • CloseWebPart Enables you to close a Web Part.

  • ConnectWebParts Enables you to connect two Web Parts in a page. This method optionally enables you to specify a Transformer for the connection.

  • CreateWebPart Enables you to create a new Generic Web Part from a control.

  • DeleteWebPart Enables you to delete a Web Part from a page.

  • DisconnectWebParts Enables you to break the connection between two Web Parts.

  • ExportWebPart Enables you to export Web Part settings to XML.

  • GetConsumerConnectionPoints Enables you to get all the consumer connection points exposed by a Web Part.

  • GetCurrentWebPartManager Enables you to get a reference to a page's WebPartManager control from any user control or component used in a page. This is a shared (static) method.

  • GetGenericWebPart Enables you to retrieve an existing Web Part as a Generic Web Part.

  • GetProviderConnectionPoints Enables you to get all the provider connection points exposed by a Web Part.

  • ImportWebPart Enables you to import Web Part settings from XML.

  • IsAuthorized Enables you to create an authorization filter.

  • MoveWebPart Enables you to move a Web Part between Web Part Zones, or change the position of a Web Part in a Web Part Zone.

Notice that everything that you can do from particular tool zonessuch as Editor and Catalog Zonesyou can do using the methods of the WebPartManager control.

For example, the page in Listing 28.31 illustrates how you can dynamically rearrange the Web Parts in a Web Part Zone every time the page is requested.

Listing 28.31. DynamicWebParts.aspx

[View full width]

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Private  Sub Page_PreRender()         Dim rnd As Random =  New Random()         Dim webPartToMove As WebPart = WebPartManager1.WebParts(rnd.Next(WebPartManager1 .WebParts.Count))         Dim NewZoneIndex As Integer =  rnd.Next(webPartToMove.Zone.WebParts.Count)         WebPartManager1.MoveWebPart(webPartToMove, webPartToMove.Zone, NewZoneIndex)     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <style type="text/css">         .column         {             width:400px;             height:200px;             margin-right:10px;             border:solid 1px black;             background-color: white;         }         html         {             background-color:#eeeeee;         }     </style>     <title>Dynamic Web Parts</title> </head> <body>     <form  runat="server">     <asp:WebPartManager                  Runat="server" />         <asp:WebPartZone                          Css             Runat="server">             <ZoneTemplate>             <asp:Panel                                  Title="Featured Books"                 Runat="server">                 <ul>                     <li>Blink</li>                     <li>A Theory of Justice</li>                 </ul>             </asp:Panel>             <asp:Panel                                  Title="Featured Movies"                 Runat="server">                 <ul>                     <li>Star Wars: Episode III</li>                     <li>Blade Runner</li>                 </ul>             </asp:Panel>             <asp:Panel                                  Title="Featured Music"                 Runat="server">                 <ul>                     <li>Black Eyed Peas</li>                     <li>Coldplay</li>                 </ul>             </asp:Panel>             </ZoneTemplate>         </asp:WebPartZone>     </form> </body> </html>

In Listing 28.31, the MoveWebPart() method called in the Page_Load() event handler is used to randomly move one of the Web Parts in the Featured Products Zone to a new position (see Figure 28.10).

Figure 28.10. Randomly Positioning Web Parts.


Finally, the WebPartManager control supports a number of useful events:

  • AuthorizeWebPart Raised when the WebPartManager control adds each Web Part to a page (and Web Parts are displayed in the Catalog Zone).

  • ConnectionsActivated Raised after a connection between two Web Parts is activated.

  • ConnectionsActivating Raised before a connection between two Web Parts is activated.

  • DisplayModeChanged Raised after the Web Part Display Mode is changed.

  • DisplayModeChanging Raised before the Web Part Display Mode is changed.

  • SelectedWebPartChanged Raised after a Web Part is selected (for example, selected for editing).

  • SelectedWebPartChanging Raised before a Web Part is selected (for example, selected for editing).

  • WebPartAdded Raised after a new Web Part is added to the page.

  • WebPartAdding Raised before a new Web Part is added to the page.

  • WebPartClosed Raised after a Web Part is closed.

  • WebPartClosing Raised before a Web Part is closed.

  • WebPartDeleted Raised after a Web Part is deleted.

  • WebPartDeleting Raised before a Web Part is deleted.

  • WebPartMoved Raised after a Web Part is moved.

  • WebPartMoving Raised before a Web Part is moved.

  • WebPartsConnected Raised after a Web Part is connected.

  • WebPartsConnecting Raised before a Web Part is connected.

  • WebPartsDisconnected Raised after a Web Part is disconnected.

  • WebPartsDisconnecting Raised before a Web Part is disconnected.

Notice that most of these events come in pairs. There is a DisplayModeChanging event and a DisplayModeChanged event. You can cancel the action associated with most of these events within the ing event handler.

The page in Listing 28.32 handles the WebPartClosed event to display an informational message after a user closes a Web Part.

Listing 28.32. ShowCloseWarning.aspx

[View full width]

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"   "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"> <script runat="server">     Protected Sub WebPartManager1_WebPartClosed(ByVal sender As Object, ByVal e As  WebPartEventArgs)         divCloseWarning.Visible = True     End Sub     Protected Sub Menu1_MenuItemClick(ByVal sender As Object, ByVal e As MenuEventArgs)         WebPartManager1.DisplayMode = WebPartManager1.DisplayModes(e.Item.Text)     End Sub </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head  runat="server">     <style type="text/css">         .divCloseWarning         {             position:absolute;             background-color:#eeeeee;             padding:15px;             left:200px;             top:50px;             border:double 3px red;             width:200px;         }         .column         {             float:left;             width:40%;             height:200px;             margin-right:10px;             border:solid 1px black;             background-color: white;         }         .menu         {             margin:5px 0px;         }         html         {             background-color:#eeeeee;         }     </style>     <title>Show Close Warning</title> </head> <body>     <form  runat="server">     <asp:WebPartManager                  OnWebPartClosed="WebPartManager1_WebPartClosed"         Runat="server" />     <asp:Menu                  OnMenuItemClick="Menu1_MenuItemClick"         Orientation="Horizontal"         Css         Runat="server">         <Items>         <asp:MenuItem Text="Browse" />         <asp:MenuItem Text="Design" />         <asp:MenuItem Text="Catalog" />         </Items>     </asp:Menu>     <asp:WebPartZone                  Css         Runat="server">         <ZoneTemplate>         <asp:Label                          Title="First Web Part"             Text="Contents of First Web Part"             Runat="server" />         <asp:Label                          Title="Second Web Part"             Text="Contents of Second Web Part"             Runat="server" />         </ZoneTemplate>     </asp:WebPartZone>     <div                           Visible="false"         Enableviewstate="false"         Runat="server">         You have closed a Web Part. You         can reopen the Web Part by opening         the Page Catalog.         <br /><br />         <asp:Button                          Text="OK"             Runat="server" />     </div>     <asp:CatalogZone                  Css         runat="server">         <ZoneTemplate>         <asp:PageCatalogPart                          Runat="server" />         </ZoneTemplate>     </asp:CatalogZone>     </form> </body> </html>

After you open the page in Listing 28.32, you can close a Web Part by selecting a Web Part control's Close menu option. When you close a Web Part, an informational message appears (see Figure 28.11).

Figure 28.11. Displaying a Close warning.





ASP. NET 2.0 Unleashed
ASP.NET 2.0 Unleashed
ISBN: 0672328232
EAN: 2147483647
Year: 2006
Pages: 276

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net