Treeview control in SharePoint - doesn't refresh when I update web part properties
I'm trying to create a web part that contains a TreeView control. I've got a Web Part bool property called MyCheckbox and I use this to determine which nodes should appear in the Treeview.
The problem I'm having is that when I modify the property exposed in the Web Part Properties ("Modify Shared Web Part.."), the MyCheckBox bool, and hit 'OK', the Treeview doesn't refresh. However, if I then browse to the page, the treeview is updated.
I am declaring the class as follows, using the Treeview and its root node as member variables:
public class MyWebPart : System.Web.UI.WebControls.WebParts.WebPart
{
private TreeView tree = new TreeView();
private TreeNode rootNode;
[WebBr开发者_StackOverflow中文版owsable(true)]
[Personalizable(PersonalizationScope.Shared)]
public bool MyCheckBox
{
get { return _myCheckBox; }
set { _myCheckBox = value; }
}
private bool __myCheckBox = false;
public MyWebPart()
{
}
public override void RenderControl(HtmlTextWriter writer)
{
tree.RenderControl(writer);
}
protected override void CreateChildControls()
{
rootNode = new TreeNode("ExampleRootNode");
for ( int x = 0; x < 3; x++)
{
TreeNode listNode = new TreeNode(x.ToString());
rootNode.ChildNodes.Add(listNode);
}
if (_myCheckBox)
{
TreeNode listNode = new TreeNode("Final entry");
rootNode.ChildNodes.Add(listNode);
}
tree.Nodes.Add(rootNode);
this.Controls.Add(tree);
}
protected override void Render(HtmlTextWriter writer)
{
base.Render(writer);
}
}
I've tried checking postback, clearing the list, and a million other things. I'm sure I must be missing something!
This is due to the page / control lifecycle of ASP.NET. The writing of properties from the editorpart (the browsable property basically has an on the fly editorpart section created) to the webpart occurs at a later stage than when the CreateChildCOntrols of the webpart is called, namely at the processing of postback events.
As a reference, see this image:
精彩评论