Hello I'm a newcomer to ASP.Net. I just need to create a hierarchy for each employee like employee name, then his immidiate manager and so on. For this i need to use a teeview. But i'm facing a problem when creating nodes dynamically. Here is a sample how my structure should look like.
xxxxxxxxxx
yyyyyyyyyyy
zzzzzzzzzzzz
aaaaaaaaaaaa
Also i want to expand and collapse each node with the + and - sign respectively. I tried with my code but it's displaying the hierarchy in the single line. Also it doesn't display the whole hierarchy. Here is my code and plz help me finding where i'm wrong. Thanks in Advance.
HTML content:
<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls" %>
<%@ Page language="c#" Codebehind="EmployeeHierarchy.aspx.cs" AutoEventWireup="false" Inherits="Ozone.Groups.EmployeeHierarchy.EmployeeH ierarchy" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>EmployeeHierarchy</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.0">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name=vs_defaultClientScript content="JavaScript">
<meta name=vs_targetSchema content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body MS_POSITIONING="GridLayout">
<asp:panel id="pnlMain" runat="server">
<iewc:treeview id=tvEmp runat="server" ExpandedImageUrl="images/dir_open.gif" ImageUrl="images/dir.gif">
</iewc:treeview>
</asp:panel>
</body>
</html>
Code Behind:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Data.SqlClient;
using Microsoft.Web.UI.WebControls;
using OZONE.Script.WebControls;
namespace Ozone.Groups.EmployeeHierarchy
{
/// <summary>
/// Summary description for EmployeeHierarchy.
/// </summary>
public class EmployeeHierarchy : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel pnlMain;
private string sConnstr = System.Configuration.ConfigurationSettings.AppSett ings["ConnectionString"];
protected Microsoft.Web.UI.WebControls.TreeNode tnEmp;
protected Microsoft.Web.UI.WebControls.TreeNode tnEmpNode;
protected Microsoft.Web.UI.WebControls.TreeView tvEmp;
private string mgrID;
private string empID;
SqlDataAdapter sqlda;
// TreeNode[] tnEmpNode = new TreeNode[10];
private void Page_Load(object sender, System.EventArgs e)
{
SqlConnection sqlconn;
string strSql;
try
{
sqlconn = new SqlConnection(sConnstr);
sqlconn.Open();
strSql = "select Employee_id as EmpID, Employee_name as Employee, Job_title as Designation, mgr_id as ProjectManager from Employee where Employee_id='02296'";
sqlda = new SqlDataAdapter(strSql,sqlconn);
sqlda.SelectCommand.CommandType = CommandType.Text;
DataTable sqldt = new DataTable("Employee");
sqlda.Fill(sqldt);
DataRow[] drRow;
drRow = sqldt.Select("EmpID='02296'");
if (drRow.Length > 0)
{
for (int i = 0; i < drRow.Length; i++)
{
tnEmp = new TreeNode();
tnEmp.Text = drRow[i]["Employee"].ToString() + " " + "[" + drRow[i]["Designation"].ToString() + "]";
tnEmp.ImageUrl = @"Images\dir_open.gif";
tvEmp.Nodes.Add(tnEmp);
mgrID = drRow[i]["ProjectManager"].ToString();
PopulateTreeView(mgrID, ref tnEmp);
}
}
}
catch
{
}
}
private void PopulateTreeView(string ID, ref TreeNode tnEmployee)
{
SqlConnection sqlconn;
string strSql;
try
{
sqlconn = new SqlConnection(sConnstr);
sqlconn.Open();
strSql = "select Employee_id as EmpID, Employee_name as Employee, Job_title as Designation, mgr_id as ProjectManager from Employee where Employee_id='" + ID.ToString() + "'";
sqlda = new SqlDataAdapter(strSql,sqlconn);
sqlda.SelectCommand.CommandType = CommandType.Text;
DataTable sqldt = new DataTable("Employee");
sqlda.Fill(sqldt);
DataRow[] drRow;
drRow = sqldt.Select("EmpID='" + ID.ToString() + "'");
if (drRow.Length > 0)
{
for (int i = 0; i < drRow.Length; i++)
{
tnEmpNode = new TreeNode();
tnPrent = new TreeNode(tnEmp,new TreeNode[]{tnEmpNode});
tnEmpNode.Text = drRow[i]["Employee"].ToString() + " " + "[" + drRow[i]["Designation"].ToString() + "]";
tnEmployee.Nodes.Add(tnEmpNode);
tnEmpNode.Expanded = true;
tnEmpNode.Expandable = ExpandableValue.Always;
mgrID = drRow[i]["ProjectManager"].ToString();
empID = drRow[i]["EmpID"].ToString();
// PopulateTreeView(mgrID, tnEmpNode);
}
}
// while(empID != mgrID)
// {
// PopulateTreeView(mgrID, tnEmployee);
// }
}
catch
{
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}