how to use base classes for user controls?

D

darrel

This is a follow-up to a question I asked yesterday.

I'm loading a UC programatically as such:

===================================================
public customContentControl as UserControl
customContentControl =
CType(Page.LoadControl("mycontrol.ascx"),UserControl)panel_controlHolder.Con
trols.Add(customContentControl)
===================================================

This works fine, but got hung up on how to now pass a property or variable
value to this loaded control.

It was suggested to use a base class or an interface. I've done a bit with
interfaces, but wanted to try understanding how the base class option would
be imnplemented.

What I eventually would like to be able to do is pass a value like this:

===================================================
customContentControl.property = 'whatever'
===================================================

Now, from what I understand, my problem is that I'm casting the loaded UC as
a 'UserControl' instead of a base-class derived from the UserControl class.
Am I on the right path with that logic? If so, what does the base-class
definition do. Is it merely creating a namespace so I can access it?
 
P

Patrice

This is the other way round.

When you create a user control, you are creating a class that inherits from
UserControl (ie. UserControl is your control base class).

As when you load your control, you use a UserControl variable, you are only
able to access to what UserControl does (and your control is able to perform
all this as it inherits these capabilities from the UserControl (base)
class).

If you load your control using a MyControl variable, you'll be able to
access all the capabilites included those you added (change also the CType).

Patrice
 
D

darrel

If you load your control using a MyControl variable, you'll be able to
access all the capabilites included those you added (change also the
CType).

Your explanation helps. Thanks! I'm still not quite sure *what* I need to
change the CType to, though. Is it the class of the UC I am loading?

-Darrel
 
M

Matt Berther

Hello darrel,

I posted an example of how to accomplish what you need in my answer to your
previous post. Did you have additional questions?
 
D

darrel

I posted an example of how to accomplish what you need in my answer to
your
previous post. Did you have additional questions?

I'm taking a second look at your example. It's in C#, so it's a bit foreign
to me, but from what I can tell, you are creating a class based on the
System.Web.UI.UserControl namespace and then, within that class, setting
some properties. That appears to be the same thing as using an interface. Is
that correct?

So based on that, would all of my controls that I want to dynamically load
then be based on this particular base class? And then, instead of loading
these as usercontrols, instead load them as usercontrol.myBaseClass?

-Darrel
 
J

jhcorey

I imagine you've figured it out by now, but here's what worked for me:

I created a base class:
Public Class BaseReportControl
Inherits System.Web.UI.UserControl

with properties and Overridable subs and functions that I might be
using in
the inherited controls.

Then to load one of the controls, I used:
Dim ctlReport As BaseReportControl
ctlReport = CType(LoadControl(SubReport), BaseReportControl)

where SubReport is a string like "ctl.ascx"

It's important to set the ID property if you're going to want to have
controls reloaded and functioning on postback. The point is to make
sure
a control has the same (arbitrary) value for ID every time.
 
M

Matt Berther

Hello darrel,

Yes... an interface would work as well.

When you load them, you can do something like this:

[C#]
IMyControlInterface instance = (IMyControlInterface)Page.LoadControl("somecontrol.ascx");

[VB]
Dim instance As IMyControlInterface
instance = CType(Page.LoadControl("somecontrol.ascx"), IMyControlInterface)

My VB.NET knowledge is non-existant, so the above may not be syntactically
correct, but you should get the idea.
 
D

darrel

I imagine you've figured it out by now, but here's what worked for me:
I created a base class:
Public Class BaseReportControl
Inherits System.Web.UI.UserControl

with properties and Overridable subs and functions that I might be
using in
the inherited controls.

Then to load one of the controls, I used:
Dim ctlReport As BaseReportControl
ctlReport = CType(LoadControl(SubReport), BaseReportControl)

where SubReport is a string like "ctl.ascx"

It's important to set the ID property if you're going to want to have
controls reloaded and functioning on postback. The point is to make
sure
a control has the same (arbitrary) value for ID every time.

Thanks! That helps! I think I got this working. I'll likely post a follow-up
question tomorrow.

-Darrel
 
D

darrel

Yes... an interface would work as well.

Thanks for all the help, Matt. I think I got things working using the
base-class concept. I'll likely be posting a followup in the next day or two
;o)

-Darrel
 

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

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top