1、脚本
<script language="javascript" type="text/javascript">
function client_OnTreeNodeChecked(event)
{
var obj;
if(event.srcElement!=null )
{
obj = event.srcElement;
}
else
{
obj = event.target;
}
var treeNodeFound = false;
var checkedState;
if (obj.tagName == "INPUT" && obj.type == "checkbox")
{
var treeNode = obj;
checkedState = treeNode.checked;
do
{
obj = obj.parentNode;
} while (obj.tagName != "TABLE")
var parentTreeLevel = obj.rows[0].cells.length;
//var parentTreeNode = obj.rows[0].cells[0];
var tables = obj.parentNode.getElementsByTagName("TABLE");
var numTables = tables.length
if (numTables >= 1)
{
goDeeperUnChecked(obj);
for (i=0; i < numTables; i++)
{
if (tables[i] == obj)
{
treeNodeFound = true;
i++;
if (i == numTables)
{
return;
}
}
if (treeNodeFound == true)
{
var childTreeLevel = tables[i].rows[0].cells.length;
if (childTreeLevel > parentTreeLevel)
{
var cell = tables[i].rows[0].cells[childTreeLevel - 1];
var inputs = cell.getElementsByTagName("INPUT");
inputs[0].checked = checkedState;
}
else
{
return;
}
}
}
}
}
}
function goDeeperUnChecked(obj)
{
var chk1 = false;
//Get the mom.
var head1 = obj.parentNode.previousSibling;
//no rows, cant do my work.
if(obj.rows == null)
{
return ;
}
//This is how may rows are at this level.
var pTreeLevel1 = obj.rows[0].cells.length;
//Are we a mommy?
if(head1.tagName == "TABLE")
{
//Get the list of rows ahead of us.
var tbls = obj.parentNode.getElementsByTagName("TABLE");
//get the count of that list.
var tblsCount = tbls.length;
//determine if any of the rows underneath are unchecked.
for (i=0; i < tblsCount; i++)
{
var childTreeLevel = tbls[i].rows[0].cells.length;
if (childTreeLevel = pTreeLevel1)
{
var chld = tbls[i].getElementsByTagName("INPUT");
if (chld[0].checked == false)
{
chk1 = false;
goDeeperUnChecked(head1);//递归向上判断
break;
}
}
}
var nd = head1.getElementsByTagName("INPUT");
nd[0].checked = chk1;
//do the same for the level above
goDeeperUnChecked(obj.parentElement);
}
else
{
return;
}
}
</script>
2、使用
<asp:TreeView ID="tvWebSite" οnclick="client_OnTreeNodeChecked(event);" runat="server">
<SelectedNodeStyle BackColor="Silver" ForeColor="White" />
</asp:TreeView>