By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,835 Members | 1,454 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,835 IT Pros & Developers. It's quick & easy.

how to use base classes for user controls?

P: n/a
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"),UserContr ol)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?
Nov 19 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
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

--

"darrel" <no*****@hotmail.com> a écrit dans le message de
news:ut*************@tk2msftngp13.phx.gbl...
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"),UserContr ol)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?

Nov 19 '05 #2

P: n/a
> 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
Nov 19 '05 #3

P: n/a
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?

--
Matt Berther
http://www.mattberther.com
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


Nov 19 '05 #4

P: n/a
> 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
Nov 19 '05 #5

P: n/a
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.

darrel wrote:
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


Nov 19 '05 #6

P: n/a
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.

--
Matt Berther
http://www.mattberther.com
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


Nov 19 '05 #7

P: n/a
> 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
Nov 19 '05 #8

P: n/a
> 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
Nov 19 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.