UserControls via LoadControl within a TreeView

K

Keith Patrick

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?
 
K

Keith Patrick

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.
 
K

Keith Patrick

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.
 

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,768
Messages
2,569,574
Members
45,049
Latest member
Allen00Reed

Latest Threads

Top