Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

Loading Web Parts into a catalog at run time


View products that this article applies to.

ASP.NET Support Voice Column

Loading Web Parts into a catalog at run time

To customize this column to your needs, we want to invite you to submit your ideas about topics that interest you and issues that you want to see addressed in future Knowledge Base articles and Support Voice columns. You can submit your ideas and feedback using the Ask For It (http://support.microsoft.com/common/survey.aspx?scid=sw;en;1176&p0=&p1=&p2=&p3=&p4=) form. There's also a link to the form at the bottom of this column.

↑ Back to the top


Introduction

Welcome to the ASP.NET Support Voice column! I am Jerry Orman. I have been with Microsoft for over five years and have spent most of my time focused on Web-related technologies such as Microsoft FrontPage and Microsoft SharePoint Products and Technologies. I've spent the past year working with Microsoft ASP.NET as a support engineer.

ASP.NET 2.0 introduces the Web Part framework outside of the SharePoint environment. This allows developers to build applications that are configurable by the user at run time. The CatalogPart class is used to provide a list of Web Parts to users at run time. Out of the box, ASP.NET 2.0 does not provide a catalog that is populated at run time. This Support Voice column will discuss how to create a custom catalog for this purpose.

↑ Back to the top


More information

Out of the box, ASP.NET 2.0 provides the following catalogs: 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: 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:
  1. Open the CustomParts Web site as a file-based project in Microsoft Visual Studio 2005.
  2. 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.
  3. Expand CustomParts Web Site.
  4. Double-click the Web.config file.
  5. Change the connection string to point to the instance of Microsoft SQL Server hosting the Northwind database.
  6. Browse the Default.aspx file.
  7. Click Login.
  8. Log in by typing BobSmith and the password BobSmith!.
  9. 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.

↑ Back to the top


Keywords: KB910446, kbasp, kbhowto

↑ Back to the top

Article Info
Article ID : 910446
Revision : 4
Created on : 3/16/2007
Published on : 3/16/2007
Exists online : False
Views : 428