|
菜鸟程序员表示2天才搞定...
CS页面:- protected void Page_Load(object sender, EventArgs e)
- {
- if (!IsPostBack)
- {
- LoadData();
- }
- }
- private void LoadData()
- {
- DataSet ds = new DataSet();
- ds.Tables.Add(CreateDataTable());
- ds.Relations.Add("TreeRelation", ds.Tables[0].Columns["Id"], ds.Tables[0].Columns["ParentId"]);
- foreach (DataRow row in ds.Tables[0].Rows)
- {
- if (row.IsNull("ParentId"))
- {
- TreeNode node = new TreeNode();
- node.Text = row["Text"].ToString();
- node.NodeID = row["Id"].ToString();
- node.Leaf = (bool)row["isLeaf"];
- Tree1.Nodes.Add(node);
- }
- }
- }
- private void ResolveSubTree(DataRow dataRow, TreeNode treeNode)
- {
- DataRow[] rows = dataRow.GetChildRows("TreeRelation");
- if (rows.Length > 0)
- {
- treeNode.Expanded = true;
- foreach (DataRow row in rows)
- {
- TreeNode node = new TreeNode();
- node.Text = row["Text"].ToString();
- treeNode.Nodes.Add(node);
- ResolveSubTree(row, node);
- }
- }
- }
- protected void Tree1_NodeExpand(object sender, FineUI.TreeExpandEventArgs e)
- {
- DynamicAppendNode(e.Node);
- }
- private void DynamicAppendNode(TreeNode parentNode)
- {
- parentNode.Expanded = true;
- TreeNode node = null;
- DataSet ds = new DataSet();
- ds.Tables.Add(CreateDataTable());
- ds.Relations.Add("TreeRelation", ds.Tables[0].Columns["Id"], ds.Tables[0].Columns["ParentId"]);
- foreach (DataRow row in ds.Tables[0].Rows)
- {
- string parentid = row["ParentId"].ToString();
- if (parentid == parentNode.NodeID)
- {
- node = new TreeNode();
- node.Text = row["Text"].ToString();
- node.NodeID = row["Id"].ToString();
- node.Leaf = (bool)row["isLeaf"];
- parentNode.Nodes.Add(node);
- }
- }
- }
- #region CreateDataTable
- private DataTable CreateDataTable()
- {
- DataTable table = new DataTable();
- DataColumn column1 = new DataColumn("Id", typeof(string));
- DataColumn column2 = new DataColumn("Text", typeof(String));
- DataColumn column3 = new DataColumn("ParentId", typeof(string));
- DataColumn column4 = new DataColumn("isLeaf", typeof(bool));
- table.Columns.Add(column1);
- table.Columns.Add(column2);
- table.Columns.Add(column3);
- table.Columns.Add(column4);
- DataRow row = table.NewRow();
- row = table.NewRow();
- row[0] = "xinjiang";
- row[1] = "新疆";
- row[2] = DBNull.Value;
- row[3] = false;
- table.Rows.Add(row);
- row = table.NewRow();
- row[0] = "wulumuqi";
- row[1] = "乌鲁木齐";
- row[2] = "xinjiang";
- row[3] = false;
- table.Rows.Add(row);
- row = table.NewRow();
- row[0] = "changji";
- row[1] = "昌吉";
- row[2] = "xinjiang";
- row[3] = false;
- table.Rows.Add(row);
- row = table.NewRow();
- row[0] = "tianshanqu";
- row[1] = "天山区";
- row[2] = "wulumuqi";
- row[3] = true;
- table.Rows.Add(row);
- row = table.NewRow();
- row[0] = "shayibakequ";
- row[1] = "沙依巴克区";
- row[2] = "wulumuqi";
- row[3] = true;
- table.Rows.Add(row);
- row = table.NewRow();
- row[0] = "wujiaqu";
- row[1] = "五家渠";
- row[2] = "changji";
- row[3] = true;
- table.Rows.Add(row);
- row = table.NewRow();
- row[0] = "mulei";
- row[1] = "木垒";
- row[2] = "changji";
- row[3] = true;
- table.Rows.Add(row);
- return table;
- }
- #endregion
复制代码 aspx页面:- <body>
- <form id="form1" runat="server">
- <x:PageManager ID="PageManager1" runat="server" />
- <x:Tree ID="Tree1" Width="500px" EnableArrows="true" EnableLines="false" ShowHeader="true" OnNodeExpand="Tree1_NodeExpand"
- Title="树控件" runat="server" AutoLeafIdentification="false">
- </x:Tree>
- </form>
- </body>
复制代码 |
|