Suppose I have a treeview, where each treenode contains an id for a different set of user controls. When the user clicks a node, these controls should be loaded to the page. As I understand the ASP page life cycle, dynamic controls should be added in the initialization stage, and postback events will fire later on.
So if the treeview click event happens after I need to add my controls, how do I dynamically add controls based on user postback events?
Edit: I tried the suggestion from ArronLS:
What I did was add the node value to the session array, and use that when I do the init to choose which form elements to load to the controls of a placeholder control. On the treeview click event, I update the node in the session array, clear the old form elements in the placeholder, and add the new form elements to the controls. When the page is loaded again, it should now find the node at init time, so viewstate problems would be circumvented.
Now I haven't fully tested this yet, but there was another similar post that talks about the problems that might result with the viewstate. They suggest a solution that polls the Request[] part of the context (in their case the dropbox) within the Init control, manually handling some of the postback functionality.
My new question is how to I access the selected node in the treeview using the Request array?
This may not be an answer to your direct question, but since I've never found an answer myself, here's the workaround I've used.
The approach I've always used when working with a TreeView is to declare the controls in the aspx page once, and then on the click event, bind the controls the the data based on the id. If necessary, you can initially set the visibility to visible="false" and change it when bound. This approach works nicely because it avoids just the conundrum you're describing.
If you're not opposed to giving up on treeviews, a nested repeater approach works well, too.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With