Using C# Interfaces and User Controls

C

csmith

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);
CReport.LoadRequestData(Request);
divContainer.Controls.Add(CReport);


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
'System.Web.UI.Control'

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

Peter Duniho

[...]
divContainer.Controls.Add(CReport);

[...] 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
'System.Web.UI.Control'

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

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.

Pete
 
C

csmith

[...]
divContainer.Controls.Add(CReport);
[...] 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
'System.Web.UI.Control'
If anyone has any ideas, they would be greatly appreciated....

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.

te problem is that i am unable to inherit the control class in my
interface, because that is not allowed by vis. studio...
 
P

Peter Duniho

te problem is that i am unable to inherit the control class in my
interface, because that is not allowed by vis. studio...

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.

Pete
 
L

Liz

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


[...]
divContainer.Controls.Add(CReport);
[...] 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
'System.Web.UI.Control'
If anyone has any ideas, they would be greatly appreciated....

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.

te problem is that i am unable to inherit the control class in my
interface, because that is not allowed by vis. studio...
 
P

Peter Duniho

te problem is that i am unable to inherit the control class in my
interface, because that is not allowed by vis. studio...

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.

Pete
 
M

Mr. Arnold

[...]
divContainer.Controls.Add(CReport);
[...] 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
'System.Web.UI.Control'
If anyone has any ideas, they would be greatly appreciated....

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.

te problem is that i am unable to inherit the control class in my
interface, because that is not allowed by vis. studio...

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
used.

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,
Cert.Forms.InterfaceReport

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top