At work I'm building a new report system that's based off a project I did when I was first learning ASP.NET. The main report page allows you to select a type of report and the report criteria. Based on what you select I want to be able to display different UserControls with the report results.
Old ASP.Net method:
I created one UserControl that contains the report selection criteria with public properties that I can use to know what type of report was selected. Then on the reports.aspx page I added that control, a button for the user to click on to generate the report and a PlaceHolder control that would contain the UserControls that would display the report.
Once the user clicked on the button I would query the properties on the report selection UserControl and add the appropriate report UserControls to the placeholder using Page.LoadControl.
New ASP.Net 2.0 method:
There have been some changes in how ASP.Net 2.0 compiles UserControls. You can no longer declare a variable with the class of a UserControl unless you use a @Register or @Reference directive or have an interface or base class that the UserControl inherits. (See
ASP.NET 2.0 and VS 2005: You win some, you lose some by
Rick Strahl the section on ASP.NET page compilation)
My new project could potentially have a large number of UserControls that would be added to the PlaceHolder control. To make matters worse the code that actually adds the UserControl to the PlaceHolder is not in an aspx page or a UserControl, it is in a class. So I'm unable to use @Register or @Reference directives to allow myself to have a strongly typed variable for the UserControl I want to add.
One thing I do have going for me is the interface that the different UserControls have. They are almost identical and with a little tweaking the UserControls can all have the same interface. So I created a new class in the App_Code directory called "ReportItem" that inherits System.Web.UI.UserControl. Then I changed the inheritence on all my Report User controls from UserControl to "ReportItem"
Works like a charm. Now I declare a varible as ReportItem and I can use Page.LoadControl to load the control. I can set all the properties I need with the ReportItem variable then add it to the PlaceHolder.