473,320 Members | 1,828 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Regarding TreeView in ASP .Net

49
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
}
}
Sep 6 '07 #1
0 811

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: sonali_reddy123 | last post by:
Hi, I am having a problem regarding use of a treeview. The problem is whether it is possible to set a default position to the scrollbar so that if any of the treenode contains the text bigger...
1
by: Norman Fritag | last post by:
Hi there I have avoided to use active x controls because I thought they are causing more problems then they are doing any good. I a new application I would want to use the tree and list view...
42
by: lauren quantrell | last post by:
So many postings on not to use the treeview control, but nothing recently. Is it safe to swim there yet with Access 2000-Access 2003?
1
by: Nelson | last post by:
Hi Folks, How can I partially color a Treeview Node in one color like the Mail Boxes in Outlook Express showing the number of mails next to the caption. Nelson
6
by: L.M | last post by:
Hello, I knew how to use the treeview under VB6. After migrating to .NET, well, I'm lost. I try to add a new node, either to the same level or as a child to a selected node in the treeview....
2
by: rolf.oltmans | last post by:
Hello all, I need to place treeview control in Grid control. I need to place it in a grid because I need to show calendar against every node. Is placing a treeview in grid possible? If I need...
0
by: Rama Jayapal | last post by:
i have a webpage where i have parsed all the details using html parser now the parsed document is in html format i have now converted this document into a treeview now the problem is i have...
4
by: sree266 | last post by:
hi, i need code for "selecting checkboxes in a treeview and it should give warning if the user selects less then five check boxes"
5
by: sree266 | last post by:
hi, i need code for selecting the checkboxes in treeview and also validations for it.. and i wanted to select atleast five boxes and it should give message if we select less then five
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.