这是我给客户的一个解决方案,为了节约时间,我直接贴可以跑到代码,同时没有写注释,希望谅解,
Code
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>TreeView Demo</title>
<script language="javascript" type="text/javascript">
function TreeviewExpandCollapseAll(treeViewId, expandAll) {
var displayState = (expandAll == true ? "none" : "block");
var treeView = document.getElementById(treeViewId);
if (treeView) {
var treeLinks = treeView.getElementsByTagName("a");
var nodeCount = treeLinks.length;
for (i = 0; i < nodeCount; i++) {
if (treeLinks[i].firstChild.tagName) {
if (treeLinks[i].firstChild.tagName.toLowerCase() == "img") {
var currentToggleLink = treeLinks[i];
var childContainer = GetParentByTagName("table", currentToggleLink).nextSibling;
if (childContainer.style.display == displayState) {
eval(currentToggleLink.href);
}
}
}
}
}
}
function GetParentByTagName(parentTagName, childElementObj) {
var parent = childElementObj.parentNode;
while (parent.tagName.toLowerCase() != parentTagName.toLowerCase()) {
parent = parent.parentNode;
}
return parent;
} </script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeViewDemo" runat="server" ExpandDepth="1">
<Nodes>
<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A" Value="A">
<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A1" Value="A1">
<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A11" Value="A11"></asp:TreeNode>
<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A12" Value="A12"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A2" Value="A2">
<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A21" Value="A21"></asp:TreeNode>
<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A22" Value="A22"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
<a href="javascript:TreeviewExpandCollapseAll('<%=TreeViewDemo.ClientID%>', true)">Expand
All</a> <a href="javascript:TreeviewExpandCollapseAll('<%=TreeViewDemo.ClientID%>', false)">
Collapse All</a>
</div>
</form>
</body>
</html>
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>TreeView Demo</title>
<script language="javascript" type="text/javascript">
function TreeviewExpandCollapseAll(treeViewId, expandAll) {
var displayState = (expandAll == true ? "none" : "block");
var treeView = document.getElementById(treeViewId);
if (treeView) {
var treeLinks = treeView.getElementsByTagName("a");
var nodeCount = treeLinks.length;
for (i = 0; i < nodeCount; i++) {
if (treeLinks[i].firstChild.tagName) {
if (treeLinks[i].firstChild.tagName.toLowerCase() == "img") {
var currentToggleLink = treeLinks[i];
var childContainer = GetParentByTagName("table", currentToggleLink).nextSibling;
if (childContainer.style.display == displayState) {
eval(currentToggleLink.href);
}
}
}
}
}
}
function GetParentByTagName(parentTagName, childElementObj) {
var parent = childElementObj.parentNode;
while (parent.tagName.toLowerCase() != parentTagName.toLowerCase()) {
parent = parent.parentNode;
}
return parent;
} </script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView ID="TreeViewDemo" runat="server" ExpandDepth="1">
<Nodes>
<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A" Value="A">
<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A1" Value="A1">
<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A11" Value="A11"></asp:TreeNode>
<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A12" Value="A12"></asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A2" Value="A2">
<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A21" Value="A21"></asp:TreeNode>
<asp:TreeNode NavigateUrl="http://www.asp.net" Text="A22" Value="A22"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
<a href="javascript:TreeviewExpandCollapseAll('<%=TreeViewDemo.ClientID%>', true)">Expand
All</a> <a href="javascript:TreeviewExpandCollapseAll('<%=TreeViewDemo.ClientID%>', false)">
Collapse All</a>
</div>
</form>
</body>
</html>
刚刚从网上搜索我的文章时候,看到另外个文章和我写的差不多,故我也把那里的代码贴过来共大家参考:
Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="jstree.aspx.cs" Inherits="jstree" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function expandAll(treeViewId)
{
var treeView = document.getElementById(treeViewId);
var treeLinks = treeView.getElementsByTagName("a");
var j = true;
for(i=0;i<treeLinks.length;i++)
{
if(treeLinks[i].firstChild.tagName == "IMG")
{
var node = treeLinks[i];
var level = parseInt(treeLinks[i].id.substr(treeLinks[i].id.length - 1),10);
var childContainer = document.getElementById(treeLinks[i].id + "Nodes");
if(j)
{
if(childContainer.style.display == "none")
TreeView_ToggleNode(eval(treeViewId +"_Data"),level,node,'r',childContainer);
j = false;
}
else
{
if(childContainer.style.display == "none")
TreeView_ToggleNode(eval(treeViewId +"_Data"),level,node,'l',childContainer);
}
}
}
}
function collapseAll(treeViewId)
{
var treeView = document.getElementById(treeViewId);
var treeLinks = treeView.getElementsByTagName("a");
var j = true;
for(i=0;i<treeLinks.length;i++)
{
if(treeLinks[i].firstChild.tagName == "IMG")
{
var node = treeLinks[i];
var level = parseInt(treeLinks[i].id.substr(treeLinks[i].id.length - 1),10);
var childContainer = document.getElementById(treeLinks[i].id + "Nodes");
if(j)
{
if(childContainer.style.display == "block")
TreeView_ToggleNode(eval(treeViewId +"_Data"),level,node,'r',childContainer);
j = false;
}
else
{
if(childContainer.style.display == "block")
TreeView_ToggleNode(eval(treeViewId +"_Data"),level,node,'l',childContainer);
}
}
}
}
function Button1_onclick() {
expandAll('<%=TreeView1.ClientID%>');
}
function Button2_onclick() {
collapseAll('<%=TreeView1.ClientID%>');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:TreeView ID="TreeView1" runat="server">
<Nodes>
<asp:TreeNode Text="New Node" Value="New Node">
<asp:TreeNode Text="New Node" Value="New Node">
<asp:TreeNode Text="New Node" Value="New Node">
<asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
<asp:TreeNode Text="New Node" Value="New Node">
<asp:TreeNode Text="New Node" Value="New Node">
<asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
<asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
<asp:TreeNode Text="New Node" Value="New Node">
<asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
</form>
<p>
<input id="Button1" type="button" value="Expand All" onclick="return Button1_onclick()" /></p>
<p>
<input id="Button2" type="button" value="Collapse All" onclick="return Button2_onclick()" /></p>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="jstree.aspx.cs" Inherits="jstree" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<script type="text/javascript">
function expandAll(treeViewId)
{
var treeView = document.getElementById(treeViewId);
var treeLinks = treeView.getElementsByTagName("a");
var j = true;
for(i=0;i<treeLinks.length;i++)
{
if(treeLinks[i].firstChild.tagName == "IMG")
{
var node = treeLinks[i];
var level = parseInt(treeLinks[i].id.substr(treeLinks[i].id.length - 1),10);
var childContainer = document.getElementById(treeLinks[i].id + "Nodes");
if(j)
{
if(childContainer.style.display == "none")
TreeView_ToggleNode(eval(treeViewId +"_Data"),level,node,'r',childContainer);
j = false;
}
else
{
if(childContainer.style.display == "none")
TreeView_ToggleNode(eval(treeViewId +"_Data"),level,node,'l',childContainer);
}
}
}
}
function collapseAll(treeViewId)
{
var treeView = document.getElementById(treeViewId);
var treeLinks = treeView.getElementsByTagName("a");
var j = true;
for(i=0;i<treeLinks.length;i++)
{
if(treeLinks[i].firstChild.tagName == "IMG")
{
var node = treeLinks[i];
var level = parseInt(treeLinks[i].id.substr(treeLinks[i].id.length - 1),10);
var childContainer = document.getElementById(treeLinks[i].id + "Nodes");
if(j)
{
if(childContainer.style.display == "block")
TreeView_ToggleNode(eval(treeViewId +"_Data"),level,node,'r',childContainer);
j = false;
}
else
{
if(childContainer.style.display == "block")
TreeView_ToggleNode(eval(treeViewId +"_Data"),level,node,'l',childContainer);
}
}
}
}
function Button1_onclick() {
expandAll('<%=TreeView1.ClientID%>');
}
function Button2_onclick() {
collapseAll('<%=TreeView1.ClientID%>');
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:TreeView ID="TreeView1" runat="server">
<Nodes>
<asp:TreeNode Text="New Node" Value="New Node">
<asp:TreeNode Text="New Node" Value="New Node">
<asp:TreeNode Text="New Node" Value="New Node">
<asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
<asp:TreeNode Text="New Node" Value="New Node">
<asp:TreeNode Text="New Node" Value="New Node">
<asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
<asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
<asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
<asp:TreeNode Text="New Node" Value="New Node">
<asp:TreeNode Text="New Node" Value="New Node"></asp:TreeNode>
</asp:TreeNode>
</asp:TreeNode>
</Nodes>
</asp:TreeView>
</form>
<p>
<input id="Button1" type="button" value="Expand All" onclick="return Button1_onclick()" /></p>
<p>
<input id="Button2" type="button" value="Collapse All" onclick="return Button2_onclick()" /></p>
</body>
</html>
这是第二段代码的出处:http://www.cnblogs.com/blodfox777/archive/2008/10/02/1303119.html
这个是我写文章出处:http://forums.asp.net/p/1355481/2779144.aspx
如果你有什么算法,也请共享出来,谢谢了先!