用户操作
[即时聊天] [发私信] [加为好友]
阿呆ID:daqingshu
22654次访问,排名5249(-1)好友0人,关注者0
daqingshu的文章
原创 14 篇
翻译 0 篇
转载 23 篇
评论 5 篇
阿呆的公告
还是喜欢那一片幽静 致远的地方 真的好舒服吗? 等到我去的时候 你还是那样美丽吗?
Q俺 Free Counter
Free Counter
最近评论
hero19851205:又是和别人一样的东西
文章分类
收藏
相册
Blogs
存档
软件项目交易
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
订阅到BlogLines
订阅到Yahoo
订阅到GouGou
订阅到飞鸽
订阅到Rojo
订阅到newsgator
订阅到netvibes

转载 使用 asp.net 2.0 自带的TreeView控件显示内联表数据(树状数据)收藏

新一篇: 给数据库自增字段指定值 | 旧一篇: visual studio 2005小tip之自动添加文件模版信息

 原文 Display Hierarchical Data with TreeView in ASP.NET 2.0
不同的 应用有不同的很多种方法
数据库结构如下

控件

<asp:TreeView  
  ID="TreeView1"
  ExpandDepth="0"
  PopulateNodesFromClient="true"
  ShowLines="true"
  ShowExpandCollapse="true"
  runat="server" />
后端代码
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  If Not Page.IsPostBack Then
    PopulateRootLevel()
  End If
End Sub
 
Private Sub PopulateRootLevel()
  Dim objConn As New SqlConnection(_
    "server=JOTEKE\SQLExpress;Trusted_Connection=true;DATABASE=TreeViewSampleDB")
  Dim objCommand As New SqlCommand("select id,title,(select count(*) FROM SampleCategories " _
    & "WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID IS NULL", _
    objConn)
 
  Dim da As New SqlDataAdapter(objCommand)
  Dim dt As New DataTable()
  da.Fill(dt)
 
  PopulateNodes(dt, TreeView1.Nodes)
End Sub
Private Sub PopulateNodes(ByVal dt As DataTable, _
  ByVal nodes As TreeNodeCollection)
  For Each dr As DataRow In dt.Rows
    Dim tn As New TreeNode()
    tn.Text = dr("title").ToString()
    tn.Value = dr("id").ToString()
    nodes.Add(tn)
 
    'If node has child nodes, then enable on-demand populating
    tn.PopulateOnDemand = (CInt(dr("childnodecount")) > 0)
  Next
End Sub

Private Sub PopulateSubLevel(ByVal parentid As Integer, _
  ByVal parentNode As TreeNode)
  Dim objConn As New SqlConnection(_
    "server=JOTEKE\SQLExpress;Trusted_Connection=true;DATABASE=TreeViewSampleDB")
  Dim objCommand As New SqlCommand("select id,title,(select count(*) FROM SampleCategories " _
    & "WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID=@parentID", _
    objConn)
  objCommand.Parameters.Add("@parentID", SqlDbType.Int).Value = parentid
 
  Dim da As New SqlDataAdapter(objCommand)
  Dim dt As New DataTable()
  da.Fill(dt)
  PopulateNodes(dt, parentNode.ChildNodes)
End Sub

这个是树节点展开事件代码
Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, _
  ByVal e As System.Web.UI.WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate
  PopulateSubLevel(CInt(e.Node.Value), e.Node)
End Sub

发表于 @ 2007年04月14日 17:39:00|评论(loading...)|编辑

新一篇: 给数据库自增字段指定值 | 旧一篇: visual studio 2005小tip之自动添加文件模版信息

评论:没有评论。

发表评论  


当前用户设置只有注册用户才能发表评论。如果你没有登录,请点击登录
Csdn Blog version 3.1a
Copyright © 阿呆