首页 > 编程 > .NET > 正文

ASP.NET 2.0 Treeview Checkboxes - Check All - Java

2024-07-10 13:11:03
字体:
来源:转载
供稿:网友

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" 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 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>



发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表