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
Java Asp PHP .Net XML C/C++ CGI VB Jsp J2ee J2se J2me EJB Servlet Tomcat Resin Struts Weblogic Eclipse ANT GUI JMS Web servise IDEA Webphere Hibernate Spring Jboss Applet Swing Socket Javamail Perl Ajax P2P 安全 模式 框架 测试 开源 游戏
Windows XP Windows 2000 Windows 2003 Windows Me Windows 9.x Linux UNIX 注册表 操作系统 服务器 应用服务器