1.更新到asp.net2.0或以上,将Microsoft.Web.UI.TreeView更换为新的System.Web.UI.WebControls.TreeView
2.将UserId,UserType,ChildType拼接成字符串放在TreeNode value中,拼接格式如下: [Id];[Type];[ChildType]
for example:
TreeView1.Nodes.Add(new TreeNode("name1", "id1;ou;0")); TreeView1.Nodes.Add(new TreeNode("name2", "id2;ou;0")); TreeView1.Nodes.Add(new TreeNode("name3", "id3;ou;0")); TreeView1.Nodes.Add(new TreeNode("name4", "id4;ou;0")); TreeView1.Nodes.Add(new TreeNode("name5", "id5;ou;0")); TreeNode child1 = new TreeNode("child name1", "child id1;user;1"); TreeView1.Nodes[].ChildNodes.Add(child1); TreeNode child11 = new TreeNode("child name2", "child id2;user;1"); TreeView1.Nodes[].ChildNodes.Add(child11);
3. 引入TreeSelector <script type="text/javascript" src="TreeSelector.js"></script>
//function setTreeNodeExpanded -- TreeView attribute FullyExpand var TreeSelector = { init: function (paramObj) { var clientId = paramObj.clientId; var onNodeDblClick = paramObj.onNodeDblClick; ) return; var treeNodes = TreeSelector.getAllNodes(clientId); ; i < treeNodes.length; i++) { var node = treeNodes[i]; , node.href.length - ); node.href = "javascript:void(0)"; ) { value = value.substring(value.lastIndexOf(); } var values = value.split(";"); var text = node.innerText; node.setAttribute(]); node.setAttribute("NodeText", text); node.setAttribute(]); node.setAttribute(]); } var treeview = document.getElementById(clientId); treeview.ondblclick = function () { var selectedNode = TreeSelector.getSelectedNode(clientId); if (selectedNode != null) { onNodeDblClick.apply(this, [selectedNode]); } } }, getAllNodes: function (treeClientId) { var result = []; var treeview = document.getElementById(treeClientId); var treeLinks = treeview.getElementsByTagName("a"); ; i < treeLinks.length; i++) { if (treeLinks[i].firstChild.tagName != "IMG") result.push(treeLinks[i]); } return result; }, getSelectedNode: function (treeClientId) { var treeViewData = window[treeClientId + "_Data"]; if (treeViewData.selectedNodeID != null && treeViewData.selectedNodeID.value != "") { return document.getElementById(treeViewData.selectedNodeID.value); } } }
4.页面脚本
<script type="text/javascript"> var tree = document.getElementById("<%=TreeView1.ClientID%>"); var searchResultList = document.getElementById("<%=lbSelectTree.ClientID%>"); var selectedList = document.getElementById("<%=lbSelected.ClientID%>"); var _userId = document.getElementById("<%=txtUser.ClientID%>").value; "; var _selectType = document.getElementById("<%=txtSelectType.ClientID%>").value; window.onload = function () { releaseXML(); TreeSelector.init({ clientId: tree.id, onNodeDblClick: function (selectNode) { addSelectedNodeToList(selectNode); } }); searchResultList.ondblclick = function () { var SelectedID = searchResultList.options[searchResultList.selectedIndex].value; var SelectedText = searchResultList.options[searchResultList.selectedIndex].text; addSearchResultToList(SelectedText, SelectedID); } } function ShowTree() { document.getElementById("divTree").style.display = ""; document.getElementById("divSelectTree").style.display = "none"; document.getElementById("btnShowTree").style.display = "none"; } function RemoveSelected() { ) return; selectedList.options.remove(selectedList.selectedIndex); } function AllSelect() { if (_isSingle) { alert("只能进行单选!"); return; } if (document.getElementById("divSelectTree").style.display == "none") { findAllNode(); } else { ; k < searchResultList.length; k++) { var SelectedID = searchResultList.options[k].value; var SelectedText = searchResultList.options[k].text; //SelectChildType = searchResultList.options[k].ChildType; //if (UserID == "" || SelectChildType == "1") { addSearchResultToList(SelectedText, SelectedID); } } } function RemoveAllSelected() { ) >= ) { selectedList.options.remove(k); } } function FindTreeNode() { ) >= ) { searchResultList.options.remove(k); } var key = document.getElementById("<%=txtkey.ClientID%>").value; if (key == "") return; var nodes = TreeSelector.getAllNodes(tree.id); ; i < nodes.length; i++) { var cNode = nodes[i]; ) { document.getElementById("divTree").style.display = "none"; document.getElementById("btnShowTree").style.display = ""; document.getElementById("divSelectTree").style.display = ""; var nowListName = getFullName(cNode.getAttribute("NodeType"), cNode.getAttribute("NodeText")); searchResultList.options.add(new Option(nowListName.Trim(), cNode.getAttribute("NodeId"))); } } } function GetXML() { ].txtIsMustSelect.value == ) { alert("必须选择一个!"); return false; } var selectName = ""; var ReturnXML = "<NewDataSet>"; ; k < selectedList.length; k++) { var nowSelectType = ""; var nowSelectID = selectedList.options[k].value; var nowSelectName = selectedList.options[k].text; ) { nowSelectType = "user"; } ) { nowSelectType = "group"; } ) { nowSelectType = "role"; } ) { nowSelectType = "ou"; } , nowSelectName.length - ); ReturnXML += "<Table>"; ReturnXML += "<Name>" + trueSelectName.Trim() + "</Name>"; ReturnXML += "<ID>" + nowSelectID.Trim() + "</ID>"; ReturnXML += "<Type>" + nowSelectType.Trim() + "</Type>"; ReturnXML += "</Table>"; selectName += trueSelectName.Trim() + "," } ReturnXML += "</NewDataSet>"; ) { selectName = selectName.substr(, selectName.length - ); } a[] = selectName; a[] = ReturnXML; Dialog.CloseAndReturn(a); } //取消窗口 function CancelWindows() { Dialog.Close(); } function UserSelectorOption(id, name, type) { this.ID = id; this.Name = name; this.Type = type; } function ParseDataSetXML(xml) { var options = []; var xmlDoc = GetXMLDocument(xml); if (xmlDoc) { var items = xmlDoc.selectNodes("/NewDataSet/Table"); ; j < items.length; j++) { var id = GetXMLNodeText(items[j].selectSingleNode("ID")); var name = GetXMLNodeText(items[j].selectSingleNode("Name")); var type = GetXMLNodeText(items[j].selectSingleNode("Type")); options.push(new UserSelectorOption(id, name, type)); } } return options; } String.prototype.Trim = function () { return this.replace(/^\s+|\s+$/g, ""); } function releaseXML() { var nodeId; var nodeText; var nodeType; var nodeListName; var j; var GetInsertXML = window.dialogArguments; if (GetInsertXML == null) return; var items = ParseDataSetXML(GetInsertXML); ; j < items.length; j++) { nodeId = items[j].ID; nodeText = items[j].Name; nodeListName = getListName(item[j].Name, item[j].Type); selectedList.options.add(new Option(nodeListName, items[j].ID)); } } function isListBoxID(nodeID) { var sel_len = selectedList.length; ) return false; ; j < sel_len; j++) { var sel_id = selectedList.options[j].value; if (sel_id == nodeID) return true; } return false; } function getFullName(type, name) { var nodeFullName = ""; switch (type) { case "ou": nodeFullName = name + " [组 织]"; break; case "group": nodeFullName = name + " [工作组]"; break; case "role": nodeFullName = name + " [角 色]"; break; case "user": nodeFullName = name + " [用 户]"; break; default: nodeFullName = ""; break; } return nodeFullName; } function addSelectedNodeToList(node) { var name = node.getAttribute("NodeText"); var type = node.getAttribute("NodeType"); var childType = node.getAttribute("ChildNodeType"); var nodeId = node.getAttribute("NodeId"); ") { alert("非下属部门,不能选择!"); return; } var nodeFullName = getFullName(type, name); addSelectedObjectToList(type, nodeFullName, nodeId); } function addSelectedObjectToList(type, name, id) { ) { alert("只能进行单选!"); return; } if (isListBoxID(id)) return; , ) == " && type == "user") || (_selectType.substr(, ) == " && type == "role") || (_selectType.substr(, ) == " && type == "ou")) selectedList.options.add(new Option(name.Trim(), id)); } function addSearchResultToList(text, id) { , text.length - ); var type = ""; ) type = "user"; ) type = "group"; ) type = "role"; ) type = "ou"; addSelectedObjectToList(type, text, id); } function findAllNode() { var nodes = TreeSelector.getAllNodes(tree.id); ; i < nodes.length; i++) { addSelectedNodeToList(nodes[i]); } } /*---- search tree ------*/ </script>