.NET Framework - Using C# Interfaces and User Controls

Asked By csmit on 25-Oct-07 07:29 PM
Hi everyone

I am trying to create a .net web application, and I am having trouble
with an aspect of using user controls...

I want to be able to create a generic method of calling a number of
user controls, based on a variable that is passed to a parent form..
the folling is an excerpt of my code

ReportName = Request.QueryString["name"];
ReportPath = ReportName+".ascx";
Cert.Forms.InterfaceReport CReport;

CReport = (Cert.Forms.InterfaceReport) LoadControl(ReportPath);

where the report class name is passed to the form in the Request
object, and is retrieved using the QueryString method.  i then pass
some other Request information to the user control class instance that
is opened.  doing some testing has revealed that the 2nd line of code
is working correctly.  the problem comes however, when i try to add
the user control to a panel (called divContainer) on the parent form.
the error i am getting is shown below..

Argument '1': cannot convert from 'Cert.Forms.InterfaceReport' to

If anyone has any ideas, they would be greatly appreciated....

Peter Duniho replied on 25-Oct-07 08:23 PM
It seems to me that the error is fairly clear: you have something that
is a Cert.Forms.InterfaceReport, and you are trying to pass that to a
method that takes a System.Web.UI.Control.

I don't know anything about the Cert.Forms.InterfaceReport type (looks
like some custom type in your own namespace), but unless it inherits
System.Web.UI.Control somehow, your code just can't work.

If you want to create a control on the fly and add it to divContainer,
your control class will need to inherit System.Web.UI.Control.

csmit replied on 25-Oct-07 08:48 PM
te problem is that i am unable to inherit the control class in my
interface, because that is not allowed by vis. studio...
Peter Duniho replied on 25-Oct-07 08:59 PM
You haven't posted enough code for us to understand why that is the
case.  I think it's possible you're mistaken about that.  But if it's
true, then you simply cannot do what you want.

It doesn't really matter _why_ your class doesn't inherit the necessary
class; if it doesn't, you can't use an instance of that class as a
parameter to the ControlCollection.Add() method.

Liz replied on 25-Oct-07 09:04 PM
what is this CReport type?  something like a Crystal Report type?  perhaps
you have to add a container which inherits from System.Web.UI.Controls
first, and THEN add your report type to THAT container ... just a guess;
there's not enough information here to provide an answer
Peter Duniho replied on 25-Oct-07 09:11 PM
By the way, a couple of examples of how to work around the lack of
inheritance in an interface:

* Use an abstract class instead.  An abstract class can inherit
some other class, providing you the inheritance required to make your
code work.

* Define a "ToClass()" method or "Class" property in the interface.
Implementors would be required to implement the necessary behavior to
convert the interface instance to something appropriate (for example,
simply cast the interface implementor instance to the necessary class).
You wouldn't get compile-time type safety though.

Finally, if for some reason you don't really want the interface to
require the implementor to inherit from the necessary class
(Web.UI.Control in this case), but you believe that at that point in
code you have good reason to believe that the implementor does in fact
inherit from the necessary class, you can simply cast the interface
reference to the necessary class.  As long as the instance actually is
of the appropriate type, it should work.

Mr. Arnold replied on 26-Oct-07 03:32 AM
It's not allowed by .Net System.Web.UI.Control not VS.

It looks like you need a Base Page that has System.Web.UI.Control and this
Cert.Forms thing defined as Interfaces.

You derive your Web page off of the Base page so that both Interfaces can be

I don't know if you can just have your aspx Code Behind file to just have
two interfaces without using a Base Page. You can try it.

public partial class SomeClass : System.Web.UI.Control,

You also may need to post msnews.microsoft.com newsgroup for ASP.Net or .Net