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 a开发者_运维问答nd 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.
My suggestion:
Store the ID's of "expanded" options in a hidden field. They could recursively have links to expanded children as well. This would be your "expanded-nodes" tree structure. In JSON it would look like this (stored as a string in the hidden field):
[{
id: "root1",
children: [{id: "sub-root1", children: []}, {id: "sub-root2", children: []}]
},
{
id: "root2",
children: []
}]
Then on postback, attach a handler on the document.load event ($addHandler(document, "load", function (){ ... }
) that parses your hidden field's JSON. When the iterator visits an expanded node, it "clicks" the actual expand button (call .click()
on the found element). This would re-build the "expanded" structure.
HTH
精彩评论