Manipulating Web Parts in ASP.Net 2.0 - The Web Part Catalog
(Page 3 of 4 )
The driving factors behind web parts are personalization and customization. With that in mind, there are going to be two very common scenarios with pages containing web parts:
You have ten available web parts, but only five are suitable for general consumption. The other five are more specialized, so you leave them hidden by default, so that people only have to add them if they decide they’re relevant.
People ‘close’ a web part or two. Then later, they decide they’d like to have them both.
Microsoft has made it very simple to take care of these scenarios from the developer’s perspective as well as the end user. They created the concept of the ‘Catalog Zone’, where information about all available web parts – including ones closed by the users – is stored. At run time, a user can simply click ‘Personalize this Page > Add Web Parts To This Page’ to bring up the catalog.
Of course we can’t get away with no code whatsoever, but it’s pretty minimal nonetheless. Here is all the code necessary to enable the functionality I just mentioned:
<asp:CatalogZone runat=”server” HeaderText=”Customize this page”>
<ZoneTemplate>
<asp:PageCatalogPart runat=”server”
title=” your parts” />
<asp:declarativeCatalogPart runat=”server”
title=”also available”>
<webpartstemplate>
<jc:mathPart runat=”server” id=”otherPart1” />
<jc:dogPart runat=”server” id=”otherPart2” />
</webpartstemplate>
</asp:declarativeCatalogPart>
</ZoneTemplate>
</asp:CatalogZone>
So the PagePartCatalog is the placeholder for any default web parts that have been removed, or ‘closed’, by the user. The DeclarativeCatalogPart is where you manually specify any additionally available web parts, which will be instantiated only after a user adds them to the page.
And of course, this would not be a full Microsoft offering if we weren’t able to customize the rendering to our liking. Here are the available display properties to modify:
CatalogItemStyle
HeaderStyle
HeaderVerbStyle
InstructionStyle
PartLinkStyle
VerbStyle
So go ahead, set up a catalog, modify its display, Microsoft has made it incredibly easy to do so. The fun part comes now, as we try to communicate data between separate web parts.
Next: Web Part Connections >>
More ASP.NET Articles
More By Justin Cook