treeview加载数据库内容作为导航只输出了一个0的子节点,求各位指教
我想用treeview加载数据库内容作为导航数据库结构为ID ,name,PID(导航属于哪一级别的),jibie(数据之间的关系)
输入下面的代码但是运行网页时候只输出了一个0的子节点,求各位指教!谢谢!
private readonly string cmdstr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TreeNode node = new TreeNode();
InitTree(ref node, 0);
this.TreeView1.Nodes.Add(node);
}
}
private List<CAUTreeNode> GetChildrenNode(int parentid)
{
List<CAUTreeNode> s = new List<CAUTreeNode>();
try
{
SqlConnection mcon = new SqlConnection(cmdstr);
mcon.Open();
SqlCommand mcmd = new SqlCommand();
mcmd.Connection = mcon;
mcmd.CommandText = string.Format("select * from Main where class={0}", parentid);
SqlDataReader sdr = mcmd.ExecuteReader();
while (sdr.Read())
{
CAUTreeNode node = new CAUTreeNode()
{
ID = int.Parse(sdr["Id"].ToString()),
Name = sdr["name"].ToString(),
ParentId = int.Parse(sdr["PID"].ToString()),
Jibie = sdr["jibie"].ToString(),
};
s.Add(node);
}
}
catch
{
}
return s;
}
/// <summary>
/// 根据Id获得节点信心
/// </summary>
/// <param name="Id"></param>
/// <returns></returns>
private CAUTreeNode GetNodeById(int Id)
{
CAUTreeNode node = new CAUTreeNode();
try
{
SqlConnection mcon = new SqlConnection(cmdstr);
mcon.Open();
SqlCommand mcmd = new SqlCommand();
mcmd.Connection = mcon;
mcmd.CommandText = string.Format("select * from main where Id={0}", Id);
SqlDataReader sdr = mcmd.ExecuteReader();
if (sdr.Read())
{
node.ID = int.Parse(sdr["Id"].ToString());
node.Name = sdr["name"].ToString();
node.ParentId = int.Parse(sdr["PID"].ToString());
node.Jibie = sdr["jibie"].ToString();
}
}
catch
{
}
return node;
}
private void InitTree(ref TreeNode parentnode, int parentid)
{
List<CAUTreeNode> rows = GetChildrenNode(parentid);
if (rows == null)
return;
if (rows.Count > 0)
{
foreach (var item in rows)
{
TreeNode node = new TreeNode();
node.Text = item.Name;
node.Value = item.ID.ToString();
List<CAUTreeNode> rows1 = GetChildrenNode(item.ID);
foreach (var item1 in rows1)
{
InitTree(ref node, item1.ID);
}
parentnode.ChildNodes.Add(node);
}
}
else
{
CAUTreeNode temprows = GetNodeById(parentid);
TreeNode tempnode = new TreeNode();
tempnode.Text = temprows.Name;
tempnode.Value = temprows.ID.ToString();
parentnode.ChildNodes.Add(tempnode);
}
} --------------------编程问答-------------------- 百度 treeview 递归 一大堆的代码 --------------------编程问答-------------------- 楼主可以考虑用递归的方式读取数据库中的数据,然后绑定到treeview,代码要简洁得多。论坛里面关于这个问题的递归算法有不少了。搜一下吧。 --------------------编程问答--------------------
--------------------编程问答-------------------- 你先做个DEMO试试,是不是节点不对呀 --------------------编程问答-------------------- 可以试试楼上的想法
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
namespace Galsun.Test.Web
{
public partial class TreeView : System.Web.UI.Page
{
DataTable dt = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dt.Columns.Add("a1");
dt.Columns.Add("a2");
dt.Columns.Add("a3");
dt.Columns.Add("num", Type.GetType("System.Int32"));
dt.Rows.Add(new object[] { "1", "0", "我的文件夹", 2 });
dt.Rows.Add(new object[] { "2", "0", "工作文件夹", 1 });
dt.Rows.Add(new object[] { "3", "1", "图片", 1 });
dt.Rows.Add(new object[] { "4", "1", "文档", 2 });
dt.Rows.Add(new object[] { "5", "3", "风景", 2 });
dt.Rows.Add(new object[] { "6", "3", "人物", 1 });
dt.Rows.Add(new object[] { "7", "2", "报告", 1 });
dt.Rows.Add(new object[] { "8", "3", "易做图", 3 });
bindData("0", TreeView1.Nodes,"根目录");
}
}
private void bindData(string parentid, TreeNodeCollection tnc,string path)
{
DataRow[] ary_row = dt.Select("a2=" + parentid, "num");
foreach (DataRow item in ary_row)
{
TreeNode node = new TreeNode();
node.Text = string.Format("<font onclick=\"selval(this)\" style=\"cursor:pointer;\">{0}</font>", item["a3"].ToString());
node.Value = item["a1"].ToString();
string txtpath = path + "//" + item["a3"].ToString();
node.Expanded = true;//是否展开
node.ShowCheckBox = true;//是否显示选择框
node.SelectAction = TreeNodeSelectAction.None;
//node.NavigateUrl = "javascript:selval('" + txtpath + "');";//连接路径
//node.SelectAction = TreeNodeSelectAction.Expand;//选择事件
tnc.Add(node);
bindData(item["a1"].ToString(), tnc[tnc.Count - 1].ChildNodes, txtpath);
}
}
}
}
补充:.NET技术 , ASP.NET