I have a somewhat complex custom user control, created in C# .NET, which is essentially a tree structure. The control , ctlGroup, contains a list of fields (which are also custom user controls - ctlFields) and a list of child groups. The datalist containing the child groups creates new instances of the same ctlGroup control, because each of the children can have a list of fields and a list of children, and so on. Whew.
The number of children each group can have is finite, and each child group is an entry in a database, so infinite recursion is not possible. The controls are created fine, and displayed fine, until a post-back occurs. I have been able to persist all the data pertaining to each dynamically created control pretty well on post back, with one exception. The part I am having trouble with is maintaining the visibility of certain panels on the controls through postback. Each child group has a button that expands and collapses part of the body. I can collapse this portion once, but as soon as I click the button again (i.e. cause a postback), the control is persisted and it's previous visibility setting is lost.
I seem to be struggling with coming up with a way to store this information. I tried saving the state of the visibility in the viewstate, but it seems this information does not carry over when the control is recreated.
Does anyone have any ideas for how to do this? Am I missing something?
edit: I should clarify that the ctlGroup controls that are created in the datalist are created dynamically because they are recursive calls. I don't know any other way to call a control from itself, but if t here is a better way, I would love to hear it.