我有一个asp.net树视图控件,它在页面加载时加载树数据。代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DocEng.TreeProvider cmsTree = new DocEng.TreeProvider(MembershipContext.AuthenticatedUser);
var node = cmsTree.SelectSingleNode(SiteContext.CurrentSiteName, "/", "en-US");
DocEng.TreeNodeCollection myChildren = node.AllChildren;
IEnumerable<DocEng.TreeNode> TreeNodes = myChildren.AsEnumerable<DocEng.TreeNode>();
foreach (var tree in TreeNodes.Where(x => x.NodeLevel == 0))
{
System.Web.UI.WebControls.TreeNode ParenTreeNode = new System.Web.UI.WebControls.TreeNode();
ParenTreeNode.Text = tree.NodeID.ToString() + ". " + SiteContext.CurrentSiteName;
ParenTreeNode.Value = tree.NodeID.ToString();
AddNodes(ParenTreeNode);
tvContentTree.Nodes.Add(ParenTreeNode);
}
}
}
/// <summary>
/// Adding child for the parent
/// </summary>
/// <param name="TreeNode"></param>
private void AddNodes(System.Web.UI.WebControls.TreeNode TreeNode)
{
DocEng.TreeProvider cmsTree = new DocEng.TreeProvider(MembershipContext.AuthenticatedUser);
var node = cmsTree.SelectSingleNode(SiteContext.CurrentSiteName, "/", "en-US");
DocEng.TreeNodeCollection myChildren = node.AllChildren;
IEnumerable<DocEng.TreeNode> childTreeNodes = myChildren.AsEnumerable<DocEng.TreeNode>();
foreach (var childTree in childTreeNodes.Where(x => x.NodeParentID == Convert.ToInt32(TreeNode.Value)))
{
System.Web.UI.WebControls.TreeNode ChildNode = new System.Web.UI.WebControls.TreeNode();
ChildNode.Text = childTree.NodeID.ToString() + ". " + childTree.DocumentName.ToString();
ChildNode.Value = childTree.NodeID.ToString();
TreeNode.ChildNodes.Add(ChildNode);
AddNodes(ChildNode);
if (childTree.NodeLevel == 1)
ChildNode.Collapse();
}
}
我需要重写此代码,仅在扩展节点时加载数据。此外,在我们扩展的同时,数据应仅填充到点击的节点及其子节点,直到结束。
答案 0 :(得分:0)
尝试仅获取Page-Load上的第一级节点,并且foreach节点添加一个空子节点,只是为了使其可扩展。
然后在展开后删除此子节点并用普通子节点填充它。