Skip to content

Instantly share code, notes, and snippets.

@subhasisrout
Created September 8, 2010 19:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save subhasisrout/570678 to your computer and use it in GitHub Desktop.
Save subhasisrout/570678 to your computer and use it in GitHub Desktop.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TreeviewDemo.aspx.cs" Inherits="dotnet_topics3.TreeviewDemo" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Tree View Demo - Client side events</title>
<script type="text/javascript">
function OnTreeClick(evt) {
var src = window.event != window.undefined ? window.event.srcElement : evt.target;
var isChkBoxClick = (src.tagName.toLowerCase() == 'input' && src.type == 'checkbox');
if (isChkBoxClick) {
var parentTable = GetParentByTagName('table', src);
var nxtSibling = parentTable.nextSibling;
//check if nxt sibling is not null & is an element node
if (nxtSibling && nxtSibling.nodeType == 1) {
if (nxtSibling.tagName.toLowerCase() == 'div') //if node has children
{
//check or uncheck children at all levels
CheckUncheckChildren(parentTable.nextSibling, src.checked);
}
}
if (src.nextSibling && src.nextSibling.innerText == "Select All") {
CheckAll('div1', src.checked);
}
}
}
function CheckUncheckChildren(childContainer, check) {
var childChkBoxes = childContainer.getElementsByTagName('input');
var childChkBoxCount = childChkBoxes.length;
for (var i = 0; i < childChkBoxCount; i++) {
childChkBoxes[i].checked = check;
}
}
function CheckAll(divId,check) {
var collection = document.getElementById(divId).getElementsByTagName('INPUT');
for (var x = 0; x < collection.length; x++) {
if (collection[x].type.toUpperCase() == 'CHECKBOX')
collection[x].checked = check;
}
}
function GetParentByTagName(parentTagName, childElementObj) {
var parent = childElementObj.parentNode;
while (parent.tagName.toLowerCase() != parentTagName.toLowerCase()) {
parent = parent.parentNode;
}
return parent;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div id="div1">
<asp:TreeView runat="server" ID="tv1" onclick="OnTreeClick(event)" ShowLines="true">
<RootNodeStyle />
</asp:TreeView>
<br />
<asp:Button runat="server" ID="btn1" OnClick="btn1_Click" Text="Save Changes" />
<br />
<asp:Button runat="server" ID="btn2" OnClientClick="checkByParent('div1')" Text="Check All" />
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using dotnet_topics3.BE;
namespace dotnet_topics3
{
public partial class TreeviewDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadTreeview();
}
}
protected void btn1_Click(object sender, EventArgs e)
{
List<CMRegionBranch> listRegionBranch = new List<CMRegionBranch>();
CMRegionBranch objCMRegionBranch = null;
foreach (TreeNode region in tv1.Nodes)
{
if (region.Value != "SELECTALL")
{
objCMRegionBranch = new CMRegionBranch();
objCMRegionBranch.Region = new CRRegion() { RegionID = Int32.Parse(region.Value), RegionName = region.Text };
objCMRegionBranch.BranchList = new List<CRBranch>();
foreach (TreeNode branch in region.ChildNodes)
{
objCMRegionBranch.BranchList.Add(new CRBranch() { BranchID = Int32.Parse(branch.Value), BranchName = branch.Text, IsAssigned = branch.Checked });
}
listRegionBranch.Add(objCMRegionBranch);
}
}
}
private void LoadTreeview()
{
List<CMRegionBranch> listRegionBranch = new List<CMRegionBranch>();
CMRegionBranch objCMRegionBranch = new CMRegionBranch()
{
Region = new CRRegion() { RegionID = 1, RegionName = "R1" },
BranchList = new List<CRBranch>() { new CRBranch() { BranchID = 1, BranchName = "B1", IsAssigned = true }, new CRBranch() { BranchID = 2, BranchName = "B2", IsAssigned = true }, new CRBranch() { BranchID = 3, BranchName = "B3", IsAssigned = false } },
};
listRegionBranch.Add(objCMRegionBranch);
objCMRegionBranch = new CMRegionBranch()
{
Region = new CRRegion() { RegionID = 2, RegionName = "R2" },
BranchList = new List<CRBranch>() { new CRBranch() { BranchID = 4, BranchName = "B4", IsAssigned = true }, new CRBranch() { BranchID = 5, BranchName = "B5", IsAssigned = true } },
};
listRegionBranch.Add(objCMRegionBranch);
objCMRegionBranch = new CMRegionBranch()
{
Region = new CRRegion() { RegionID = 3, RegionName = "R3" },
BranchList = new List<CRBranch>() { new CRBranch() { BranchID = 6, BranchName = "B6", IsAssigned = false }, new CRBranch() { BranchID = 7, BranchName = "B7", IsAssigned = true }, new CRBranch() { BranchID = 8, BranchName = "B8", IsAssigned = true } },
};
listRegionBranch.Add(objCMRegionBranch);
TreeNode tnRegion = null;
TreeNode tnBranch = null;
TreeNode tnRootNode = new TreeNode("Select All", "SELECTALL");
tnRootNode.ShowCheckBox = true;
tv1.Nodes.Add(tnRootNode);
foreach (CMRegionBranch item in listRegionBranch)
{
tnRegion = new TreeNode(item.Region.RegionName, item.Region.RegionID.ToString());
tnRegion.ShowCheckBox = true;
foreach (CRBranch brnch in item.BranchList)
{
tnBranch = new TreeNode(brnch.BranchName,brnch.BranchID.ToString());
tnBranch.ShowCheckBox = true;
tnBranch.Checked = brnch.IsAssigned;
tnRegion.ChildNodes.Add(tnBranch);
}
tv1.Nodes.Add(tnRegion);
}
}
}
}
@subhasisrout
Copy link
Author

Treeview with Client side events

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment