TreeView保存当前节点展开状态
由于微软的TreeView设计成提交式的,所以有的人写程序的时候喜欢在把node.NavigateUrl写成本页再加上一个参数(我原来也喜欢这么做),这样做的后果是一提交之后,treeView又回到原来的样式,原来点的节点又缩回去,让用户不知道点了哪一个节点,网上有很多人给了思路,以下是解决方法。
一:首先在给节点赋值的时候不要给NavigateUrl赋值。把整个TreeView的TreeView1.ExpandDepth设成1级或者二级,看你的情况而定,默认为全部展开。
二:给TreeView添加SelectedNodeChanged事件,如下:
protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
{
string typeId = TreeView1.SelectedNode.Value;
string typeName = TreeView1.SelectedNode.Text;
string path = "Default.aspx?typeId=" + typeId + "&typeName=" + Server.UrlEncode(typeName);
Context.Items["path"] = TreeView1.SelectedNode.ValuePath;
Server.Transfer(path);
}
三:在Page_load事件加入以下代码:
if (!IsPostBack)
{
if (Context.Items["path"] != null)
{
TreeNode node = TreeView1.FindNode(Context.Items["path"].ToString()) as TreeNode;
if (node != null)
{
expand(node);
node.Selected = true;
}
}
}
四:另外写一个方法 expand(TreeNode node)
private void expand(TreeNode node)
{
if (node.Parent != null)
{
node.Parent.Expanded = true;
expand(node.Parent);
}
}
五:设置一下SelectNodeStyle,使选择的节点满足您的要求。
注意:
关于TreeView的OnSelectedNodeChanged事件
只有节点(Node)的SelectAction被设定为Select时才能够触发该事件,而TreeView的DataSource如果为siteMap则默认该属性值为None,并且如果节点(Node)的Target属性被设定为一个url则会忽略OnSelectedNodeChanged,而直接跳转到该url
因此使用siteMap作为TreeView的数据源时,添加OnTreeNodeDataBound事件的回调方法,将上诉2个属性进行变更
<asp:TreeView ID="tvAcc" runat="server" ShowLines="True" ExpandDepth="1" NodeIndent="0" LeafNodeStyle-Width="100%" NodeStyle-Width="100%" ParentNodeStyle-Width="100%" RootNodeStyle-Width="100%" SelectedNodeStyle-Width="100%" OnSelectedNodeChanged="tvAcc_SelectedNodeChanged"> <Nodes> <asp:TreeNode Text="Test1" Value="Test1"> <asp:TreeNode Text="Test11" Value="Test11"> <asp:TreeNode Text="Test111" Value="Test111"></asp:TreeNode> </asp:TreeNode> </asp:TreeNode> <asp:TreeNode Text="Test2" Value="Test2"> <asp:TreeNode Text="Test11" Value="Test11"> <asp:TreeNode Text="Test111" Value="Test111"></asp:TreeNode> </asp:TreeNode> <asp:TreeNode Text="Test22" Value="Test22"> <asp:TreeNode Text="Test221" Value="Test221"></asp:TreeNode> <asp:TreeNode Text="Test222" Value="Test222"></asp:TreeNode> </asp:TreeNode> </asp:TreeNode> <asp:TreeNode Text="Test3" Value="Test3"> <asp:TreeNode Text="Test31" Value="Test31"> <asp:TreeNode Text="Test311" Value="Test311"></asp:TreeNode> </asp:TreeNode> </asp:TreeNode> <asp:TreeNode Text="Test4" Value="Test4"> <asp:TreeNode Text="Test41" Value="Test41"></asp:TreeNode> </asp:TreeNode> </Nodes> </asp:TreeView>
后台代码:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { if (Context.Items["path"] != null) { TreeNode node = tvAcc.FindNode(Context.Items["path"].ToString()) as TreeNode; if (node != null) { expand(node); node.Selected = true; } } } } private void expand(TreeNode node) { if (node.Parent != null) { node.Parent.Expanded = true; expand(node.Parent); } } protected void tvAcc_SelectedNodeChanged(object sender, EventArgs e) { string typeId = tvAcc.SelectedNode.Value; string typeName = tvAcc.SelectedNode.Text; string path = "Default.aspx?typeId=" + typeId + "&typeName=" + Server.UrlEncode(typeName); //在Node.Value中保存NavigationUrl信息,通过Server.Transfer跳转到指定页面。替换掉此处Sample用的Default.aspx Context.Items["path"] = tvAcc.SelectedNode.ValuePath; Server.Transfer(path);//Response.Redirect(path); }
Backup for 动态绑定TreeView后台Sample:
private void BindDocNode() { TreeNode rootDoc = new TreeNode("Document Center", "Document Center", "", "#", ""); rootDoc.Expanded = false; tvAcc.Nodes.Add(rootDoc); DocBLL docBLL = new DocBLL(); DataTable dtDoc = new DataTable(); dtDoc = docBLL.GetItems(); for (int i = 0; i < dtDoc.Rows.Count; i++) { string itemID = dtDoc.Rows[i]["ID"].ToString(); string itemTitle = dtDoc.Rows[i]["itemTitle"].ToString(); TreeNode treeDoc = new TreeNode(itemTitle, "DocumentCenter.aspx?did=" + itemID, "", "", ""); rootDoc.ChildNodes.Add(treeDoc); } }