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="Micr osoft.Web.UI.We bControls" Assembly="Micro soft.Web.UI.Web Controls" %>
<%@ Page language="c#" Codebehind="Emp loyeeHierarchy. aspx.cs" AutoEventWireup ="false" Inherits="Ozone .Groups.Employe eHierarchy.Empl oyeeHierarchy" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Employee Hierarchy</title>
<meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.0">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name=vs_default ClientScript content="JavaSc ript">
<meta name=vs_targetS chema content="http://schemas.microso ft.com/intellisense/ie5">
</head>
<body MS_POSITIONING= "GridLayout ">
<asp:panel id="pnlMain" runat="server">
<iewc:treevie w id=tvEmp runat="server" ExpandedImageUr l="images/dir_open.gif" ImageUrl="image s/dir.gif">
</iewc:treeview>
</asp:panel>
</body>
</html>
Code Behind:
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using System.IO;
using System.Data.Sql Client;
using Microsoft.Web.U I.WebControls;
using OZONE.Script.We bControls;
namespace Ozone.Groups.Em ployeeHierarchy
{
/// <summary>
/// Summary description for EmployeeHierarc hy.
/// </summary>
public class EmployeeHierarc hy : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Pane l pnlMain;
private string sConnstr = System.Configur ation.Configura tionSettings.Ap pSett ings["ConnectionStri ng"];
protected Microsoft.Web.U I.WebControls.T reeNode tnEmp;
protected Microsoft.Web.U I.WebControls.T reeNode tnEmpNode;
protected Microsoft.Web.U I.WebControls.T reeView tvEmp;
private string mgrID;
private string empID;
SqlDataAdapter sqlda;
// TreeNode[] tnEmpNode = new TreeNode[10];
private void Page_Load(objec t sender, System.EventArg s e)
{
SqlConnection sqlconn;
string strSql;
try
{
sqlconn = new SqlConnection(s Connstr);
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='02 296'";
sqlda = new SqlDataAdapter( strSql,sqlconn) ;
sqlda.SelectCom mand.CommandTyp e = CommandType.Tex t;
DataTable sqldt = new DataTable("Empl oyee");
sqlda.Fill(sqld t);
DataRow[] drRow;
drRow = sqldt.Select("E mpID='02296'");
if (drRow.Length > 0)
{
for (int i = 0; i < drRow.Length; i++)
{
tnEmp = new TreeNode();
tnEmp.Text = drRow[i]["Employee"].ToString() + " " + "[" + drRow[i]["Designatio n"].ToString() + "]";
tnEmp.ImageUrl = @"Images\dir_op en.gif";
tvEmp.Nodes.Add (tnEmp);
mgrID = drRow[i]["ProjectManager "].ToString();
PopulateTreeVie w(mgrID, ref tnEmp);
}
}
}
catch
{
}
}
private void PopulateTreeVie w(string ID, ref TreeNode tnEmployee)
{
SqlConnection sqlconn;
string strSql;
try
{
sqlconn = new SqlConnection(s Connstr);
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.SelectCom mand.CommandTyp e = CommandType.Tex t;
DataTable sqldt = new DataTable("Empl oyee");
sqlda.Fill(sqld t);
DataRow[] drRow;
drRow = sqldt.Select("E mpID='" + 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]["Designatio n"].ToString() + "]";
tnEmployee.Node s.Add(tnEmpNode );
tnEmpNode.Expan ded = true;
tnEmpNode.Expan dable = ExpandableValue .Always;
mgrID = drRow[i]["ProjectManager "].ToString();
empID = drRow[i]["EmpID"].ToString();
// PopulateTreeVie w(mgrID, tnEmpNode);
}
}
// while(empID != mgrID)
// {
// PopulateTreeVie w(mgrID, tnEmployee);
// }
}
catch
{
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
}
}
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="Micr osoft.Web.UI.We bControls" Assembly="Micro soft.Web.UI.Web Controls" %>
<%@ Page language="c#" Codebehind="Emp loyeeHierarchy. aspx.cs" AutoEventWireup ="false" Inherits="Ozone .Groups.Employe eHierarchy.Empl oyeeHierarchy" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<html>
<head>
<title>Employee Hierarchy</title>
<meta name="GENERATOR " Content="Micros oft Visual Studio .NET 7.0">
<meta name="CODE_LANG UAGE" Content="C#">
<meta name=vs_default ClientScript content="JavaSc ript">
<meta name=vs_targetS chema content="http://schemas.microso ft.com/intellisense/ie5">
</head>
<body MS_POSITIONING= "GridLayout ">
<asp:panel id="pnlMain" runat="server">
<iewc:treevie w id=tvEmp runat="server" ExpandedImageUr l="images/dir_open.gif" ImageUrl="image s/dir.gif">
</iewc:treeview>
</asp:panel>
</body>
</html>
Code Behind:
using System;
using System.Collecti ons;
using System.Componen tModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.Sess ionState;
using System.Web.UI;
using System.Web.UI.W ebControls;
using System.Web.UI.H tmlControls;
using System.IO;
using System.Data.Sql Client;
using Microsoft.Web.U I.WebControls;
using OZONE.Script.We bControls;
namespace Ozone.Groups.Em ployeeHierarchy
{
/// <summary>
/// Summary description for EmployeeHierarc hy.
/// </summary>
public class EmployeeHierarc hy : System.Web.UI.P age
{
protected System.Web.UI.W ebControls.Pane l pnlMain;
private string sConnstr = System.Configur ation.Configura tionSettings.Ap pSett ings["ConnectionStri ng"];
protected Microsoft.Web.U I.WebControls.T reeNode tnEmp;
protected Microsoft.Web.U I.WebControls.T reeNode tnEmpNode;
protected Microsoft.Web.U I.WebControls.T reeView tvEmp;
private string mgrID;
private string empID;
SqlDataAdapter sqlda;
// TreeNode[] tnEmpNode = new TreeNode[10];
private void Page_Load(objec t sender, System.EventArg s e)
{
SqlConnection sqlconn;
string strSql;
try
{
sqlconn = new SqlConnection(s Connstr);
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='02 296'";
sqlda = new SqlDataAdapter( strSql,sqlconn) ;
sqlda.SelectCom mand.CommandTyp e = CommandType.Tex t;
DataTable sqldt = new DataTable("Empl oyee");
sqlda.Fill(sqld t);
DataRow[] drRow;
drRow = sqldt.Select("E mpID='02296'");
if (drRow.Length > 0)
{
for (int i = 0; i < drRow.Length; i++)
{
tnEmp = new TreeNode();
tnEmp.Text = drRow[i]["Employee"].ToString() + " " + "[" + drRow[i]["Designatio n"].ToString() + "]";
tnEmp.ImageUrl = @"Images\dir_op en.gif";
tvEmp.Nodes.Add (tnEmp);
mgrID = drRow[i]["ProjectManager "].ToString();
PopulateTreeVie w(mgrID, ref tnEmp);
}
}
}
catch
{
}
}
private void PopulateTreeVie w(string ID, ref TreeNode tnEmployee)
{
SqlConnection sqlconn;
string strSql;
try
{
sqlconn = new SqlConnection(s Connstr);
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.SelectCom mand.CommandTyp e = CommandType.Tex t;
DataTable sqldt = new DataTable("Empl oyee");
sqlda.Fill(sqld t);
DataRow[] drRow;
drRow = sqldt.Select("E mpID='" + 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]["Designatio n"].ToString() + "]";
tnEmployee.Node s.Add(tnEmpNode );
tnEmpNode.Expan ded = true;
tnEmpNode.Expan dable = ExpandableValue .Always;
mgrID = drRow[i]["ProjectManager "].ToString();
empID = drRow[i]["EmpID"].ToString();
// PopulateTreeVie w(mgrID, tnEmpNode);
}
}
// while(empID != mgrID)
// {
// PopulateTreeVie w(mgrID, tnEmployee);
// }
}
catch
{
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArg s e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeCompo nent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.Load += new System.EventHan dler(this.Page_ Load);
}
#endregion
}
}