为了实现点击Treeview Root & parentnode 点击后自动选择leafNODE 节点,我在ASPX的页面增加代码如下:
<script language="javascript" type="text/javascript"> //RoleManagement CheckBox select ; ==================================================================================== function client_OnTreeNodeChecked(obj) {//var obj = window.event.srcElement; var treeNodeFound = false; var checkedState; if (obj.tagName == "INPUT" && obj.type == "checkbox") { var treeNode = obj; checkedState = treeNode.checked; do { obj = obj.parentElement; } while (obj.tagName != "TABLE") var parentTreeLevel = obj.rows[0].cells.length; var parentTreeNode = obj.rows[0].cells[0]; var tables = obj.parentElement.getElementsByTagName("TABLE"); var numTables = tables.length if (numTables >= 1) { 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 CheckEvent() { var objNode = event.srcElement; client_OnTreeNodeChecked(objNode); if(objNode.tagName == "INPUT" && objNode.type== "checkbox") { var objParentDiv = objNode.id.replace("checkbox","Nodes"); if(objNode.checked==true) { //setChildCheckState(objParentDiv,true); //client_OnTreeNodeChecked(); setParentCheckeState(objNode,true); } } else { //setChildCheckState(objParentDiv,false); //client_OnTreeNodeChecked(); // if(!HasOtherChecked(objNode)) // { // setParentCheckeState(objNode,false); // } } } //判断是否有并行的其他节点被选中 function HasOtherChecked(objNode) { var objParentDiv = WebForm_GetParentByTagName(objNode, "div"); var chks = objParentDiv.getElementsByTagName("INPUT"); for(var i=0;i<chks.length;i++) { if(chks[i].checked && chks[i].id != objNode.id) { return true; } } return false; } //设置父节点 function setParentCheckeState(objNode,chkstate) { try { var objParentDiv = WebForm_GetParentByTagName(objNode, "div"); if(objParentDiv == null || objParentDiv == "undefined ") { return; } else { var objParentChkId = objParentDiv.id.replace("Nodes","CheckBox"); var objParentCheckBox = document.getElementById(objParentChkId); if(objParentCheckBox) { objParentCheckBox.checked = chkstate; setParentCheckeState(objParentDiv,chkstate); } } } catch(e){} } //设置子节点 function setChildCheckState(nodeid,chkstate) { var node = document.getElementById(nodeid); if(node) { var chks = node.getElementsByTagName("INPUT"); for(var i=0;i<chks.length;i++) { chks[i].checked = chkstate; } } }
然后再.CS 内给TREEview绑定了一个属性。
this.TreeView1.Attributes.Add ( "onclick", "CheckEvent();" );
然后再定义一个方法,来遍历选择的节点,对应的保存到不同的表内,代码如下,但是不知道为何
if (TN.Checked)都是false ,奇怪中。
private string SaveTreeString(TreeNodeCollection TNC) // 保存TREE 选择情况。 { string RString = ""; foreach ( TreeNode TN in TNC ) { if (TN.Checked ) { if ( TN.ToolTip == "公司" ) { RString = "insert into TR_A(PlanID,CorpID) values(N'" + L_newid.Text + "',N'" + TN.Value + "'); "; } if ( TN.ToolTip == "部门" ) { RString = "insert into TR_B(PlanID,DeptInstantID) values(N'" + L_newid.Text + "',N'" + TN.Value + "'); "; } if ( TN.ToolTip == "岗位" ) { RString = "insert into TR_C(PlanID,PosInstID) values(N'" + L_newid.Text + "',N'" + TN.Value + "'); "; } if ( TN.ToolTip == "员工" ) { RString = "insert into TR_D(PlanID,UID) values(N'" + L_newid.Text + "',N'" + TN.Value + "'); "; } RString += SaveTreeString ( TN.ChildNodes ); } else RString += SaveTreeString ( TN.ChildNodes ); } return RString; }
自己解决了,犯了一个低级错误,没有吧TREEVIEW的绑定放在 if (!IsPostBack ){} 之内。