前台只需要放一个 treeview控件就行
后台
protected void Page_Load(object sender EventArgs e)
{
if (!IsPostBack)
bindTree();
}
private void bindTree()
{
DataTable dt = thisGetTreeData();
thisFillNode(dt null);
}
private void FillNode(DataTable dt TreeNode node)
{
DataView dv = new DataView(dt);
if (node == null) //根结点
{
dvRowFilter = "parentid="; //筛选到根结点的所有子节点
}
else //不是根结点
{
dvRowFilter = "parentid=" + nodeValue + ""; //筛选对应节点的所有子节点
}
foreach (DataRowView drv in dv) //遍历填充节点的所有子节点如果传入的节点node为叶子节点遍历要退出不再进行递归
{
TreeNode no = new TreeNode(drv["menuname"]ToString() drv["menuid"]ToString());
FillNode(dt no); //填充no节点的子节点
if (node == null)
{
thisTreeViewNodesAdd(no);
}
else
{
nodeChildNodesAdd(no);
}
}
}
private DataTable GetTreeData()
{
string strcnn = ConfigurationManagerConnectionStrings["treecnn"]ConnectionString;
using (SqlConnection sqlcnn = new SqlConnection(strcnn))
{
SqlCommand sqlcmm = sqlcnnCreateCommand();
sqlcmmCommandText = "select * from MenuTree order by parentidmenuorder";
SqlDataAdapter da = new SqlDataAdapter(sqlcmm);
DataSet ds = new DataSet();
daFill(ds);
return dsTables[];
}
}