UserControls via LoadControl within a TreeView

Discussion in 'ASP .Net Web Controls' started by Keith Patrick, Dec 5, 2005.

  1. I'm trying to do the old "dynamically create controls via LoadControl don't
    fire events" thing, but whereas normally, I could put some code in the
    viewstate handlers to put the controls back into the control hierarchy
    during initialization, the problem in my case is that my base control is a
    TreeView, and a TreeView cannot have controls in its Controls collection
    (each node contains a usercontrol). Because of that, while I can create the
    controls during LoadViewState just fine, I have no place to put the controls
    (such as the Controls collection of a parent) that would result in the
    events getting wired in. I'm not aware of a way to manually add the event
    handlers (i.e. what does OnInit call that wires in the events), so where
    could I store the usercontrols to allow them to get initialized with rest of
    the control and thus connected to its event handlers?
     
    Keith Patrick, Dec 5, 2005
    #1
    1. Advertising

  2. I found a solution...head-slappingly simple:

    override TreeView.CreateControlCollection() to return a regular
    ControlCollection, and the controls can be added, getting hooked into the
    event system.
     
    Keith Patrick, Dec 7, 2005
    #2
    1. Advertising

  3. OK, I found a solution after overriding a ton of methods in both the tree
    and the node. Basically, I missed one key override:
    CreateControlCollection(), which, by replacing with a standard "new
    ControlCollection(this)", allows me to deposit my controls there so that
    they get hooked into the event system OnInit.
     
    Keith Patrick, Dec 7, 2005
    #3
    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. JX
    Replies:
    1
    Views:
    318
  2. Stijn Vrancken via .NET 247

    Events not firing when loading multiple usercontrols with LoadControl

    Stijn Vrancken via .NET 247, Mar 26, 2005, in forum: ASP .Net Building Controls
    Replies:
    0
    Views:
    411
    Stijn Vrancken via .NET 247
    Mar 26, 2005
  3. Deane
    Replies:
    0
    Views:
    887
    Deane
    Sep 3, 2007
  4. Anthony Williams

    Problems with UserControls within a DataGrid

    Anthony Williams, Sep 18, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    3
    Views:
    153
    Anthony Williams
    Sep 22, 2003
  5. Nestor

    LoadControl and Multiple IDs in UserControls Error

    Nestor, Nov 10, 2005, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    204
    .net noobie
    Nov 12, 2005
Loading...

Share This Page