Expanding treeview

1 posts, 0 answers
  1. efter
    efter avatar
    3 posts
    Member since:
    Nov 2008

    Posted 05 Feb 2009 Link to this post

    I have a treeview using ServerSideCallBack as its expand mode. My child nodes are url's which reloads the page with the relevant content for that child node. On the reload of the page i want the treeview to expand the parent node of the selected child and select that child.

    The problem I'm having is expanding the parent node properly on the reload of the page. I've read around the forums/documentation and see that it can only be achieved client-side so wrote a script based on some i'd seen on the telerik site. The child nodes are built and the script executes, however it doesn't seem to be able to find the treeview object, so obviously doesn't expand properly - the '+' button still appears instead of '-' but the child nodes appear making it 'look' like it has expanded but not having the expected funtionality when the user then clicks the button again. I think its a timing issue of the script executing before the object is created, or I'm doing somthing wrong of course, either way I haven't yet been able to resolve it. Any advice would be gratefully received, here's what i'm currently doing:


    protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadRootNodes();
                if (this.SelectedChildNode > 0)
                {
                    PreselectNode();
                }

            }
        }
        
    private void LoadRootNodes()
        {
            List<MyRootObject> _rootNodes = this.MyRootNodesList;
            
            foreach (MyRootObject _rootNode in _rootNodes)
                {
                    RadTreeNode _node = new RadTreeNode();
                    node.Text = _rootNode.Title;
                    node.Value = _rootNode.ID.ToString();
                    node.ExpandMode = TreeNodeExpandMode.ServerSideCallBack;
                    treeViewID.Nodes.Add(_node);
                }
            }
        }
        
    private void AddChildNodes(RadTreeNodeCollection treeNodes, int parentNodeId)
        {
            List<MyRootObject> _rootNodes = this.MyRootNodes;
            
            foreach (MyRootObject _rootNode in _rootNodes)
            {
                if (_rootNodes.Exists(delegate(MyRootObject obj) { return obj.ID == parentNodeId; }))
                {
                    List<MyChildObject> _childNodes = GetChildNodesList(parentNodeId);

                    foreach (MyChildObject _childNode in _childNodes)
                    {
                        RadTreeNode node = new RadTreeNode();
                        node.Text = _childNode.Title;
                        node.Value = _childNode.ID.ToString();
                        node.NavigateUrl = _childNode.Url;
                        treeNodes.Add(node);
                    }
                }
            }
        }
        
    private void PreselectNode()
        {
            int _parentNodeId = GetParentNodeId(this.SelectedChildNode);
            
            RadTreeNode _parentNode = treeViewID.Nodes.FindNodeByValue(_parentNodeId);
            AddChildNodes(_parentNode.Nodes, _parentNodeId);
            
            RadTreeNode _childNode = parentNode.Nodes.FindNodeByValue(this.SelectedChildNode);
            _childNode.Selected = true;
            
            string script = "var tree = $find('" + treeViewID.ClientID + "'); var expandNode = tree.get_nodes().getNode(1); expandNode.expand(); ";
            ScriptManager.RegisterClientScriptBlock(Page, this.GetType(), "ExpandScript", script, true);
        }
Back to Top