loading user controls dynamically

N

Nick Stansbury

Hi,
Apologies for what are probably pretty basic questions - and my convoluted
explanation. Built some simple user controls, that represent the UI side for
a whole bunch of Event objects (in the form of real everyday events likes
conferences, seminars etc.). So I have the following:

Class EventBase
EventTitle as string
etc.
End Class
Class EventSummary : Inherits EventBase
EventDate as dataTime
etc.
End Class
class EventDetails : Inherits EventBase
LongDescription as string
End class

And user controls CtlEventSummary.ascx & CtlEventDetail.ascx , each of
which have a BoundEvent as EventBase property and a BindEventDetails Method.
Seemed like a great idea, until I ran into some pretty simple problems:

I tried to go (summary):

For each CurrentEvent as EventBase in Session("CurrentEventList") 'stored
list of events to be displayed
'for a summary object
CurrentControl = new CtlEventSummary
CurrentControl.EventTitle = CurrentEvent.EventTitle
'etc.
PlaceHolderEventList.Controls.Add(CurrentControl)
CurrentControl.BindEventDetails() 'which basically does this :
Me.LblEventTitle = Me.EventTitle
'...
next

But I am getting an object not set error - because the Labels in the custom
control haven't been initialised yet. I could use the LoadControl() method -
but I don't really understand what it does or what I should have to use it.

In addition can someone help me out with how I deal with @Register
directives when I am using code-behind for all pages? If I end up having to
use LoadControl() how do I register the user control on the .vb page? Do I
need to?

Many thanks

Nick Stansbury
 
J

John Saunders

Nick Stansbury said:
Hi,
Apologies for what are probably pretty basic questions - and my convoluted
explanation. Built some simple user controls, that represent the UI side for
a whole bunch of Event objects (in the form of real everyday events likes
conferences, seminars etc.). So I have the following:

Class EventBase
EventTitle as string
etc.
End Class
Class EventSummary : Inherits EventBase
EventDate as dataTime
etc.
End Class
class EventDetails : Inherits EventBase
LongDescription as string
End class

And user controls CtlEventSummary.ascx & CtlEventDetail.ascx , each of
which have a BoundEvent as EventBase property and a BindEventDetails Method.
Seemed like a great idea, until I ran into some pretty simple problems:

I tried to go (summary):

For each CurrentEvent as EventBase in Session("CurrentEventList") 'stored
list of events to be displayed
'for a summary object
CurrentControl = new CtlEventSummary
CurrentControl.EventTitle = CurrentEvent.EventTitle
'etc.
PlaceHolderEventList.Controls.Add(CurrentControl)
CurrentControl.BindEventDetails() 'which basically does this :
Me.LblEventTitle = Me.EventTitle
'...
next

You need to use LoadControl to load a user control. This is because the HTML
in the user control needs to be compiled. In particular, "CtlEventSummary"
is just the codebehind. There is a generated class, something like
ASP._CtlEventSummary, which inherits from CtlEventSummary, and which is only
created when the user control is compiled. LoadControl will ensure that this
happens.
 

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

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top