问题描述:C1TreeView后台递归绑定
问题解答:直接上代码
前台代码(直接拖入一个C1TreeView控件)
<wijmo:C1TreeView ID="C1TreeView1" runat="server" ShowCheckBoxes="true">
</wijmo:C1TreeView>
后台代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using C1.Web.Wijmo.Controls.C1TreeView;
public partial class tree : System.Web.UI.Page
{
DataTable areaDT = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
setDT();
C1TreeViewNode node = new C1TreeViewNode();
DataRow[] far = areaDT.Select("subid=0");
DataRow drfar = far[0];
node.Text = drfar[1].ToString();
this.C1TreeView1.Nodes.Add(node);
DataRow[] drarea = areaDT.Select("subid=1");
for (int i = 0; i < drarea.Length; i++)
{
AddArea(this.C1TreeView1.Nodes[0].Nodes, drarea);
}
}
private void AddArea(C1TreeViewNodeCollection tvn, DataRow dr)
{
int areaid = Convert.ToInt32(dr[0]);
string areaname = dr[1].ToString().Trim();
C1TreeViewNode newnode = new C1TreeViewNode();
newnode.Text = areaname;
tvn.Add(newnode);
C1TreeViewNodeCollection tvn1 = newnode.Nodes;
DataRow[] drssuperarea = areaDT.Select("subid=" + areaid);
for (int i = 0; i < drssuperarea.Length; i++)
{
AddArea(tvn1, drssuperarea);
}
}
private void setDT()
{
areaDT.Columns.Add("id");
areaDT.Columns.Add("name");
areaDT.Columns.Add("subid");
DataRow dr = areaDT.NewRow();
dr["id"] = 1;
dr["name"] = "父文件";
dr["subid"] = 0;
areaDT.Rows.Add(dr);
dr = areaDT.NewRow();
dr["id"] = 2;
dr["name"] = "子文件1";
dr["subid"] = 1;
areaDT.Rows.Add(dr);
dr = areaDT.NewRow();
dr["id"] = 3;
dr["name"] = "孙文件";
dr["subid"] = 2;
areaDT.Rows.Add(dr);
}
}
效果截图
|