绑定无限级的树形菜单,在前台需要的地方拖放一个TreeView控件,后台绑定代码如下:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class InfinitTree : System.Web.UI.Page
{
string sql = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindTVInfinite();
}
}
void BindTVInfinite()
{
sql = "select * from treeTable where parentID=0";
DataTable dt = DataBase.ExecuteDataSet(sql).Tables[0];
foreach (DataRow dr in dt.Rows)
{
TreeNode node = new TreeNode();
node.Text = dr["treeName"].ToString();
node.Value = dr["treeID"].ToString();
//bind child nodes
BindNodes(int.Parse(dr["treeID"].ToString()), node);
TVInfinite.Nodes.Add(node);
}
}
void BindNodes(int parentID,TreeNode node)
{
sql = string.Format("select * from treeTable where parentID={0}",parentID);
DataTable dt = DataBase.ExecuteDataSet(sql).Tables[0];
foreach (DataRow rows in dt.Rows)
{
TreeNode tnode = new TreeNode();
tnode.Text = rows["treeName"].ToString();
tnode.Value = rows["treeID"].ToString();
node.ChildNodes.Add(tnode);
BindNodes(int.Parse(rows["treeID"].ToString()), tnode);
}
}
}
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class InfinitTree : System.Web.UI.Page
{
string sql = null;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindTVInfinite();
}
}
void BindTVInfinite()
{
sql = "select * from treeTable where parentID=0";
DataTable dt = DataBase.ExecuteDataSet(sql).Tables[0];
foreach (DataRow dr in dt.Rows)
{
TreeNode node = new TreeNode();
node.Text = dr["treeName"].ToString();
node.Value = dr["treeID"].ToString();
//bind child nodes
BindNodes(int.Parse(dr["treeID"].ToString()), node);
TVInfinite.Nodes.Add(node);
}
}
void BindNodes(int parentID,TreeNode node)
{
sql = string.Format("select * from treeTable where parentID={0}",parentID);
DataTable dt = DataBase.ExecuteDataSet(sql).Tables[0];
foreach (DataRow rows in dt.Rows)
{
TreeNode tnode = new TreeNode();
tnode.Text = rows["treeName"].ToString();
tnode.Value = rows["treeID"].ToString();
node.ChildNodes.Add(tnode);
BindNodes(int.Parse(rows["treeID"].ToString()), tnode);
}
}
}