asp.net(VB.NET)做的动态加载treeview中子结点的例子
asp.net(VB.NET)做的动态加载treeview中子结点的例子,网上太多的是C#做的
ComDll.cDepts是调用DLL中的一个类
是可以打开无限子节点的,只是不知如何不刷新加载子节点
以下是代码部分:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then '不加此句的话,只能加到第三级
Dim obj As ComDll.cDepts
Dim Mynode() As TreeNode
Dim i As Integer
obj = New ComDll.cDepts
obj.Find(0, 0)
'i = obj.Count
TreeView1.Nodes.Clear()
ReDim Mynode(obj.Count + 1)
Mynode(0) = New TreeNode
Mynode(0).Text = "单位名称"
Mynode(0).Value = "单位名称"
TreeView1.Nodes.Add(Mynode(0))
For i = 1 To obj.Count - 1
Mynode(i) = New TreeNode
'Response.Write(obj.Item(i).DeptName & "<br>")
Mynode(i).Text = obj.Item(i).DeptName
Mynode(i).Value = obj.Item(i).ID
Mynode(i).ToolTip = "单位性质:" & obj.Item(i).UnitQulity & " 用户名:" & obj.Item(i).ID
Mynode(i).PopulateOnDemand = True
Mynode(i).SelectAction = TreeNodeSelectAction.SelectExpand
TreeView1.Nodes.Item(0).ChildNodes.Add(Mynode(i))
Next
TreeView1.ExpandDepth = 1
TreeView1.ExpandAll()
End If
End Sub
Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
On Error GoTo err
Dim SelectedUnitID As Integer
SelectedUnitID = TreeView1.SelectedValue
'打开当前选择节点的子节点
If TreeView1.SelectedNode.ChildNodes.Count = 0 Then
Dim obj As ComDll.cDepts
Dim Mynode2() As TreeNode
Dim i As Long
obj = New ComDll.cDepts
obj.Find(0, SelectedUnitID)
'i = obj.Count
ReDim Mynode2(obj.Count)
For i = 1 To obj.Count
Mynode2(i) = New TreeNode
Mynode2(i).Text = obj.Item(i).DeptName
Mynode2(i).Value = obj.Item(i).ID
Mynode2(i).ToolTip = "单位性质:" & obj.Item(i).UnitQulity & " 用户名:" & obj.Item(i).ID
Mynode2(i).PopulateOnDemand = True
Mynode2(i).SelectAction = TreeNodeSelectAction.SelectExpand
TreeView1.SelectedNode.ChildNodes.Add(Mynode2(i))
Next
TreeView1.ExpandDepth = 1
TreeView1.ExpandAll()
End If
err:
Response.Write(Err.Description)
End Sub
End Class
[解决办法]
顶上去先.我也正在做这一类东西.
[解决办法]
数据库的结构是什么样的?
[解决办法]
给你个例子吧
treeview 源教程动态绑定树部分代码段
============================================================================
protected void page_load(object o ,eventargs e )
{//首先填充第一级节点
dataview dv=获取dataview视图函数;
foreach(datarowview datav in dv)
{//循环数据视图,将对应的值交给一个节点对象,然后添加到树上有继续添加
treenode tn=new TreeNode();
tn.Text=datav("title").ToString();
tn.Value=datav("bid").ToString();
tn.Expanded=false;
TreeView1.Nodes.Add(tn);
AddchildNodes(tn);//调用方法填充二级节点
}
}
private void AddchildNodes(TreeNode tn)
{//填充二级节点,他需要一级节点的value ,所以干脆送了一个完整的根节点对象
int bid=Convert.ToInt32(tn.Value);
DataView dv=drv("select * from class where bid="+bid+");
foreach(DataRowView datav in dv)
{//同上
TreeNode ctn=new TreeNode();
ctn.Text=datav["title"].ToString();
ctn.Value=datav["cid"].ToString();
ctn.NavigateUrl="http://thcjp.cnblogs.com/jiashe.aspx?aa="+datav["cid"].ToSring;
tn.ChildNodes.Add(ctn);
//把节点ctn加载到tn下面或者说添加为tn的子节点
}
}
//下面这段代码的作用在于把选用节点的text值打印出来
protect void Button1_Click(object sender,Eventargs e)
{
if(TreeView1.CheckedNodes.Count>0)
{
//如果有项被选中
foreach(TreeNode tn in TreeView1.CheckedNodes)
{//跌代所有被选中的节点
if(tb.ChildNodes.Count>0)
{//如果被选中项下面还有子节点
foreach(TreeNode ctn in tn.ChildNodes)
{//迭代所有子节点,把值读出来
Response.Write(ctn.Text+"<br>");
}
}
else
{
//如果下面没有节点了,说明是叶节点,那么直接读出被选取项的值
Response.Write(tn.Text+"<br>");
}
}
}
}
============================================================================
treeview 实例代码段(待续)
============================================================================
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class _Default : System.Web.UI.Page
{
TreeNode tn = new TreeNode();
protected void Page_Load(object sender, EventArgs e)
{
TreeView1.ShowLines = true;
TreeView1.ToolTip = "a test";
TreeView1.ShowExpandCollapse = true;
TreeView1.ShowCheckBoxes = TreeNodeTypes.Leaf;
if (!IsPostBack)
{
tn.Text = "根结点";
tn.Value = "rootnode";
tn.Expanded = true;
TreeView1.Nodes.Add(tn);
addlevel2nodes(tn);
}
else
{
addlevel2nodes(tn);
}
}
protected void addlevel2nodes(TreeNode fathertn)
{
string conn = "Server=localhost;Database=bookshop;Uid=sa;Pwd=asen";
Userinfo userinfo1 = new Userinfo(conn);
DataView dv1 = userinfo1.selectalluserinfo();
foreach (DataRowView dv in dv1)
{
TreeNode tn = new TreeNode();
tn.Text = dv["username"].ToString();
tn.Value = dv[3].ToString();
tn.Expanded = true;
fathertn.ChildNodes.Add(tn);
}
}
}
试验过,通过