- 热门文章:
- · 怎么后台添加CheckBoxList并且得到选择结果
- · HTTP Downloader for Updater Application Block 2 (GotDotNet)
- · HOW TO:从资源管理器中拖放文件到控件
- · dataGrid在页面拖动(用表头),不刷新页面
- · winform下如何检测你一个给定的网址(Ull),是否可访问?
- · Abstract Factory pattern -- Structural example
- · XML-RPC学习笔记(一)
- · [VS宏]根据函数名称定位函数
- · 三层式的层次划分
- · 服务器控件中js脚本注册方法
- · 复制文件夹所有内容和删除整个文件夹的2个函数
- · 动态绑定DATAGRID里的DropDownList
Display hierarchical data with TreeView
Very oftenapplications have need to manage hierarchical data and indeed such thathierarchy is unlimited. This has been a bit problem in databases, althoughsolved using self-referencing tables or alternative solutions. OK, but how todisplay such data that uses self-referencing table?
WithASP.NET one optional answer is to use TreeView control. TreeView givesdevelopers chance to display hierarchical data as I’ll demonstrate in thisarticle. What makes TreeView so cool is that it can also display elements (treenodes) based on XML.
Overview ofthings that developer needs to do to get this feature are:
Get the data from self-referencing table into DataSetCreate DataRelation that corresponds to relation in self-referencing table and add it to DataSet’s Relations collection. DataRelation’s Nested property needs to be true.Get DataSet’s XML representation and apply XSLT transformation for it so that result corresponds to XML syntax used by TreeView control.Bind TreeView.
Self-referencing table
Assume thatin database we have table as follows:
CATEGORIES
CategoryID
ParentCategoryID
CategoryName
2
1
3
2
4
3
5
2
1.1
6
2
1.2
7
2
1.3
8
3
2.1
9
3
2.2
10
4
3.1
11
5
1.1.1
12
5
1.1.2
13
10
3.1.1
14
13
3.1.1.1
15
14
3.1.1.1.1
16
14
3.1.1.1.2
17
14
3.1.1.1.3
Shortly,CategoryID is the primary key and ParentCategoryID is foreign key referencingto CategoryID with default value NULL. CategoryName represents text I want todisplay in TreeView.
Get data into DataSet
//Connection to database
OleDbConnection objConn=newOleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +Server.MapPath("db.mdb") +";Persist Security Info=False");
//SQL query to get data from CATEGORIES table
OleDbCommand objCommand=newOleDbCommand("SELECT * FROM CATEGORIES",objConn);
//OleDbDataAdapter
OleDbDataAdapter objDa =newOleDbDataAdapter(objCommand);
//DataSet
DataSet ds=newDataSet("CATEGORIESDS");
//Fill DataSet
objDa.Fill(ds ,"CATEGORIES");
Nested DataRelation
//Create DataRelation
DataRelation drel=newDataRelation("CATEGORIES_RECURSIVE",
ds.Tables["CATEGORIES"].Columns["CategoryID"],
ds.Tables["CATEGORIES"].Columns["ParentCategoryID"]);
//Make sure relation is nested
drel.Nested =true;
//Add relation to DataSet@#s Relations collection
ds.Relations.Add(drel);
DataSet’s XML and XSLTtransformation [Source XML][XSLT stylesheet]
//XmlDocument to hold XML generated from DataSet
XmlDocument objDoc=new XmlDocument();
//Load XML
objDoc.LoadXml(ds.GetXml());
//Create XslTransform object
XslTransform objXSL=new XslTransform();
//Load XSLT stylesheet
objXSL.Load(Server.MapPath("transformationtemplate.xslt"));
//StringWriter to temporarily hold result of thetransformation
StringWriter writer=new StringWriter();
//Apply transformation with no arguments and dumpresults to StringWriter.
objXSL.Transform(objDoc.CreateNavigator(),null,writer);
Bind TreeView [Result of transformation]
//Set TreeView@#s TreeNodeSrc property to get XML fromStringWriter.
TreeView1.TreeNodeSrc =writer.ToString();
//Bind TreeView
TreeView1.DataBind();
//Close StringWriter
writer.Close();
Other useful resources at AspAlliance:
Programming with TreeView by Steve Sharrock
Developing with the Treeview Web Control Part 1 by James Avery
Developing with the Treeview Web Control Part 2 by James Avery
下一篇:怎么后台添加CheckBoxList并且得到选择结果 >>
相关文章:
- · 彻底解决!无效的 CurrentPageIndex 值.它必须大于等于 0 且小于 PageCount!的问题
- · HOW TO:检索本机硬件信息
- · HOW TO:检索应用程序当前执行代码的引用信息
- · HOW TO:初始化共享变量
- · 怎么触发DataGrid模板列中控件的事件?
- · 刚学ASP.Net,学了个简单的计算器
- · 全选DataGrid里的复选框
- · WEB页面多语言支持解决方案
- · javascript 与 asp.net 的交互
- · 动态模板列更新数据分页的例子
- · DataList 控件分页操作
- · 如何用请求字符串传值给用模式窗口打开的aspx页面。
- · Duwamish7学习笔记1
- · Matt Powell的《Server-Side 异步Web Methhods》
- · DataGrid和DropDownList的一些配合以及使用css定制DataGrid
- · 轻松解决asp.net论坛中的一版块多斑竹和一用户担任多版块斑竹的问题(不用数组和split)
- · 分享:aspx页面javascript的几个trick
- · 2个页面间不通过Session与url的传值方式
- · Solidworks二次开发—09--添加配合参考
- · Solidworks二次开发—08--判断是什么特征
- · 创建动态数据输入用户界面
- · 可以代替窗体Refresh方法的函数
- · 无外部控件制作多媒体播放器(四)
- · 无外部控件制作多媒体播放器(三)
- · DirectX9 3D快速上手 3
- · AlertButton, 您确定要执行吗?
- · 无外部控件制作多媒体播放器(二)
- · 用TreeView显示数据通用方法
- · 无外部控件制作多媒体播放器(一)
- · Web Browser Express 概述
- · datagrid编辑删除分页
- · 展现C# 清单5.10 生成exe文件执行的问题
- · 联通增值业务“定位之星”L1协议服务端的模拟器
- · 一个自认为写得还可以的存储过程,就是没有注释,看起来有点乱。与ERP的BOM相关的
- · [EnterpriseServices]利用assembly定义我们的组件在COM+中的注册方式
- · 给windows服务添加描述
- · 用户认证管理设计方案
- · Solidworks二次开发—07—控制草图对象
