C#生成树状图
库里表的结构如下:
id ParentId父类ID name名称
id ParentId父类ID name名称
Code
前台页代码如下:
<div>
<%
Response.Write(Tree());
%>
</div>
后台代码如下:
public static string Tree()
{
StringBuilder sb = new StringBuilder();
List<treeinfo> list = GetParentId();
foreach (treeinfo info in list)
{
sb.AppendFormat("<ul><li>{0}", info.Name);
StringBuilder sb2 = new StringBuilder(TreeNode(sb.ToString(), info.Id));
sb2.AppendFormat("</li>");
//先清空再赋值
sb.Remove(0,sb.Length-1);
sb.Append(sb2);
}
sb.AppendFormat("</ul>");
return sb.ToString();
}
public static string TreeNode(string html, int ParentID)
{
StringBuilder sb = new StringBuilder(html);
List<treeinfo> li = GetParentId(ParentID);
foreach (treeinfo info in li)
{
sb.AppendFormat("<ul><li>{0}", info.Name);
StringBuilder sb2 = new StringBuilder(TreeNode(sb.ToString(), info.Id));
sb2.AppendFormat("</li>");
sb2.AppendFormat("</ul>");
//先清空再赋值
sb.Remove(0, sb.Length - 1);
sb.Append(sb2);
}
return sb.ToString();
}
//获取第一个父类
public static List<treeinfo> GetParentId()
{
List<treeinfo> list = new List<treeinfo>();
treeinfo info = new treeinfo();
string sql = "select * from dbo.Type where ParentId=0";
DataSet ss = DbHelper.ExecuteDataset(sql);
DataTable tab = ss.Tables[0];
if (tab.Rows.Count > 0)
{
info.Id = (int)tab.Rows[0][0];
info.ParentId = (int)tab.Rows[0][1];
info.Name = tab.Rows[0][2].ToString();
}
list.Add(info);
return list;
}
//能过父类ID来获取相关项
public static List<treeinfo> GetParentId(int ParentId)
{
List<treeinfo> list = new List<treeinfo>();
string sql = "select * from dbo.Type where ParentId=" + ParentId;
DataSet ss = DbHelper.ExecuteDataset(sql);
DataTable tab = ss.Tables[0];
if (tab.Rows.Count > 0)
{
foreach(DataRow rows in tab.Rows)
{
treeinfo info = new treeinfo();
info.Id = (int)rows[0];
info.ParentId = (int)rows[1];
info.Name = rows[2].ToString();
list.Add(info);
}
}
return list;
}
前台页代码如下:
<div>
<%
Response.Write(Tree());
%>
</div>
后台代码如下:
public static string Tree()
{
StringBuilder sb = new StringBuilder();
List<treeinfo> list = GetParentId();
foreach (treeinfo info in list)
{
sb.AppendFormat("<ul><li>{0}", info.Name);
StringBuilder sb2 = new StringBuilder(TreeNode(sb.ToString(), info.Id));
sb2.AppendFormat("</li>");
//先清空再赋值
sb.Remove(0,sb.Length-1);
sb.Append(sb2);
}
sb.AppendFormat("</ul>");
return sb.ToString();
}
public static string TreeNode(string html, int ParentID)
{
StringBuilder sb = new StringBuilder(html);
List<treeinfo> li = GetParentId(ParentID);
foreach (treeinfo info in li)
{
sb.AppendFormat("<ul><li>{0}", info.Name);
StringBuilder sb2 = new StringBuilder(TreeNode(sb.ToString(), info.Id));
sb2.AppendFormat("</li>");
sb2.AppendFormat("</ul>");
//先清空再赋值
sb.Remove(0, sb.Length - 1);
sb.Append(sb2);
}
return sb.ToString();
}
//获取第一个父类
public static List<treeinfo> GetParentId()
{
List<treeinfo> list = new List<treeinfo>();
treeinfo info = new treeinfo();
string sql = "select * from dbo.Type where ParentId=0";
DataSet ss = DbHelper.ExecuteDataset(sql);
DataTable tab = ss.Tables[0];
if (tab.Rows.Count > 0)
{
info.Id = (int)tab.Rows[0][0];
info.ParentId = (int)tab.Rows[0][1];
info.Name = tab.Rows[0][2].ToString();
}
list.Add(info);
return list;
}
//能过父类ID来获取相关项
public static List<treeinfo> GetParentId(int ParentId)
{
List<treeinfo> list = new List<treeinfo>();
string sql = "select * from dbo.Type where ParentId=" + ParentId;
DataSet ss = DbHelper.ExecuteDataset(sql);
DataTable tab = ss.Tables[0];
if (tab.Rows.Count > 0)
{
foreach(DataRow rows in tab.Rows)
{
treeinfo info = new treeinfo();
info.Id = (int)rows[0];
info.ParentId = (int)rows[1];
info.Name = rows[2].ToString();
list.Add(info);
}
}
return list;
}