Recipe 11.3. Creating a Reusable Web Parts Catalog


Problem

You need to reuse the same set of web parts in many pages in your application but you want to avoid having to declare them on every page. Instead, you'd like to create a reusable web parts catalog you can maintain separately but make available to the pages.

Solution

Implement the solution described in Recipe 11.1, create a user control containing all of the web parts to be available in your pages, remove the <WebPartsTemplate> element and control definitions from the DeclarativeCatalogPart control, and set the WebPartsListUserControlPath attribute of the DeclarativeCatalogPart to the name of the user control you created.

In the .ascx file for the user control that will contain all available web parts, add the server controls, user controls, and custom web parts in the same manner you would add them to a .aspx file.

In the .aspx file:

  1. Remove the <WebPartsTemplate> element and control definitions from the DeclarativeCatalogPart control.

  2. Set the WebPartsListUserControlPath attribute of the DeclarativeCatalogPart to the name of the user control containing the available web parts.

The application we have implemented to demonstrate the solution is shown in Examples 11-8, 11-9, 11-10 through 11-11. Example 11-8 shows the user control used as a "web parts catalog." Examples 11-9, 11-10 through 11-11 show the .aspx and code-behind for a page that demonstrates using the "web parts catalog" user control.

Discussion

Applications that use web parts commonly reuse the same web parts in most or all of the pages of the application. If your application only has a couple of web parts and the list never changes, declaring them in every page where they can be used is not a problem. However, if you have many web parts and new parts are periodically added, this becomes a maintenance problem since every page that uses the web parts would require revision to add the new parts.

The web parts framework provides the ability to address this problem by using a user control to define the web parts available instead of listing them individually in the DeclarativeCatalogPart control. This approach, which allows you to manage the list of available web parts in a single location, significantly reduces the effort required to change the list of available web parts.

The user control that contains the available web parts is created in the same manner as any other user control, but it is used differently. It is never visible to the user, so you do not need to be concerned about control layout. It is used by the DeclarativeCatalogPart control to provide the list of available web parts.

The order in which the controls are defined in the "web part catalog" user control is the same order in which they will be presented to the user.


In our application that implements this solution, we started with the example presented in Recipe 11.1. We added a new user control that contains a declaration of each of the controls to be used as web parts. As in Recipe 11.1, a standard calendar control, the "weather" user control, and the "book list" user control are declared, as shown in Example 11-9.

Next, we remove the <WebPartsTemplate> element and control definitions from the DeclarativeCatalogPart control in the .aspx file. Then, we set the WebPartsListUserControlPath attribute of the DeclarativeCatalogPart control to the name of our "web parts catalog" user control (CH11WebPartCatalogVB.ascx). The complete CatalogZone control for our application is shown below.

 <asp:CatalogZone  runat="server"  EmptyZoneText="No Catalog Items"  HeaderCloseVerb-Visible="false"  Css  Padding="6" > <ZoneTemplate> <asp:PageCatalogPart  runat="server" Title="Previously Closed Controls" /> <asp:DeclarativeCatalogPart  runat="server" Title="Available Parts" WebPartsListUserControlPath="~/CH11WebPartCatalogVB.ascx" > </asp:DeclarativeCatalogPart> </ZoneTemplate> </asp:CatalogZone> 

The "web parts catalog" user control shown in this recipe functions exactly the same as the example in Recipe 11.1 but with the added benefit of reduced maintenance.

See Also

Recipe 11.1

Example 11-8. User control containing available web parts (.ascx)

 <%@ Control Language="VB" ClassName="CH11WebPartCatalogVB" %>  <%@ Register TagPrefix="ASPNetCookbook" TagName="CvilleWeather"  src="/books/1/505/1/html/2/~/CH11CVilleWeatherVB.ascx" %>  <%@ Register TagPrefix="ASPNetCookbook" TagName="BookData"  src="/books/1/505/1/html/2/~/CH11DisplayTabularDataVB.ascx" %> <ASPNetCookbook:CvilleWeather    runat="server"   Title="Weather" /> <asp:Calendar  runat="server"   Title="Calendar" />  <ASPNetCookbook:BookData  runat="server"  Title="Book Data" /> 

Example 11-9. Using a web parts catalog (.aspx)

 <%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master"  AutoEventWireup="false" CodeFile="CH11UsingReusableWebPartCatalogVB.aspx.vb" Inherits="ASPNetCookbook.VBExamples.CH11UsingReusableWebPartCatalogVB" Title="Reusable Web Part Catalog" %> <asp:Content  runat="server" ContentPlaceHolder>  <div align="center" > Reusable Web Part Catalog (VB)  </div>  <asp:WebPartManager  runat="server" Personalization-Enabled="true" />  <table width="90%" align="center" border="1" cellpadding="4" cellspacing="0"> <tr> <td align="right"> <asp:LinkButton  runat="server"     Text="Customize"     Css     OnClick="btnCustomize_Click" />&nbsp;&nbsp; <asp:LinkButton  runat="server"    Text="Reset"    Css    OnClick="btnReset_Click" /> </td> </tr> <tr> <td> <asp:WebPartZone  runat="server"  EmptyZoneText="No Content Selected"  Height="10" HeaderText="Zone 1"  LayoutOrientation="Horizontal"  Css  Padding="6" /> </td> </tr> <tr> <td> <asp:WebPartZone  runat="server"  EmptyZoneText="No Content Selected"  Height="10" HeaderText="Zone 2"  LayoutOrientation="Horizontal"  Css  Padding="6" /> </td> </tr> <tr> <td> <asp:CatalogZone  runat="server"  EmptyZoneText="No Catalog Items"  HeaderCloseVerb-Visible="false"  Css  Padding="6" > <ZoneTemplate>  <asp:PageCatalogPart  runat="server"  Title="Previously Closed Controls" /> <asp:DeclarativeCatalogPart  runat="server"    Title="Available Parts"     WebPartsListUserControlPath="~/CH11WebPartCatalogVB.ascx" >  </asp:DeclarativeCatalogPart> </ZoneTemplate>  </asp:CatalogZone>  </td>  </tr>  </table>  </asp:Content> 

Example 11-10. Using a web parts catalog code-behind (.vb)

 Option Explicit On  Option Strict On Imports System Namespace ASPNetCookbook.VBExamples  ''' <summary>  ''' This class provides the code-behind for  ''' CH11UsingReusableWebPartCatalogVB.aspx  ''' </summary>  Partial Class CH11UsingReusableWebPartCatalogVB Inherits System.Web.UI.Page '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the customize button ''' click event. It is responsible for placing the web part manager ''' in catalog mode. ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Protected Sub btnCustomize_Click(ByVal sender As Object, _  ByVal e As System.EventArgs) wpm1.DisplayMode = WebPartManager.CatalogDisplayMode End Sub 'btnCustomize_Click '''*********************************************************************** ''' <summary> ''' This routine provides the event handler for the reset button ''' click event. It is responsible for resetting the web part manager ''' personalization data. ''' </summary> ''' ''' <param name="sender">Set to the sender of the event</param> ''' <param name="e">Set to the event arguments</param> Protected Sub btnReset_Click(ByVal sender As Object, _  ByVal e As System.EventArgs) wpm1.Personalization.ResetPersonalizationState() End Sub 'btnReset_Click End Class 'CH11UsingReusableWebPartCatalogVB  End Namespace 

Example 11-11. Using a web parts catalog code-behind (.cs)

 using System;  using System.Web.UI.WebControls.WebParts; namespace ASPNetCookbook.CSExamples {  /// <summary>  /// This class provides the code-behind for  /// CH11UsingReusableWebPartCatalogCS.aspx  /// </summary>  public partial class CH11UsingReusableWebPartCatalogCS : System.Web.UI.Page  { ///*********************************************************************** /// <summary> /// This routine provides the event handler for the customize button /// click event. It is responsible for placing the web part manager /// in catalog mode. /// </summary> /// /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void btnCustomize_Click(Object sender,   System.EventArgs e) { wpm1.DisplayMode = WebPartManager.CatalogDisplayMode; } // btnCustomize_Click ///*********************************************************************** /// <summary> /// This routine provides the event handler for the reset button /// click event. It is responsible for resetting the web part manager /// personalization data. /// </summary> /// /// <param name="sender">Set to the sender of the event</param> /// <param name="e">Set to the event arguments</param> protected void btnReset_Click(Object sender,   System.EventArgs e)  {       wpm1.Personalization.ResetPersonalizationState();  } // btnReset_Click  } // CH11UsingReusableWebPartCatalogCS  } 



ASP. NET Cookbook
ASP.Net 2.0 Cookbook (Cookbooks (OReilly))
ISBN: 0596100647
EAN: 2147483647
Year: 2003
Pages: 202

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