递归方法,无非就是自己调用自己!下面是一个绑定TreeView控件的实例:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
InitTree(this.tvClasses.Nodes, "0");
}
}
private void InitTree(TreeNodeCollection node, string parentId)
{
DataSet ds = common.GetList("select * from tb_web_classy");
if (CheckDataSet(ds, 0))
{
DataView dv = new DataView(ds.Tables[0]);
TreeNode tn;
dv.RowFilter = "parent_id=" + parentId;
foreach (DataRowView drv in dv)
{
tn = new TreeNode();
tn.Value = drv["type_id"].ToString();
tn.Text = drv["type_name"].ToString();
node.Add(tn);
InitTree(tn.ChildNodes, tn.Value.ToString()); //这里就是调用自己绑定子节点
}
}
效果图:
}