日期:2014-05-19  浏览次数:20874 次

怎样用脚本语言javascript获得treeview(vs2005)中选定节点叶子的所有值,并且不刷新页面
我要把选定节点的叶子所有值加到listbox中,如何做?
马上给分

------解决方案--------------------
可以参考下面的执行。下面的文件直接存成tree.aspx,可以运行,看效果

<%@ Page Language= "C# " %>
<script runat= "server ">
protected void Page_Load(object sender, EventArgs e)
{
TreeView1.Attributes.Add( "onclick ", "OnClientTreeNodeChecked(event) ");
}
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Form[ "CheckedNode "] != null)
{
string CheckedNodeValue = Request.Form[ "CheckedNode "];
if (CheckedNodeValue == " ")
return;

if (CheckedNodeValue.EndsWith( ", "))
CheckedNodeValue = CheckedNodeValue.TrimEnd( ', ');

Response.Write( "您选择的节点有: "+CheckedNodeValue);
}
}
</script>

<html xmlns= "http://www.w3.org/1999/xhtml ">
<head runat= "server ">
<title> 客户端实现全选子节点 </title>
<script type= "text/javascript " language= "javascript ">
//识别不同的浏览器
function getTargetElement(evt) {
var elem
if (evt.target)
{
elem = (evt.target.nodeType == 3) ? evt.target.parentNode : evt.target
}
else
{
elem = evt.srcElement
}
return elem
}

function OnClientTreeNodeChecked(evt)
{
evt = (evt) ? evt : ((window.event) ? window.event : " ");
if(evt == " ")
{
return;
}
var obj = getTargetElement(evt);
var hasTreeNode = false;
if(obj.tagName)
{
if (obj.tagName == "INPUT " && obj.type == "checkbox ") {
var treeNode = obj;
while (obj.tagName != "TABLE ")
{
obj = obj.parentNode;
}
var parentTreeDeep = obj.rows[0].cells.length;
var parentTreeNode = obj.rows[0].cells[0];
var oDiv = obj.parentNode;
if( null == oDiv )
{
return;
}
if( 1 != oDiv.nodeType)
{
return;
}
var tables = oDiv.getElementsByTagName( "TABLE ");
if( null == tables)
{
return;
}
var tableCount = tables.length;
if(tableCount < 1)
{
return;
}
for (i = 0; i < tableCount; i++)
{
if ( obj == tables[i] )
{
hasTreeNode = true;
i++;
if (tableCount == i )
{
return;
}
}
if (hasTreeNode)
{
var childTreeDeep = tables[i].rows[0].cells.length;
if (childTreeDeep > parentTreeDeep)
{
var cellTreeNode = tables[i].rows[0].cells[childTreeDeep - 1];
var inputs = cellTreeNode.getElementsByTagName( "INPUT ");
if( null != inputs)
{
inputs[0].checked = treeNode.checked;
}
}
else
{
return;
}
}