Asp.net 2.0的TreeView客户端个性化控制

ASP.NET 2.0 的 TreeView  控件功能虽说强大,但其客户端控制很逊色,本文将讲解 TreeView 的客户端实现原理,并实现两个个性化操作:

  

(1) 节点的全部打开和关闭;

TreeNode Expand(or Collapse) all

(2) 只打开一个节点(关闭其他兄弟节点)。

just one node expanded(when a client expand one node all other will collaps)

用记事本打开页面源代码,可以找到一下两个脚本引用:

 

 

< script src = " /WebUI/WebResource.axd?d=RAQeBcDUNuP9iuS8q3tNEw2&amp;t=633300220640000000 "  type = " text/javascript " >
< / script>
 
< script src = " /WebUI/WebResource.axd?d=JuTdJhq3NM8Jq_RhssAkEg2&amp;t=633300220640000000 "  type = " text/javascript " >
< / script>

 

将"/WebUI/WebResource.axd?d=RAQeBcDUNuP9iuS8q3tNEw2&t=633300220640000000"拷到地址栏尾,下载脚本,并以 .js 命名,另一个同样操作。分析第二个脚本文件,可以看到TreeView的很多客户端函数,其中关键的一个 TreeView_ToggleNode 就是客户端点击时触发的事件。

  要想做个性化的操作,就得从 TreeView_ToggleNode 事件下手。我们无法更改.net封装好的脚本,只有“重写”。所谓的重写就是在原来的函数之后添加一个同名函数(因为js对于同名函数只调用最后一个)。

 

  TreeView_ToggleNode 的原函数:

 

 

 

ExpandedBlockStart.gif ContractedBlock.gif function  TreeView_ToggleNode(data, index, node, lineType,children)
  
var img = node.childNodes[0]; 
  
var newExpandState; 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
try
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if (children.style.display == "none")
  children.style.display 
= "block"
  newExpandState 
= "e"
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if ((typeof(img) != "undefined"&& (img != null))
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if (lineType == "l")
  img.src 
= data.images[15]; 
  }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
else if (lineType == "t")
  img.src 
= data.images[12]; 
  }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
else if (lineType == "-")
  img.src 
= data.images[18]; 
  }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
else
  img.src 
= data.images[5]; 
  }
 
  img.alt 
= data.collapseToolTip.replace(//, TreeView_GetNodeText(node)); 
  }
 
  }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
else
  children.style.display 
= "none"
  newExpandState 
= "c"
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if((typeof(img) != "undefined"&& (img != null))
ExpandedSubBlockStart.gifContractedSubBlock.gif  
if(lineType == "l")
  img.src 
= data.images[14]; 
  }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
else if(lineType == "t")
  img.src 
= data.images[11]; 
  }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
else if(lineType == "-")
  img.src 
= data.images[17]; 
  }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
else
  img.src 
= data.images[4]; 
  }
 
  img.alt 
= data.expandToolTip.replace(//, TreeView_GetNodeText(node)); 
  }
 
  }
 
  }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif  
catch(e){} 
  data.expandState.value 
= data.expandState.value.substring(0, index) + newExpandState + data.expandState.value.slice(index + 1); 
  }

 

   1. 节点的全部打开和关闭:

 

 

< html xmlns = " http://www.w3.org/1999/xhtml " >  
< head runat = " server " >  
    
< title > LeftMenu_Tree < / title> 
< / head> 
< body bgcolor = " #DDEDFD " >  
    
< form id = " form1 "  runat = " server " >  
        
< a href = " javascript:TreeviewExpandCollapseAll('<%=TreeView1.ClientID%>', true) " > Expand 
            All
< / a> <a href="javascript:TreeviewExpandCollapseAll('<%=TreeView1.ClientID%>', false)"> 
                Collapse All < / a> 
         < asp:TreeView ID = " TreeView1 "  SkinID = " tvClass "  runat = " server "  ShowLines = " true "  NodeWrap = " false " >  
        
< / asp:TreeView> 
     < / form> 
< / body> 
< / html> 

< script language = " javascript " >  
// ----------------------------------------------------------------------------- 
function  $(s) 
function  isNull(_sVal) 

function  TreeviewExpandCollapseAll(treeViewId, expandAll) 
ExpandedBlockStart.gifContractedBlock.gif

    
var displayState = (expandAll == true ? "none" : "block"); 
    
var treeView = $(treeViewId); 
    
if(treeView) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    

        
var treeLinks = treeView.getElementsByTagName("a"); 
        
var nodeCount = treeLinks.length; 
        
var flag = true
        
for(i=0;i<nodeCount;i++
ExpandedSubBlockStart.gifContractedSubBlock.gif        

            
if(treeLinks[i].firstChild.tagName) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            

                
if(treeLinks[i].firstChild.tagName.toLowerCase() == "img"
ExpandedSubBlockStart.gifContractedSubBlock.gif                

                    
var node = treeLinks[i]; 
                    
var level = parseInt(node.id.substr(node.id.length - 1),10); 
                    
var childContainer = GetParentByTagName("table", node).nextSibling; 
                    
if(!isNull(childContainer)) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                    

                        
if(flag) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                        

                            
if(childContainer.style.display == displayState) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                            

                                TreeView_ToggleNode(eval(treeViewId 
+"_Data"),level,node,'r',childContainer); 
                            }
 
                            flag 
= false
                        }
 
                        
else 
ExpandedSubBlockStart.gifContractedSubBlock.gif                        

                            
if(childContainer.style.display == displayState) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                            

                                TreeView_ToggleNode(eval(treeViewId 
+"_Data"),level,node,'l',childContainer); 
                            }
 
                        }
 
                    }
 
                }
 
            }
 
        }
//for loop ends 
    }
 
}
 

function  GetParentByTagName(parentTagName, childElementObj) 
ExpandedBlockStart.gifContractedBlock.gif

    
var parent = childElementObj.parentNode; 
    
while(parent.tagName.toLowerCase() != parentTagName.toLowerCase()) 
ExpandedSubBlockStart.gifContractedSubBlock.gif    

        parent 
= parent.parentNode; 
    }
 
    
return parent; 
}
 
// ----------------------------------------------------------------------------- 
< / script>

 

2. 只打开一个节点(关闭其他兄弟节点)

 

 

< html xmlns = " http://www.w3.org/1999/xhtml " >  
< head runat = " server " >  
    
< title > LeftMenu_Tree < / title> 
< / head> 
< body bgcolor = " #DDEDFD " >  
    
< form id = " form1 "  runat = " server " >  
        
< a href = " javascript:TreeviewExpandCollapseAll('<%=TreeView1.ClientID%>', true) " > Expand 
            All
< / a> , <a href="javascript:TreeviewExpandCollapseAll('<%=TreeView1.ClientID%>', false)"> 
                Collapse All < / a> 
         < asp:TreeView ID = " TreeView1 "  SkinID = " tvClass "  runat = " server "  ShowLines = " true "  NodeWrap = " false " >  
        
< / asp:TreeView> 
     < / form> 
< / body> 
< / html> 

< script language = " javascript " >  
// 2. 只打开一个节点(关闭其他兄弟节点)------------------------------------------ 
function  TreeView_ToggleNode(data, index, node, lineType, children)  
ExpandedBlockStart.gifContractedBlock.gif

    
var img = node.childNodes[0]; 
    
var newExpandState; 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
try 
        
//***折叠兄弟节点------------------------ 
        CollapseBrothers(data,children); 
        
//--------------------------------------- 
             
        
if (children.style.display == "none")  
ExpandedSubBlockStart.gifContractedSubBlock.gif        

            children.style.display 
= "block"
            newExpandState 
= "e"
            
if ((typeof(img) != "undefined"&& (img != null))  
ExpandedSubBlockStart.gifContractedSubBlock.gif            

                
if (lineType == "l")  
ExpandedSubBlockStart.gifContractedSubBlock.gif                

                    img.src 
= data.images[15]; 
                }
 
                
else if (lineType == "t")  
ExpandedSubBlockStart.gifContractedSubBlock.gif                

                    img.src 
= data.images[12]; 
                }
 
                
else if (lineType == "-")  
ExpandedSubBlockStart.gifContractedSubBlock.gif                

                    img.src 
= data.images[18]; 
                }
 
                
else  
ExpandedSubBlockStart.gifContractedSubBlock.gif                

                    img.src 
= data.images[5]; 
                }
 
                img.alt 
= data.collapseToolTip.replace(//, TreeView_GetNodeText(node)); 
            }
 
        }
 
        
else  
ExpandedSubBlockStart.gifContractedSubBlock.gif        

            children.style.display 
= "none"
            newExpandState 
= "c"
            
if ((typeof(img) != "undefined"&& (img != null))  
ExpandedSubBlockStart.gifContractedSubBlock.gif            

                
if (lineType == "l")  
ExpandedSubBlockStart.gifContractedSubBlock.gif                

                    img.src 
= data.images[14]; 
                }
 
                
else if (lineType == "t")  
ExpandedSubBlockStart.gifContractedSubBlock.gif                

                    img.src 
= data.images[11]; 
                }
 
                
else if (lineType == "-")  
ExpandedSubBlockStart.gifContractedSubBlock.gif                

                    img.src 
= data.images[17]; 
                }
 
                
else  
ExpandedSubBlockStart.gifContractedSubBlock.gif                

                    img.src 
= data.images[4]; 
                }
 
                img.alt 
= data.expandToolTip.replace(//, TreeView_GetNodeText(node)); 
            }
 
        }
 
    }
 
ExpandedSubBlockStart.gifContractedSubBlock.gif    
catch(e) {} 
    data.expandState.value 
=  data.expandState.value.substring(0, index) + newExpandState + data.expandState.value.slice(index + 1); 
}
 

// 折叠兄弟节点 
function  CollapseBrothers(data,childContainer) 
ExpandedBlockStart.gifContractedBlock.gif

    
var parent = childContainer.parentNode;    
    
for(i=0; i< parent.childNodes.length; i++
ExpandedSubBlockStart.gifContractedSubBlock.gif    

        
if(parent.childNodes[i].tagName.toLowerCase() =="div"
ExpandedSubBlockStart.gifContractedSubBlock.gif        

            
if(parent.childNodes[i].id != childContainer.id) 
ExpandedSubBlockStart.gifContractedSubBlock.gif            

                parent.childNodes[i].style.display 
= "none" 
            }
 
        }
 
        
else if(parent.childNodes[i].tagName.toLowerCase() =="table"
ExpandedSubBlockStart.gifContractedSubBlock.gif        

            
var treeLinks = parent.childNodes[i].getElementsByTagName("a");             
            
if(treeLinks.length > 2
ExpandedSubBlockStart.gifContractedSubBlock.gif            

                
var j=0
                
if(treeLinks[j].firstChild.tagName) 
ExpandedSubBlockStart.gifContractedSubBlock.gif                

                    
if(treeLinks[j].firstChild.tagName.toLowerCase() == "img"
ExpandedSubBlockStart.gifContractedSubBlock.gif                    

                        
var img = treeLinks[j].firstChild; 
                        
if(i==0)  
                            img.src 
= data.images[8]; 
                        
else if(i==parent.childNodes.length-2)  
                            img.src 
= data.images[14]; 
                        
else  
                            img.src 
= data.images[11]; 
                    }
 
                }
     
            }
 
        }
         
    }
 
}
 
// ----------------------------------------------------------------------------- 
< / script>

 

 

 

转载于:https://www.cnblogs.com/mFrog/archive/2008/07/24/1250511.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值