读书人

100分求关于Asp.net 2.0中TreeView层

发布时间: 2011-12-28 22:45:21 作者: rapoo

100分,求关于Asp.net 2.0中TreeView层次输出
例如树:

根节点
A
A1
A2
A21
A22
B
C
C1
那么在页面输出时这样:(最好是Table)
-----------------------------------------------
| 根节点 |
-----------------------------------------------
| A | B | C |
-----------------------------------------------
| A1 | A2 | | C1 |
-----------------------------------------------
| | A21 | A22 | | |
-----------------------------------------------





[解决办法]
JF UP 我知道`不过不知道怎么说``汗``表达能力不够
[解决办法]
沙发,帮忙顶
[解决办法]
解析XML 还是数据库?
[解决办法]
TreeView的资源文件copy到根目录了吗?
[解决办法]
貌似我理解错误了楼主的意思
[解决办法]
学习,顶~
[解决办法]
没理解lz的意思
[解决办法]
就是将树过来显示,对不?~~
[解决办法]
这叫什么序遍历?我都忘记了~~-_-#!
[解决办法]
我的XML文件是这样写的
<?xml version= "1.0 " standalone= "yes "?>
<siteMap>
<siteMapNode title= "Library Home " url= "~/contentPages/LibraryHome.aspx " description= "Go To Library Home ">
<siteMapNode title= "Book 0 " url= "~/contentPages/book0/book.aspx " description= "Go To Book 0 ">
<siteMapNode title= "123 " url= "123 " description= "123 " />
<siteMapNode title= "1231 " url= "123 " description= "123 " />


</siteMapNode>
<siteMapNode title= "Book 1 " url= "~/contentPages/book1/book.aspx " description= "Go To Book 1 " />
<siteMapNode title= "Book 2 " url= "~/contentPages/book2/book.aspx " description= "Go To Book 2 " />
</siteMapNode>
</siteMap>
==================================================================================
HTML代码
<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> 无标题页 </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<asp:Table ID= "Table1 " runat= "server " Style= "position: relative ">
</asp:Table>

</div>
</form>
</body>
</html>
==================================================================================
C#代码
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Xml.XPath;

public partial class FileMS_Default2 : System.Web.UI.Page
{
XmlDocument xmlDoc;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TableCell td = new TableCell();
TableRow tr = new TableRow();
xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath( "sitemap.xml "));
XmlNodeList xnl = xmlDoc.SelectSingleNode( "siteMap ").ChildNodes;
td.Controls.Add(new LiteralControl( "siteMap "));
tr.Cells.Add(td);
Table1.Rows.Add(tr);
fill(xnl, td);
}
}

public void fill(XmlNodeList xnl,TableCell ptd)
{
TableRow ctr = new TableRow();
foreach (XmlNode xn in xnl)
{
XmlElement xe = (XmlElement)xn;
TableCell ctd = new TableCell();
ctd.Controls.Add(new LiteralControl(xe.GetAttribute( "title ")));
ctr.Cells.Add(ctd);
fill(xn.ChildNodes,ctd);
Table1.Rows.Add(ctr);
}
}
}

[解决办法]
楼上朋友写的好长啊
[解决办法]
--测试数据
DECLARE @t TABLE(ID char(3),PID char(3),Name nvarchar(10))
INSERT @t SELECT '001 ',NULL , '山东省 '
UNION ALL SELECT '002 ', '001 ', '烟台市 '
UNION ALL SELECT '004 ', '002 ', '招远市 '
UNION ALL SELECT '003 ', '001 ', '青岛市 '
UNION ALL SELECT '005 ',NULL , '四会市 '
UNION ALL SELECT '006 ', '005 ', '清远市 '
UNION ALL SELECT '007 ', '006 ', '小分市 '


--深度排序显示处理
--生成每个节点的编码累计(相同当单编号法的编码)
DECLARE @t_Level TABLE(ID char(3),Level int,Sort varchar(8000))
DECLARE @Level int
SET @Level=0
INSERT @t_Level SELECT ID,@Level,ID
FROM @t
WHERE PID IS NULL
WHILE @@ROWCOUNT> 0
BEGIN
SET @Level=@Level+1
INSERT @t_Level SELECT a.ID,@Level,b.Sort+a.ID
FROM @t a,@t_Level b


WHERE a.PID=b.ID
AND b.Level=@Level-1
END

--显示结果
SELECT SPACE(b.Level*2)+ '|-- '+a.Name
FROM @t a,@t_Level b
WHERE a.ID=b.ID
ORDER BY b.Sort
/*--结果
|--山东省
|--烟台市
|--招远市
|--青岛市
|--四会市
|--清远市
|--小分市
--*/
----------------------
用老大的算法改一下就可以了~~
[解决办法]
先看下面左边的tree view:

http://game.dg.gd.cn/List.aspx?ID=CTG200707051119024846CR

下面是code:
<asp:TreeView ID= "LinksTreeView " ForeColor= "Blue " OnTreeNodePopulate= "PopulateNode "
runat= "server " ImageSet= "Arrows ">
<Nodes>
<asp:TreeNode Text= "游戏目录 " SelectAction= "Select " PopulateOnDemand= "True "
Selected= "False " />
</Nodes>
</asp:TreeView>

protected void PopulateNode(Object sender, TreeNodeEventArgs e)
{
switch (e.Node.Depth)
{
case 0:
// Populate the first-level nodes.
PopulateCategories(e.Node);
break;
case 1:
// Populate the second-level nodes.
PopulateSort(e.Node);
break;
default:
// Do nothing.
break;
}

}

private void PopulateCategories(TreeNode node)
{

DataTable dt = objCategory.GetCategoryForTreeView();

if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode newNode = new TreeNode();
newNode.Text = dr[ "CategoryName "].ToString();
newNode.Value = dr[ "CategoryId "].ToString();
newNode.NavigateUrl = "~/List.aspx?ID= " + dr[ "CategoryId "].ToString();
newNode.PopulateOnDemand = true;
newNode.SelectAction = TreeNodeSelectAction.Select;
node.ChildNodes.Add(newNode);
}
}
}


private void PopulateSort(TreeNode node)
{
DataTable dt = objSort.GetSortForTreeView(node.Value);
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
TreeNode newNode = new TreeNode();
newNode.Text = dr[ "SortName "].ToString();
newNode.Value = dr[ "SortId "].ToString();
newNode.NavigateUrl = "~/List.aspx?ID= " + dr[ "SortId "].ToString();
newNode.PopulateOnDemand = true;
newNode.SelectAction = TreeNodeSelectAction.Select;
node.ChildNodes.Add(newNode);
}
}
}

读书人网 >asp.net

热点推荐