Out of the box, ASP.NET 2.0 provides the following catalogs:
- DeclarativeCatalogPart
This class allows you to declaratively add the Web Parts to
the .aspx page at design time. You can also set the WebPartsListUserControlPath property and point to a user control (.ascx) that has Web Parts
declaratively added at design time. For more information, visit the following
Microsoft Developer Network (MSDN) Web site: - PageCatalogPart
This class displays Web Parts that were closed by a user. For
more information, visit the following MSDN Web site: - ImportCatalogPart
This class allows you to import a Web Part that you have
exported. Exporting a Web Part allows you to preserve the settings, which is
useful for highly customized Web Parts. For more information, visit the
following MSDN Web site:
So, as you can see, there is nothing out of the box that will
allow you to drop DLLs into a folder and have them get picked up as Web Parts
at run time. In order to do this, you need to create a custom
CatalogPart class and override the following methods:
- GetAvailableWebPartDescriptions
This method returns the Web Part descriptions used to
populate the catalog in the CatalogZone. In this sample, we'll use reflection in this method to load the
DLLs and determine if they contain Web Parts. For more information, visit the
following MSDN Web site: - GetWebPart
This method returns the actual Web Parts to be rendered in
the page. If you don't handle populating the collection that returns the Web
Part correctly, your catalog will render and display the descriptions, but
adding a Web Part to a WebPartZone will not do anything. For more information, visit the following
MSDN Web site:
The following sample demonstrates how to build a custom catalog
that uses reflection to populate the list of Web Parts at run time. This sample
is provided as-is and is meant for demonstration purposes only.
The following
file is available for download from the Microsoft Download
Center:
Download
the CustomCatalogSample.exe package
now. For more information about how to
download Microsoft support files, click the following article number to view
the article in the Microsoft Knowledge Base:
119591�
How to obtain Microsoft support files from online services
Microsoft scanned this file for viruses. Microsoft used the most
current virus-detection software that was available on the date that the file
was posted. The file is stored on security-enhanced servers that help prevent
any unauthorized changes to the file.
We'll walk through the sample projects so you
can get an idea of what you're getting into. The code is fully commented, which
should make for some great reading.
The sample includes three
projects:
- MyControls class project
- ReflectionCatalogPart
This is the class that does the work for the
catalog.
- SampleWebParts class project
- RSSPart.cs
This is a Web Part I wrote that will go
out and pull data from an RSS2 feed. You could flesh this out more to add
different templates and caching. - FeaturedProductPart.cs, ICategoryInterface.cs, and
SelectCategory.cs
These are from the following MSDN article: I included them here to flesh out the list of Web
Parts.
- CustomParts Web site
- Default.aspx
This file drives the sample. It has
all the various catalogs and parts already declared on the page. MyControls
- CatalogTemplate.ascx
This file is used in the WebPartsListUserControlPath property of the DeclarativeCatalogPart class to demonstrate the use of that property. - ReportGenerator.ascx
This file has a GridView control to show using an .ascx file as a Web Part. (Any control
that is not a Web Part is wrapped in the GenericWebPart class when it is placed in a WebPartZone.)
To use the sample, follow these steps:
- Open the CustomParts Web site as a file-based project in
Microsoft Visual Studio 2005.
- Add the MyControls class project and the SampleWebParts
class project as projects to the solution. To do this, click
File, click Add, and then click
Existing Project.
- Expand CustomParts Web Site.
- Double-click the Web.config file.
- Change the connection string to point to the instance of
Microsoft SQL Server hosting the Northwind database.
- Browse the Default.aspx file.
- Click Login.
- Log in by typing BobSmith and the
password BobSmith!.
- On the Modify Web Parts menu, click
Catalog.
You'll have two catalogs, the Reflection Parts catalog and the Declarative Parts catalog.
You can view the ReflectionCatalogPart.cs file in the
MyControls class and the
asp:CatalogZone section in the Default.aspx file to see how the custom catalog
works. In general, it checks to determine if there is an
HttpContext and then uses reflection to populate and cache the Web Parts that
are available in the /bin folder. You can add Web Parts by adding the DLL to
the /bin folder. This will trigger an
AppDomain recycle, which will clear the cache and allow the catalog to
repopulate it on the next load.
Now that you have a sample
demonstrating how to populate the catalog using reflection, you can leverage
this to build catalogs that may better suit your needs.