In order to customize the aspect of some nodes in a TreeView control, I've created a custom control derived from the TreeNode class, which overrides the RenderPreText and RenderPostText methods.

This works fine... until the user clicks on anything in the page. As soon as a postback is made, all TreeNodes lose their custom aspect.

I've inspected the code with the debugger, and I've discovered that all the TreeNodes are being created as MyCustomTreeNode objects, but after the postback they just become standard TreeNodes.

What's happening here?!?

有帮助吗?

解决方案

Answer found, as documented here: http://support.microsoft.com/kb/2527108.

Creating a custom control derived from TreeNode is not enough, two other steps are required:

  • Enable the control to save its additional properties in the viewstate.
  • Create another custom control derived from TreeView, which knows how to recreate those custom TreeNodes when a postback happens.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top