Sunday, November 13, 2011

how to Add Child Node and sub child node to the tree view dynamically in asp.net


here goes your aspx code:

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

<!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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:ScriptManager ID="smTreeItems" runat="server" />
            <asp:UpdatePanel ID="updatePanel1" runat="server">
                <ContentTemplate>
                    <table>
                        <tr>
                            <td style="width: 467px">
                                <div id="divGrid" style="overflow: auto; width: 100%; height: 520px; margin: 0; border: 0px;
                                    background-color: White">
                                    <table cellpadding="0" cellspacing="0" style="margin: 0px; width: 98%" class="XGridOuterBorder">
                                        <tr>
                                            <td style="height: 314px">
                                                <asp:TreeView ID="tvwItems" runat="server" Font-Names="Verdana" Font-Size="12px"
                                                    ForeColor="#F48110" SkinID="NonPostBackTree" Height="296px" Width="415px" SelectedNodeStyle-ForeColor="blue"
                                                    ShowCheckBoxes="All" OnSelectedNodeChanged="tvwItems_SelectedNodeChanged">
                                                    <LeafNodeStyle ImageUrl="~/Images/ArrowColor.gif" />
                                                    <NodeStyle CssClass="tree_node_text" />
                                                    <ParentNodeStyle CssClass="tree_node_text" />
                                                    <RootNodeStyle CssClass="tree_node_text" />
                                                    <HoverNodeStyle CssClass="tree_node_text" />
                                                </asp:TreeView>
                                            </td>
                                        </tr>
                                    </table>
                                </div>
                            </td>
                        </tr>
                    </table>
                </ContentTemplate>
            </asp:UpdatePanel>
        </div>
    </form>
</body>
</html>
CodeBehind:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class TreeItems : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            tvwItems.Nodes.Clear();
            BindRoots();
        }
    }
    private void BindRoots()
    {
        try
        {
            Page.Header.Title = "Organogram Picker";

            string str = "Select NodeID as ID , Name  From RAHierarchyNode where Level=1";
            SqlDataReader reader = GetData(str);

            while (reader.Read())
            {
                TreeNode rootNode = new TreeNode(reader[1].ToString(), reader[0].ToString());
                tvwItems.Nodes.Add(rootNode);
            }
            reader.Close();
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

    private void BindChilds(TreeNode node, int parentNodeID)
    {

        string str = "Select NodeID as ID , Name   From RAHierarchyNode where ParentNodeID=" + parentNodeID;
        SqlDataReader reader = GetData(str);
        while (reader.Read())
        {
            TreeNode childNode = new TreeNode(reader[1].ToString(), reader[0].ToString());
            node.ChildNodes.Add(childNode);
        }
        reader.Close();
    }

    protected void tvwItems_SelectedNodeChanged(object sender, EventArgs e)
    {
        if (!(tvwItems.SelectedNode.ChildNodes.Count > 0))
        {
            BindChilds(tvwItems.SelectedNode, Convert.ToInt32(tvwItems.SelectedNode.Value));
            tvwItems.SelectedNode.Expand();
        }
    }

    private SqlDataReader GetData(string commandText)
    {
        string strConnection = "server=cel38;database=HRMIS010710;user id=sa; connection timeout=60";
       
        SqlConnection con = new SqlConnection(strConnection);
        con.Open();
        SqlCommand sqlcmd = new SqlCommand(commandText, con);
        SqlDataReader dr = sqlcmd.ExecuteReader();
       
        return dr;
    }
}