这几天在写hrm的时候 这问题搞了我两天,哈哈!开始在使用google 找了半天都是一堆垃圾,都是使用算法的较多, 后来就去了www.asp.net 找到点启示。
好了废话多说无用。
首先表结构如下 表名 test
写个存储过程 gettreeview
这个不用我说了吧下面用到
为了速度缓存datatable
public function gettreetable() as datatable
dim dt as new datatable()
dt = httpcontext.current.cache(“treeview”)
if dt is nothing then
dim
dim clsconndatabase as new connectiondatabase
dim command as new sqlcommand
command.connection =
command.commandtext = “gettreeview”
command.commandtype = commandtype.storedprocedure
command.executenonquery()
dim da as new sqldataadapter(command)
dt = new datatable()
da.fill(dt)
httpcontext.current.cache.insert(“treeview”, dt)
end if
return dt
end function
这里是主要阿
public sub populatenodes(byval nodes as treenodecollection, optional byval intparentid as int32 = 0)
dim dt as new datatable()
dt = clswebforms.gettreetable()
dim strexpression as string
strexpression = “[parentid] = “ & intparentid
dim foundrows() as datarow
foundrows = dt.select(strexpression)
dim i as integer
for i = 0 to foundrows.getupperbound(0)
dim tn as new treenode()
tn.text = foundrows(i).item(“tablename”).tostring()
tn.value = foundrows(i).item(“id”).tostring()
dim dr() as datarow
dr = dt.select(“[parentid] = “ & tn.value)
if dr.getupperbound(0) > -1 then
tn.populateondemand = true
end if
nodes.add(tn)
next
end sub
建立webform 放入treeview
protected sub page_load(byval sender as object, byval e as system.eventargs) handles me.load
if not page.ispostback then
populatenodes(treeview1.nodes, 0)
end if
end sub
protected sub treeview1_treenodepopulate(byval sender as object, byval e as system.web.ui.webcontrols.treenodeeventargs) handles treeview1.treenodepopulate
populatenodes(e.node.childnodes, e.node.value)
end sub
至于速度我没测试,如果大家有兴趣帮忙测测。
题外话(突然发现自己老了,26岁了写了快7年的程序了 一直使用vb各种版本, 及 masm(ms宏汇编语言)在vb 6.0 时代 2000 年左右建立了 以vb 编程为主的个人主页编程浪子) by shadow