当前位置:首页 > 网络编程 > WEB编程 > ASP.net >  ASP.NET 2.0 Treeview Checkboxes - Check All -

 ASP.NET 2.0 Treeview Checkboxes - Check All -

点击次数:18 次 发布日期:2008-11-26 12:42:06 作者:源代码网
源代码网推荐      ASP.NET 2.0 TreeView has many built-in features such as showing a checkbox for all the Tree Nodes. Node level formating, style, etc., Enabling the ShowCheckBoxes="All" property sets it to show a checkbox for all the nodes. The other options are Leaf, None, Parent and Root which show checkboxes at the respective node levels. None doesnt display CheckBoxes.
源代码网推荐  
源代码网推荐  When we set ShowCheckBoxes="All", we would like to provide a feature where people can select the checkbox on the Root Node so that all the other checkboxes are checked automatically. Basically, when the parent node is checked, all the child nodes should be checked automatically.
源代码网推荐  
源代码网推荐  It would be intuitive to accomplish this task at the client side without involving a postback.
源代码网推荐  
源代码网推荐  The following code snippet helps in accomplishing the same.
源代码网推荐  
源代码网推荐  TreeView Declaration
源代码网推荐  
源代码网推荐  <asp:TreeView ID="TreeView1" Runat="server" DataSourceID="XmlDataSource1" onclick="client_OnTreeNodeChecked();" ShowCheckBoxes="all">
源代码网推荐  
源代码网推荐  <DataBindings>
源代码网推荐  
源代码网推荐  <asp:TreeNodeBinding DataMember="Category" ValueField="ID" TextField="Name"></asp:TreeNodeBinding>
源代码网推荐  
源代码网推荐  <asp:TreeNodeBinding DataMember="Description" ValueField="Value" TextField="Value"></asp:TreeNodeBinding>
源代码网推荐  
源代码网推荐  </DataBindings>
源代码网推荐  
源代码网推荐  </asp:TreeView>
源代码网推荐  
源代码网推荐  
源代码网推荐  In the above TreeView declaration Code, you can find the property onclick="client_OnTreeNodeChecked();" event which actually is the JavaScript function which would accomplish this task.
源代码网推荐  
源代码网推荐  The Javascript Code snippet is as follows:-
源代码网推荐  
源代码网推荐  <script language="javascript" type="text/javascript">
源代码网推荐  function client_OnTreeNodeChecked()
源代码网推荐  {
源代码网推荐  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;
源代码网推荐  }
源代码网推荐  }
源代码网推荐  }
源代码网推荐  }
源代码网推荐  }
源代码网推荐  }
源代码网推荐  </script>
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐  
源代码网推荐    做人要厚道,请注明转自酷网动力(www.ASPCOOL.COM)。
源代码网推荐


源代码网供稿.
网友评论 (0)
会员中心
网络编程
本站推荐
网络编程之精华