0
我有一個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();
}
}
我需要重寫這段代碼,當我們展開節點只加載數據。而且,當我們擴展時,數據只應填充到被點擊的節點及其子節點直到結束。
不知道這裏的問題是...你有沒有試圖自己重寫代碼?您必須使用['TreeNodeExpanded'](https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.treeview.treenodeexpanded(v = vs.110).aspx)事件。您可以通過查看CMS \ CMSModules \ Content \ Controls \ ContentTree.ascx控件獲得一些靈感。 – rocky