asp.net TreeView 一些操作

   最近在asp.net中总是用到TreeView,有一些基本的操作,比如选中所有子节点,选中父节点之类的,每次用的时候都得找以前的代码段拷贝,挺烦的,现在就把这些放在这里,方便以后使用 ^_^
 
  1. 选中所有子节点:

None.giffunction OnTreeNodeChecked() 
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            var ele 
= window.event.srcElement; 
InBlock.gif            
if(ele.type=='checkbox'
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif
InBlock.gif                var childrenDivID 
= ele.id.replace('CheckBox','Nodes'); 
InBlock.gif                var div 
= document.getElementById(childrenDivID); 
InBlock.gif                
if(div==null)return
InBlock.gif                var checkBoxs 
= div.getElementsByTagName('INPUT'); 
InBlock.gif                
for(var i=0;i<checkBoxs.length;i++
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif
InBlock.gif                    
if(checkBoxs[i].type=='checkbox'
InBlock.gif                        checkBoxs[i].
checked=ele.checked
ExpandedSubBlockEnd.gif                }
 
ExpandedSubBlockEnd.gif            }
 
ExpandedBlockEnd.gif        }
 
 
 2. 如果要选中子节点又同时选中所有父节点:
  
None.giffunction getParentByTagName(element,tagName)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            var    parent 
= element.parentNode;
InBlock.gif            var    upperTagName 
= tagName.toUpperCase();
InBlock.gif            
while (parent && (parent.tagName.toUpperCase() != upperTagName))
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                parent 
= parent.parentNode ? parent.parentNode : parent.parentElement;
ExpandedSubBlockEnd.gif            }

InBlock.gif            
return parent;
ExpandedBlockEnd.gif        }

None.gif
None.gif        function setParentChecked(objNode)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            var    objParentDiv 
= getParentByTagName(objNode,"div");
InBlock.gif            
if(objParentDiv == null || objParentDiv == "undefined")
InBlock.gif                
return;
InBlock.gif            var    objID 
=    objParentDiv.getAttribute("ID");
InBlock.gif            var    objParentCheckBox 
=    document.getElementById(objID.replace("Nodes","CheckBox"));
InBlock.gif            
if(objParentCheckBox == null || objParentCheckBox    == "undefined")
InBlock.gif                
return;
InBlock.gif            
if(objParentCheckBox.tagName!="INPUT" && objParentCheckBox.type    == "checkbox")
InBlock.gif                
return;
InBlock.gif            objParentCheckBox.
checked =    true;
InBlock.gif            setParentChecked(objParentCheckBox);
ExpandedBlockEnd.gif        }

None.gif
None.gif
None.gif        function setChildCheckedState(div,state)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            var    objchild 
= div.childNodes;
InBlock.gif            var    count 
=    objchild.length;
InBlock.gif            
for(var    i=0;i<objchild.length;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                var    tempObj    
= objchild[i];
InBlock.gif                
if(tempObj.tagName=="INPUT"    && tempObj.type    == "checkbox")
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    tempObj.
checked    = state;
ExpandedSubBlockEnd.gif                }

InBlock.gif                setChildCheckedState(tempObj,state);
ExpandedSubBlockEnd.gif            }

ExpandedBlockEnd.gif        }

None.gif        function TreeNodeChecked()
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif{
InBlock.gif            var    objNode    
= window.event.srcElement;
InBlock.gif            
if(objNode.tagName!="INPUT"    || objNode.type!="checkbox")
InBlock.gif                
return;
InBlock.gif            
InBlock.gif            
if(objNode.checked == true)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                setParentChecked(objNode);
ExpandedSubBlockEnd.gif            }

InBlock.gif            var    objID 
=    objNode.getAttribute("ID");
InBlock.gif            var    objParentDiv 
= document.getElementById(objID.replace("CheckBox","Nodes"));
InBlock.gif            
if(objParentDiv==null || typeof(objParentDiv) == "undefined")
InBlock.gif                
return;
InBlock.gif            setChildCheckedState(objParentDiv,objNode.
checked);
InBlock.gif
ExpandedBlockEnd.gif        }

  后台的给treeview添加onclick事件处理:
 
None.gifm_Tree.Attributes.Add("onclick""TreeNodeChecked();");


3.每次只选中一个节点,相对与互斥,这个很好实现
None.giffunction TreeNodeChecked()
ExpandedBlockStart.gifContractedBlock.gif
dot.gif{
InBlock.gif  var objNode 
= window.event.srcElement;
InBlock.gif  
if(objNode.tagName!="INPUT" || objNode.type !="checkbox")
InBlock.gif  
return;
InBlock.gif  
if(window.lastCheckNode != null)
InBlock.gif  window.lastCheckNode.
checked = false;
InBlock.gif  window.lastCheckNode 
= objNode;
ExpandedBlockEnd.gif}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值