All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
自定义阿ASP.NET,CheckBox的设计思路_.net资料_编程技术
Asp.net对文件夹和文件的操作类_[Asp.Net教程]
ASP.NET中Session的状态保持方式浅议_.net资料_编程技术
Repeater控件实例应用
跟我学SQL:(九)datetime和interval数据类型_mssql学习_编程技术
根据主键,合并gridview的行_[Asp.Net教程]
HTML网页制作基础教程(3):常用标记讲解_[Html教程]
理解PHP5中static和const关键字_php资料_编程技术
asp.net实现站点RSS功能_[Asp.Net教程]
AJAX基础:JavaScript中类的实现_[AJAX教程]
Reading number is top 10 pictures
刘亦菲写真集2
清醇靓丽的美眉
Most cow mistress ZhaoGongXia face exposure
迷人的靓女
Sora aoi in China4
Fierce! China's special forces training the devil1
NeedWallpaper8
非笑不可:最强爆笑图片精选
The money of more than 100 countries and regions3
So beauty, will let you spray blood10
Download software ranking
jdk1.5
双旗镇刀客B
C#与.NET技术平台实战演练
Prostitutes diary
徐若瑄成名作“魔鬼天使”
网络管理员第三版
尖东毒玫瑰A
小黑猫大战两米大花蛇
Boxer vs Yellow3
Eclipse 4.2.1 For Win32
delv published in(发表于) 2014/1/24 9:03:30 Edit(编辑)
asp.net2.0,treeview控件用数据表做为数据源实现n级动态菜单_[Asp.Net教程]

asp.net2.0,treeview控件用数据表做为数据源实现n级动态菜单_[Asp.Net教程]

asp.net2.0 treeview控件用数据表做为数据源实现n级动态菜单_[Asp.Net教程]

最近做个产品展示网站,本来打算treeview和menu控件数据源用sitemap来实现,但是发现新增产品信息的发布页需要有个dropdownlist来读取原有的菜单分类,想不出ddl和sitemap如何关联,所以还是改用数据库做为数据源了,参考了网上一些资料,终于实现treeview的n级动态菜单,写下来做个记录。

写一个递归函数。

vb代码:

程序代码

Public Sub PopulateNodes(ByVal nodes As TreeNodeCollection, Optional ByVal intParentID As Int32 = 0)
Dim showmenu As New ClassLibrary.Jkdata.JkDataBasic ‘创建一个新的类对象
Dim dt As New DataTable()
dt = showmenu.GetProduct_Class ‘获得业务层返回的datatable
Dim strExpression As String
strExpression = "[UpID] = " & intParentID
Dim foundRows() As DataRow
foundRows = dt.Select(strExpression)
Dim I As Integer
Dim PassID As Integer
For I = 0 To foundRows.GetUpperBound(0)
Dim tn As New TreeNode()
tn.Text = foundRows(I).Item("ClassName").ToString()
tn.Value = foundRows(I).Item("ID").ToString()
If foundRows(I).Item("UpID") = 0 Then
tn.NavigateUrl = foundRows(I).Item("Url").ToString‘父分类的页面连接
Else
PassID = Convert.ToInt32(foundRows(I).Item("ID"))
tn.NavigateUrl = foundRows(I).Item("Url") + "?classid=" + PassID.ToString ‘子分类的页面连接含参数传递

End If

Dim dr() As DataRow
dr = dt.Select("[UpID] = " & tn.Value)
If dr.GetUpperBound(0) > -1 Then
tn.PopulateOnDemand = True
End If
nodes.Add(tn)
Next
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


Page_Load事件中调用函数绑定数据:

程序代码

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



aspx页面:

程序代码






数据库字段:

ID 自动编号
UpID 数字
ClassName 文本
Url 文本

最后效果图:



来源:http://www.morock.net/blog/article.asp?id=76







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.