Saturday, 20 April 2013

how to check all the cheboxes of treeview in asp.net

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Previlege.aspx.cs" Inherits="Website.ERP.ADMIN.Previlege" %>

<!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 id="Head1" runat="server">
    <title></title>
    <link href="../../css/ken-campus-main.css" rel="stylesheet" type="text/css" />
    <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) {
                //alert(src.value);
                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);
                    }
                }
                //check or uncheck parents at all levels
                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) //checkbox checked
                {
                    var isAllSiblingsChecked = AreAllSiblingsChecked(srcChild);
                    if (isAllSiblingsChecked)
                        checkUncheckSwitch = true;
                    else
                        return; //do not need to check parent if any(one or more) child not checked
                }
                else //checkbox unchecked
                {
                    checkUncheckSwitch = false;
                }
                var inpElemsInParentTable = parentNodeTable.getElementsByTagName("input");
                if (inpElemsInParentTable.length > 0) {
                    var parentNodeChkBox = inpElemsInParentTable[0];
                    parentNodeChkBox.checked = checkUncheckSwitch; //do the same recursively
                    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) {
                    //check if the child node is an element node
                    if (parentDiv.childNodes[i].tagName.toLowerCase() == "table") {
                        var prevChkBox = parentDiv.childNodes[i].getElementsByTagName("input")[0];
                        //if any of sibling nodes are not checked,
                        return false
                        if (!prevChkBox.checked) {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
        //utility function to get the container of an element by tagname
        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">
    <asp:ScriptManager ID="sm" runat="server">
    </asp:ScriptManager>
    <div>
        <asp:UpdatePanel ID="up" runat="server">
            <ContentTemplate>
                <div>
                    <div class="DefaultWI">
                        <div class="MainSectionDiv">
                            Previlege</div>
                        <div class="DefaultWOI">
                            <div class="labeldiv">
                                Role:</div>
                            <div class="valuedivSmall">
                                <asp:DropDownList ID="ddlRole" runat="server" Width="250">
                                </asp:DropDownList>
                            </div>
                            <div class="labeldiv">
                                Previleges:</div>
                            <div class="valuedivAuto" style="overflow: auto; height: 250px; width: 250px; border-bottom: 1px solid #cccccc;
                                border-left: 1px solid #cccccc; border-right: 1px solid #cccccc; border-top: 1px solid #cccccc;">
                                <asp:TreeView ID="tvprevileges" runat="server" ShowCheckBoxes="All">
                                </asp:TreeView>
                            </div>
                        </div>
                        <div class="PlaceButton">
                            <asp:Button ID="btnsave" Width="80" runat="server" Text="Save" /></div>
                    </div>
                </div>
            </ContentTemplate>
        </asp:UpdatePanel>
    </div>
    </form>
</body>
</html>
 

c# code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Swash.BusinessLayer;
using Swash.Objects;

namespace Website.ERP.ADMIN
{
    public partial class Previlege : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            tvprevileges.Attributes.Add("onclick", " OnTreeClick(event)");
            if (!IsPostBack)
            {
                fill();
                tvprevileges.CollapseAll();
            }
        }
        void fill()
        {
            tvprevileges.Nodes.Clear();
           
            var m = (from x in ERPManagement.GetInstance.GetAllMenus() where x.Parent_Menu_ID == 0 select new { x.Menu_ID, x.Menu_Text }).ToList();
            for (int i = 0; i < m.Count; i++)
            {
                TreeNode tn = new TreeNode();
                tn.Text = m[i].Menu_Text;
                tn.Value = m[i].Menu_ID.ToString();
                //tn.SelectAction = TreeNodeSelectAction.Select;
                tvprevileges.Nodes.Add(tn);
            }
            for (int i = 0; i < tvprevileges.Nodes.Count; i++)
            {
                var n = (from x in ERPManagement.GetInstance.GetAllMenus() where x.Parent_Menu_ID == Convert.ToInt32(tvprevileges.Nodes[i].Value) select new { x.Menu_ID, x.Menu_Text }).ToList();
                for (int j = 0; j < n.Count; j++)
                {
                    TreeNode tn1 = new TreeNode();
                    tn1.Text = n[j].Menu_Text;
                    tn1.Value = n[j].Menu_ID.ToString();
                    //tn1.SelectAction = TreeNodeSelectAction.Select;
                    tvprevileges.Nodes[i].ChildNodes.Add(tn1);
                }
            }
        }

      
    }
}


In jquery
 <script src="http://localhost:56420/Scripts/jquery-1.8.2.js" type="text/javascript"></script>
    <script language="javascript" type="text/javascript">

      $(document).ready(function () {
          $("div[id=<%= tvprevileges.ClientID %>] input[type=checkbox]").click(function () {
                $(this).closest('table').next('div').find('input[type=checkbox]').attr('checked', this.checked);
   
            $(this).parents('div').each(function (index) {
                    if ($(this).find('input[type=checkbox]:checkbox').length > 0) {
                    $(this).prev('table').find('input[type=checkbox]').attr('checked', true);
                    }
            });
            });

    });       
    </script>

Press Me

No comments:

Post a Comment