TreeView保存当前节点展开状态【Draft|TreeView保存当前节点展开状态【Draft Saved】

前台没有什么而设置,主要代码实现都是在后台。包括加载节点数据,保存当前节点Parent路径状态,设置当前节点展开状态。
Form_Load中:首先获取到保存的state状态,去遍历treeview所有节点,匹配。如果匹配成功,设置该节点Expended属性为true。
【TreeView保存当前节点展开状态【Draft|TreeView保存当前节点展开状态【Draft Saved】】【图中Highlight部分,换成递归遍历更好,暂时没有实现。】

protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindHowNode(); BindReportNode(); BindDocNode(); BindLinksNode(); } if (Request.QueryString["state"] != null) { string state = Request.QueryString["state"].ToString(); foreach (TreeNode tn in tvAcc.Nodes) { if (tn.Text == state) { tn.Expanded = true; }foreach (TreeNode tn1 in tn.ChildNodes) { if (tn1.Text == state) { tn1.Expanded = true; }foreach (TreeNode tn2 in tn1.ChildNodes) { if (tn2.Text == state) { tn2.Expanded = true; } } } } } }


后台动态加载TreeView数据:
private void BindHowNode() { TreeNode rootHow = new TreeNode("How We Work", "How We Work", "", "#", ""); rootHow.Expanded = false; tvAcc.Nodes.Add(rootHow); HowBLL howBLL = new HowBLL(); DataTable dtHow = new DataTable(); dtHow = howBLL.GetItems(); for (int i = 0; i < dtHow.Rows.Count; i++) { string itemID = dtHow.Rows[i]["ID"].ToString(); string itemTitle = dtHow.Rows[i]["itemTitle"].ToString(); TreeNode treeDoc = new TreeNode(itemTitle, "HowWeWork.aspx?hid=" + itemID, "", "", ""); rootHow.ChildNodes.Add(treeDoc); } }

在SelectedNodeChanged()事件中添加当前状态的值:
(此处用的是TreeNode.Text, 而没用TreeeNode.ValuePath,因为Value值用来页面跳转了,与treeview的路径有一定出入。)
protected void tvAcc_SelectedNodeChanged(object sender, EventArgs e) { TreeNode tn = tvAcc.SelectedNode.Parent; if (Request.QueryString["state"] != null) { tn.Expanded = true; } string navUrl = tvAcc.SelectedNode.Value.ToString() + "&state=" + tn.Text; Response.Redirect(navUrl); }


转载于:https://www.cnblogs.com/eva_2010/archive/2012/06/15/2550697.html

    推荐阅读