public partial class tree_datatable : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}
private void LoadData()
{
// 模拟从数据库返回数据表
DataTable table = CreateDataTable();
DataSet ds = new DataSet();
ds.Tables.Add(table);
ds.Relations.Add("TreeRelation", ds.Tables[0].Columns["Id"], ds.Tables[0].Columns["ParentId"], false);
foreach (DataRow row in ds.Tables[0].Rows)
{
if (row["ParentId"].ToString() == "0")
{
ExtAspNet.TreeNode node = new ExtAspNet.TreeNode();
node.Text = "<a href='showcar.aspx'>" + row["Text"].ToString() + "</a>";
node.Expanded = true;
Tree1.Nodes.Add(node);
ResolveSubTree(row, node);
}
}
}
private void ResolveSubTree(DataRow dataRow, ExtAspNet.TreeNode treeNode)
{
DataRow[] rows = dataRow.GetChildRows("TreeRelation");
if (rows.Length > 0)
{
treeNode.Expanded = true;
foreach (DataRow row in rows)
{
ExtAspNet.TreeNode node = new ExtAspNet.TreeNode();
node.Text = "<a href='showcar.aspx'>" + row["Text"].ToString() + "</a>";
treeNode.Nodes.Add(node);
ResolveSubTree(row, node);
}
}
}
#region CreateDataTable
private DataTable CreateDataTable()
{
DataTable dt = new Test().GetList("");
return dt;
}
#endregion
}
怎么样实现左边这种效果的
|