【原】ASP.NET TreeView 的那点事 【给你七棵大树——TreeView大总结——有源码】

本来打算过些日子总结树的,看看最近几个网友发消息(邮件)问我相关树方面的问题,今天我没有休息,花了3个多小时总结我们在使用树中经常遇到的问题,好了下面就让我们来看看这些树吧!

第一棵:使用AJAX 按需求建立【一棵】树 Building Tree View on demand using AJAX

http://www.cnblogs.com/OceanChen/archive/2009/02/07/1385664.html

这篇文章简单告诉我们如何使用AJAX异步生成树,且数据源为XML,看看截图,是不是很酷?当然你还可以在链接上下载源代码!

 

文章出去: http://www.codeproject.com/KB/aspnet/TreeViewAjax.aspx

 

第二棵:用使用JavaScript展开/折叠TreeView中所有节点(Expand and Collapse All Nodes of asp.net Treeview on the client with javascript)

http://www.cnblogs.com/OceanChen/archive/2009/02/07/1385662.html

这是我给客户的一个解决方案,为了节约时间,我直接贴可以跑到代码,同时没有写注释,希望谅解,

Code

刚刚从网上搜索我的文章时候,看到另外个文章和我写的差不多,故我也把那里的代码贴过来共大家参考:

Code

 

这是第二段代码的出处:http://www.cnblogs.com/blodfox777/archive/2008/10/02/1303119.html

这个是我写文章出处:http://forums.asp.net/p/1355481/2779144.aspx

第三棵:Microsfot.Web.UI.WebControls.TreeView JavaScript控制方法研究

http://www.cnblogs.com/OceanChen/archive/2009/02/07/1385656.html 

  1. 被选择的节点的索引:
    tree.selectedNodeIndex

  2. 被单击的节点的索引:
    tree.clickedNodeIndex

  3. 获取一个节点:
    tree.getTreeNode(nodeIndex)

  4. 在根节点下增加一个子节点:
    var tree = document.all['TreeView1'];
    var node = tree.createTreeNode() ;
    tree.add(node);
    node.setAttribute( "text", "aaaa");

  5. 在当前节点下增加一个子节点:
    var tree = document.all['TreeView1'];
    var node = tree.createTreeNode() ;
    var parentNode = tree.getTreeNode( tree.clickedNodeIndex);
    parentNode.add(node);
    node.setAttribute( "text", "aaaa");

  6. 通过XML文件来增加子节点:
    node.setAttribute("NavigateUrl","xxx");
    node.databind();
  7. 动态增加子节点后自动展开:
    node.setAttribute('expanded', 'true');//MS提供的HTC中需要修改一个地方,否则就会产生一个异常
  8. 获取节点的属性:
    node.getAttribute("xxx")
  9. 设置节点的属性 :
    node.setAttribute('xxx', 'xxxx');
  10. 常用属性列表:
    属性 含义
    CheckBox True False 是否有选择框
    checked True False 选择框是否被选中
    Expanded True False 是否展开
    ImageURL   正常状态下左边的图标
    SelectedImageUrl   当节点被选择时左边的图标
    ExpandedImageUrl   当节点被展开后左边的图标
    Target   目标框架
    navigateurl   目标URL
    type   节点的类型
    childtype   子节点的类型
    Text   节点显示的文本
    innerText    
    innerHTML    
    defaultstyle   默认的风格
    hoverstyle   当鼠标移到节点的上面时的风格
    selectedstyle   当节点被选择时的风格
    treenodesrc    
  11. 获取父节点:
    node.getParent()
  12. 获取子节点:
    node.getChildren()
  13. 判断节点是否有子节点
    node.getChildren().length > 0
  14. 响应onselectedindexchange事件:
    var tree = document.all["tvMain"];
    tree.attachEvent("onselectedindexchange", SelectedIndexChange);

原文出处:http://www.cnblogs.com/caidaoli/archive/2005/02/23/108317.html

第四棵:在ASP.NET 2.0 中如何使用多个Sitemap文件【Using Multiple Sitemap Files in ASP.NET 2.0--英文】

http://www.cnblogs.com/OceanChen/archive/2009/02/06/1385200.html 

请直接参考链接先:

http://www.codeproject.com/KB/aspnet/MutlipleSiteMap.aspx【英文的——有代码下载】

通过这个,我们就可以很容易给特定用户看特定的站点导航了。

参考部分代码:

sitemap

 

如果再加上这篇,何愁网站人性化导航不破?

【原】根据网站动态目录生成树结构,并用TreeView输出【提供源码下载,有详细注释】 

http://www.cnblogs.com/OceanChen/archive/2009/02/05/1384816.html

第五棵:根据网站动态目录生成树结构,并用TreeView输出【提供源码下载,有详细注释】

http://www.cnblogs.com/OceanChen/archive/2009/02/05/1384816.html

各位博友好,本着共享的原则,本人花了点时间写了个这棵树,虽然简单,但是应该很实用。

我知道我们可以使用 Web.sitemap 来实现,但是用Web.sitemap 需要预先写好配置,我写这个就不需要考虑配置了。

比如说你的目录改变了,那么自动生成树也会跟着变动,下面贴代码:

MyWebsiteTreeView.aspx

 

MyWebsiteTreeView.aspx.cs

 

具体应用,请在下面的这个链接上下载整个应用项目示例:

http://www.cnblogs.com/OceanChen/archive/2009/02/04/1383794.html

第六棵:给TreeView 加个确认导航功能(如虎添翼?还是画蛇添足?)【可用代码,且有注释】

http://www.cnblogs.com/OceanChen/archive/2009/02/05/1384439.html

最近回答客户问题时,遇到这样的问题【人啊,想法就是多!】,说是要给TreeView 导航加个导航确认功能,开始看那个英文啊,好别扭!经过仔细问问,终于知道他到底要什么,然后自己就Google啊,可是不行,没有现成的,然后没有办法,我个人认为我的JS功底是一等一 Js还算可以【高人很多,不能放肆,谢谢高人指点】的,写个给他把,果然客户觉得爽,所以我觉得有必要公布出来供大家参考,本啦注释是E文的,我怕有些初学者不识E文,就修改注释为中文的了,发扬我一贯作风,贴可以跑起来的代码:

TreeView.aspx

 

如果你觉得有更好的办法,请多多赐教,谢谢了先。

第七棵:父子树的选择【全选/不全选】Asp.Net 2.0 Treeview Checkbox Check/Uncheck All script

http://www.cnblogs.com/OceanChen/archive/2009/01/14/1375707.html

为了使您更好的理解,我简单用中文叙述一下,这个是一棵好树!

这棵树主要解决这样一个场景:我们想,如果我们选择父节点,然后要求父节点下的所有子节点都被选中,我们肯定知道如何做,就是找到父节点,然后循环选择其子节点,这个是不是很好实现啊?呵呵,我想你的回答肯定是没有问题,可是现在增加两个小小的功能:

1. 如果任何一个子节点被反选了,要求这个子节点对应的父节点也被反选了;

2. 如果所有子节点都被选中了,要求与这些子节点对应的父节点也被选中。

怎么样难度很大吧,不要怕,这篇文章就是专门来解决上述两个问题的,好了,废话少说,给大伙来点实际的。

嗯,建议大伙学点英文,^_^,下面就交个您了:

Its a very common requirement to have the parent-child check behaviour in asp.net treeview. To define the problem:
1)Check all the child nodes if the parent is checked and uncheck all child nodes if parent is unchecked ( well, this part is simple).
2)If a node at any level is checked and all its siblings are already checked then the parent node should be checked and the same should apply for the parent node(i.e., if its siblings are checked….), this should happen till the root node.
3)If a node at any level is unchecked then the parents( ma, grand ma, grand grand ma….) up to the root node must be unchecked.

Well there have been scripts on the net that only half accomplished the task(check footnotes). So I wrote the script that solves the problem completely, upto best of my knowledge. I’ve tested in IE 7 and Firefox 2.0, hope it works fine for you all.

Here’s how to implement it:

Step 1: In the page load in code behind file add an attribute to the treeview as:

 

Code
If(!isPostBack)
{
TreeView1.Attributes.Add(”onclick”,”OnTreeClick(
event
)”);
}

 

The desired affect could also be accomplished by direclty adding the attribute to the treeview tag in .aspx file as: <asp:treeview onclick=”OnTreeClick(event)”… which would cause Visual Studio to display a warning but it works anyway but the codebehind way of doint it is the right way.

Step 2: Put the below script in the head section of your .aspx page:

 

Code

 

//utility function to get the container of an element by tagname
function GetParentByTagName(parentTagName, childElementObj)
{
var parent = childElementObj.parentNode;
while(parent.tagName.toLowerCase() != parentTagName.toLowerCase())
{
parent = parent.parentNode;
}
return parent;
}

The script is pretty much self explanatory with function names saying it all.
Comments awaited.

Now, see the whole code,

 

Code

可以参考:http://forums.asp.net/t/1367074.aspx

 

这七棵树中其中有一棵不是树,但是如果作为数据源绑定到树上,那么就和树联系起来了。那么到底那棵不是呢?这个是我留给您的家庭作业! 

为了总结方便,有些树我就直接从别处转载了,且一一注明出处了。希望您能从这七棵树得到你想要的东西!同时如果你要更好的意见或建议,请与我联系!由于水平有限,可能会有疏忽之处,请多多指教,共同探讨,共同进步。

posted @ 2009-02-07 03:39  海洋——海纳百川,有容乃大.  阅读(3993)  评论(10编辑  收藏  举报