.NET treeview无限分级问题???
我知道是用递归写。。。但给点思路吧。。。贴点代码。。。解释一下代码的内容。。。主要是不怎么明白递归思想。。。顺便也说说递归算法吧。。。各位神啊。。。救救我吧。。。.NET treeview无限分级问题???谢谢勒。。。 --------------------编程问答-------------------- 求求你们帮帮小弟吧。。。谢谢。。。 --------------------编程问答-------------------- namespace Tree{
public partial class _Default : System.Web.UI.Page
{
string StrConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + HttpContext.Current.Server.MapPath("~/") + "b.mdb";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadData();
}
}
private void LoadData()
{
OleDbConnection cn = new OleDbConnection(StrConn);
cn.Open();
OleDbCommand cmd = new OleDbCommand("select * from D where DID=PID", cn);
OleDbDataReader read = cmd.ExecuteReader();
while (read.Read())
{
MyTreeView.Nodes.Clear();
TreeNode part = new TreeNode();
part.Expanded = true;
part.Text = read["DName"].ToString();
part.Value = read["DId"].ToString();
MyTreeView.Nodes.Add(part);
FillFolderNode(read["DId"].ToString(), part);
}
read.Close();
cn.Close();
}
private void FillFolderNode(string p, TreeNode part)
{
OleDbConnection cn = new OleDbConnection(StrConn);
cn.Open();
OleDbCommand cmd = new OleDbCommand("select * from D where PID=" + p + " and DID<>PID", cn);
OleDbDataReader read = cmd.ExecuteReader();
while (read.Read())
{
TreeNode parts = new TreeNode();
parts.Expanded = false;
parts.Text = read["DName"].ToString();
parts.Value = read["DId"].ToString();
part.ChildNodes.Add(parts);
FillFolderNode1(read["DId"].ToString(), parts);
}
read.Close();
cn.Close();
}
private void FillFolderNode1(string p, TreeNode part)
{
OleDbConnection cn = new OleDbConnection(StrConn);
cn.Open();
OleDbCommand cmd = new OleDbCommand("select * from D where DID=" + p + " and DID<>PID", cn);
OleDbDataReader read = cmd.ExecuteReader();
while (read.Read())
{
TreeNode parts = new TreeNode();
parts.Expanded = false;
parts.Text = read["DName"].ToString();
parts.Value = read["DId"].ToString();
part.ChildNodes.Add(parts);
}
read.Close();
cn.Close();
}
protected void MyTreeView_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
string id = e.Node.Value;
TreeNode subnode = e.Node;
subnode.ChildNodes.Clear();//删除以前的,然后才可向下添加两级
FillFolderNode(id, subnode);
}
protected void MyTreeView_SelectedNodeChanged(object sender, EventArgs e)
{
string id = MyTreeView.SelectedNode.Value;
MyTreeView.SelectedNode.Expanded = true;
TreeNode subnode = MyTreeView.SelectedNode;//当前节点
subnode.ChildNodes.Clear();//删除以前的,然后才可向下添加两级
FillFolderNode(id, subnode);
Response.Write("<script>window.open('t.aspx?id=" + id + "');</script>");
}
}
} --------------------编程问答--------------------
学习学习 --------------------编程问答-------------------- 还是有点。。。我是用sql server做的。。 --------------------编程问答-------------------- if(!IsPostBack)
{
BindJG();
}
private void BindJG()
{
DataSet ds = new DataSet();
//获取数据
DataTable dtb = ds.Tables[0];
TreeNode root = new TreeNode();
root.Value = "0";
root.Text = "";
root.ImageUrl = "../images/folder.gif";
root.Expanded = true;
this.TreeView1.Nodes.Add(root);
initTree(dtb, "", root);
this.TreeView1.ExpandAll();
}
protected void initTree(DataTable dt, string nFatherid, TreeNode fatherNode)
{
DataView dv = new DataView(dt);
if (nFatherid == "")
dv.RowFilter = "depth='1'";
else
dv.RowFilter = "ParentId='"+nFatherid+"'";
foreach (DataRowView Row in dv)
{
TreeNode node = new TreeNode();
if (fatherNode.Value == "0")
{
node.Value = Row["BH"].ToString();
node.Text = Row["mc"].ToString();
node.NavigateUrl = "";
node.ImageUrl = "../images/folder.gif";
fatherNode.ChildNodes.Add(node);
initTree(dt, Row["BH"].ToString(), node);
}
else
{
node.Text = Row["mc"].ToString();
node.Value = Row["BH"].ToString();
node.ImageUrl = "../images/jg.gif";
fatherNode.ChildNodes.Add(node);
}
}
}
--------------------编程问答-------------------- 不用写Sql语句吗 --------------------编程问答-------------------- 无限分级,个人觉得不应该一次性加载
因为这样效率不高,你可以看一下Windows操作系统(比如资源管理器)就是逐级加载的
表结构就是多加个Pid
如果非要递归,好像也只能在业务逻辑层来实现,想直接通过sql语句来实现好像是不现实的
--------------------编程问答-------------------- 学习了! --------------------编程问答-------------------- 分页可以用存储过程,别人总是问分页存储过程 --------------------编程问答-------------------- 好郁闷啊!!!无限分级~~~怨念
补充:.NET技术 , ASP.NET