Adding Web Parts


Maybe you've tested the example as it's been presented thus far in this chapter on your PC. Did you check what happens when you click the Close link? Well, the Web Part is closed and disappears from the page. How can it be shown again? So far not at all! The example can be enhanced to allow you do so, however.

Adding Web Parts Formerly Removed from the Page

If you want to allow users to add Web Parts, you'll first need a new zone, CatalogZone. This zone will only be shown if the WebPartManager control is in catalog mode. I've integrated a corresponding zone into the empty table cell on the example page on the lower-right side.

This zone control has a ZoneTemplate as well that holds the elements to be shown. They must derive (at least indirectly) from the CatalogBasePart class. Regular controls or Web Parts aren't allowed. One example of a control that is allowed is PageCatalogPart, which I've placed in the example. With the help of this control, you can add all the Web Parts to your page that you have removed from it earlier.

For the sake of a correctly functioning example, you need—apart from a new zone including PageCatalogPart—a second button that switches the WebPartManager control into catalog mode. The following event handling is being allocated to the LinkButton control:

 void LB_AddWebParts_Click(object sender, System.EventArgs e) {     WebPartManager manager = (this.Items[typeof(WebPartManager)]         as WebPartManager);     if (manager.DisplayMode != WebPartDisplayMode.Catalog)     {         manager.SetDisplayMode(WebPartDisplayMode.Catalog);         this.LB_Personalize.Text = "End Adding";     }     else     {         manager.SetDisplayMode(WebPartDisplayMode.Normal);         this.LB_Personalize.Text = "Add web parts";     } } 

In Figure 8-8, you can see the example in action. First I removed the welcome text from the page, and then I switched to catalog mode. Now the mentioned zone is shown. In the Page Catalog section, you can see the removed Web Part; if you were to check the check box beside it, choose a zone, and click the Add button, the text would be shown again.

click to expand
Figure 8-8: Add any removed web parts using the PageCatalogPart Control.

Adding New Web Parts

Apart from adding previously removed Web Parts, CatalogZone has a second role to fulfill: the insertion of new Web Parts. In the first step, you have to add a TemplateCatalogPart control to the existing CatalogZone. This control works similarly to the previously described WebPartZone and can take on Web Parts as well as any other controls. You can insert several TemplateCatalogPart controls within the CatalogZone and define content groups. The user can access a specific group through the LinkButton control and obtain a list of available entries afterward in the form of a CheckBoxList control.

On first view, the nesting of the control is sort of confusing. I hope that the following overview will make it more comprehensible for you:

 CatalogZone     TemplateCatalogPart         Web Part 1             Content         Web Part 2         Web Part 3 

I've enhanced the example accordingly and added a TemplateCatalogPart titled "Money" as well as three ContentWebPart controls (see Listing 8-3). These have been titled according to three indexes—Dow Jones Industrial Average, S&P 500, and NASDAQ Composite—and could show the actual price. As you can see in this example and in Figure 8-9, however, functionality has not been integrated.

click to expand
Figure 8-9: Just add all the Web Parts your users can select.

Listing 8-3: Adding Previously Removed Web Parts Using PageCatalogPart

start example
 <asp:catalogzone      title="Catalog Zone" runat="server" width="100%">     <zonetemplate>         <asp:pagecatalogpart  runat="server">         </asp:pagecatalogpart>         <asp:templatecatalogpart              title="Money" runat="server" width="100%">             <webpartstemplate>                 <asp:contentwebpart                      title="DOW" runat="server" width="100%"                     caption="DOW">                     <contenttemplate>                     DOW                 </contenttemplate>             </asp:contentwebpart>             <asp:contentwebpart                  title="S&P" runat="server" width="100%"                 caption="S&P">                 <contenttemplate>                     S&P                 </contenttemplate>             </asp:contentwebpart>             <asp:contentwebpart                  title="NASDAQ" runat="server" width="100%"                 caption="NASDAQ">                 <contenttemplate>                     NASDAQ                 </contenttemplate>             </asp:contentwebpart>             </webpartstemplate>         </asp:templatecatalogpart>     </zonetemplate> </asp:catalogzone> 
end example

Caution

Please be aware that Web Parts defined within a TemplateCatalogPart control sometimes won't be correctly added to the page or even shown without content in the current version. This unattractive behavior will, of course, be solved in the Beta version.

Tip

You can spread the whole catalog, including the content to be shown within a user control. This way you can allow your users to add the same content to each personalized page without additional work.




ASP. NET 2.0 Revealed
ASP.NET 2.0 Revealed
ISBN: 1590593375
EAN: 2147483647
Year: 2005
Pages: 133

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