Wednesday 26 October 2011

Checking child nodes when checking the parent node


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>

<!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>Test</title>

    <script language="javascript" 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;
            if(nxtSibling && nxtSibling.nodeType == 1)
            {
                if(nxtSibling.tagName.toLowerCase() == "div") //if node has children
                {
                    CheckUncheckChildren(parentTable.nextSibling, src.checked);
                }
            }
            CheckUncheckParents(src, 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 CheckUncheckParents(srcChild, check)
   {
       var parentDiv = GetParentByTagName("div", srcChild);
       var parentNodeTable = parentDiv.previousSibling;
     
       if(parentNodeTable)
        {
            var checkUncheckSwitch;
           
            if(check)
            {
                var isAllSiblingsChecked = AreAllSiblingsChecked(srcChild);
                if(isAllSiblingsChecked)
                    checkUncheckSwitch = true;
                else  
                    return;
            }
            else
            {
                checkUncheckSwitch = false;
            }
           
            var inpElemsInParentTable = parentNodeTable.getElementsByTagName("input");
            if(inpElemsInParentTable.length > 0)
            {
                var parentNodeChkBox = inpElemsInParentTable[0];
                parentNodeChkBox.checked = checkUncheckSwitch;
             
                CheckUncheckParents(parentNodeChkBox, checkUncheckSwitch);
            }
        }
   }
   function AreAllSiblingsChecked(chkBox)
   {
     var parentDiv = GetParentByTagName("div", chkBox);
     var childCount = parentDiv.childNodes.length;
     for(var i=0; i<childCount; i++)
     {
        if(parentDiv.childNodes[i].nodeType == 1)
        {
            if(parentDiv.childNodes[i].tagName.toLowerCase() =="table")
            {
               var prevChkBox = parentDiv.childNodes[i].getElementsByTagName("input")[0];
             
              if(!prevChkBox.checked)
              {
                return false;
              }
            }
        }
     }
     return true;
   }
   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>
        <asp:TreeView ID="TreeViewDemo" runat="server" ExpandDepth="0" ShowCheckBoxes="All">
            <Nodes>
                <asp:TreeNode NavigateUrl="" Text="P" Value="P">
                    <asp:TreeNode NavigateUrl="" Text="P:C1" Value="C1"></asp:TreeNode>
                    <asp:TreeNode NavigateUrl="" Text="P:C2" Value="C2"></asp:TreeNode>
                </asp:TreeNode>
                <asp:TreeNode NavigateUrl="" Text="P1" Value="P1">
                    <asp:TreeNode NavigateUrl="" Text="P1:C1" Value="P1:C1"></asp:TreeNode>
                    <asp:TreeNode NavigateUrl="" Text="P1:C2" Value="P1:C2"></asp:TreeNode>
                </asp:TreeNode>
                <asp:TreeNode NavigateUrl="" Text="P2" Value="P2">
                    <asp:TreeNode NavigateUrl="" Text="P2:C1" Value="P2:C1"></asp:TreeNode>
                    <asp:TreeNode NavigateUrl="" Text="P2:C2" Value="P2:C2"></asp:TreeNode>
                </asp:TreeNode>
            </Nodes>
        </asp:TreeView>
    </div>
    </form>
</body>
</html>
------------------------------------------------------------------
 protected void Page_Load(object sender, EventArgs e)
    {
        TreeViewDemo.Attributes.Add("onclick", "OnTreeClick(event)");
    }

1 comment:

  1. You are the man!!!!!!!!!!!!!!!!!!!!bro you save mi life..........thanks a lot for your great Knowledge SudiptaSanyal

    ReplyDelete

Get all non-clustered indexes

DECLARE cIX CURSOR FOR     SELECT OBJECT_NAME(SI.Object_ID), SI.Object_ID, SI.Name, SI.Index_ID         FROM Sys.Indexes SI             ...