c# - asp:TreeView with parent-child relationship in SQL -
i wrote code:
protected void rootnodes() { var contents = listcontentrootnodes(0, this.culture, true); foreach (var content in contents) { treenode newnode = new treenode { value = content.title }; list<treenode> childnode = childnode(content.contentid); if (childnode != null) { foreach (var item in childnode) { newnode.childnodes.add(item); } treeview.nodes.add(newnode); } treeview.expandall(); } } protected list<treenode> childnode(int contentid) { var subcontents = listcontentchildnodesall(contentid, this.culture); list<treenode> nodes = new list<treenode>(); foreach (var sub in subcontents) { nodes.add(new treenode { value = sub.title }); childnode(sub.contentid); } return nodes; }
'listcontentrootnodes' returns first level data rows 'parentid' set '-1'. listcontentchildnodesall calling stored procedure:
create procedure [dbo].[listchildnodesall] @contentid int, @culture nvarchar(5) set nocount on select * content parentid=@contentid , culture=@culture
as far searched on net, i've understood need recursion method in order populate rows 'db', not managed it. need create sitemap subnodes represented. code returning first level , not child rows. how should fixed it?
i find solution.my complete working code :
protected void rootnodes() { var contents = listcontentrootnodes(0, this.culture, true); foreach (var content in contents) { treenode newnode = new treenode { value = content.title }; list<treenode> childnode = childnode(content.contentid); if (childnode != null) { foreach (var item in childnode) { newnode.childnodes.add(item); } treeview.nodes.add(newnode); } treeview.expandall(); } } protected list<treenode> childnode(int contentid) { var subcontents = listcontentchildnodesall(contentid, this.culture); list<treenode> nodes = new list<treenode>(); foreach (var sub in subcontents) { treenode nod = new treenode { value = sub.title }; nodes.add(nod); list<treenode> leafnodes = childnode(sub.contentid); foreach (var leaf in leafnodes) { nod.childnodes.add(leaf); } } return nodes; }
Comments
Post a Comment