loading user controls dynamically

Discussion in 'ASP .Net' started by Nick Stansbury, Jul 19, 2004.

  1. 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
     
    Nick Stansbury, Jul 19, 2004
    #1
    1. Advertising

  2. "Nick Stansbury" <> wrote in message
    news:...
    > 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.
    --
    John Saunders
    johnwsaundersiii at hotmail
     
    John Saunders, Jul 19, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Miller [msft]
    Replies:
    0
    Views:
    598
    Ben Miller [msft]
    Aug 5, 2003
  2. sonic_soul
    Replies:
    0
    Views:
    519
    sonic_soul
    Aug 14, 2003
  3. Johnny Fugazzi

    Dynamically loading user controls

    Johnny Fugazzi, Nov 24, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    410
    Johnny Fugazzi
    Nov 24, 2004
  4. =?Utf-8?B?V291dGVy?=

    Dynamically Loaded controls loading other controls

    =?Utf-8?B?V291dGVy?=, Feb 27, 2006, in forum: ASP .Net
    Replies:
    3
    Views:
    501
    =?Utf-8?B?V291dGVy?=
    Feb 27, 2006
  5. Bob Rock
    Replies:
    1
    Views:
    643
    Cowboy \(Gregory A. Beamer\)
    Sep 17, 2006
Loading...

Share This Page