I'm new to using Telerik ASP.net AJAX treeview control. The problem I'm
facing is whenever I do a nodeEdit it does fires the code behind
RadTreeView1_NodeEdit event but, when the page reappears it will have 2
treeview controls. One having the old data and the other having the new
updated data. I'm updating the database table and repopulating the tree
when the database write is complete.
Please let me know how to solve this issue.
Thanks
.aspx File
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!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">
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">
function OnClientContextMenuItemClicking(sender, eventArgs) {
var item = eventArgs.get_menuItem();
var node = eventArgs.get_node();
// alert(item.get_text());
curr_item = node.get_text();
var hiddenValue = curr_item;
// alert(hiddenValue);
document.getElementById('Hidden1').value = hiddenValue;
// alert(curr_item);
var w = 500;
var h = 300;
var winl = (screen.width - w) - 75;
var wint = (screen.height - h) / 2;
if (item.get_text() == "Add") {
// var curr_item = parent.frames.view.document.forms("main").curr_item.value;
newupd = window.open('autoComplete_textBox.aspx?curr_item=' + curr_item,
'add_org', 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,width=' + w + ',height=' + h + ',top=' + wint + ',left=' + winl);
}
else if (item.get_text() == "Edit") {
newupd = window.open('Add_Tree_Node.aspx?curr_item=' + curr_item,
'add_org', 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,width=' + w + ',height=' + h + ',top=' + wint + ',left=' + winl);
}
else if (item.get_text() == "Delete") {
alert('Are You Sure You want to delete the menu Item');
}
}
function ClientNodeClicked(sender, eventArgs) {
var node = eventArgs.get_node();
var x = node.get_text();
//var hiddenValue = x;
window.document.getElementById('Hidden1').value = x;
}
function onClicking(sender, eventArgs) {
var item = eventArgs.get_item();
var x = item.get_text();
var hiddenValue = document.getElementById('Hidden1').value;
// alert(hiddenValue);
if (hiddenValue == '')
{
alert('Please choose a Node First');
return;
}
var w = 500;
var h = 300;
var winl = (screen.width - w) - 75;
var wint = (screen.height - h) / 2;
if (x == 'Add') {
// var curr_item = parent.frames.view.document.forms("main").curr_item.value;
newupd = window.open('autoComplete_textBox.aspx?curr_item=' + x, 'add_org', 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,width=' + w + ',height=' + h + ',top=' + wint + ',left=' + winl);
}
else if (x == 'Edit') {
newupd = window.open('autoComplete_textBox.aspx?curr_item=' + x, 'add_org', 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,width=' + w + ',height=' + h + ',top=' + wint + ',left=' + winl);
}
else if (x == 'Delete') {
alert('Do you really want to Delete the Node?');
}
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<telerik:RadMenu ID="RadMenu1" runat="server" Style="float: none; " OnClientItemClicking="onClicking" >
<Items>
<telerik:RadMenuItem Text="Add" >
</telerik:RadMenuItem>
<telerik:RadMenuItem Text="Edit" >
</telerik:RadMenuItem>
<telerik:RadMenuItem Text="Delete" >
</telerik:RadMenuItem>
</Items>
</telerik:RadMenu>
<asp:Panel ID="Panel1" runat="server">
<telerik:RadTextBox runat="Server" ID="NodeTextBox" EmptyMessage="Enter node text"></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Text is required"
Display="Dynamic" ControlToValidate="NodeTextBox" ValidationGroup="TextRequired" ></asp:RequiredFieldValidator>
<asp:Button ID="AddButton" runat="server" OnClick="AddButton_Click"
ValidationGroup="TextRequired" Text="Add new node"></asp:Button>
</asp:Panel>
</div>
<div style="text-align: left">
<telerik:RadTreeView ID="RadTreeView1" runat="server" Skin="Office2007"
EnableDragAndDrop="True" EnableDragAndDropBetweenNodes="True"
OnClientNodeClicked="ClientNodeClicked"
OnNodeDrop="RadTreeView1_HandleDrop"
OnClientContextMenuItemClicking="OnClientContextMenuItemClicking"
TabIndex="1" AccessKey="T" AllowNodeEditing="True"
OnNodeEdit="RadTreeView1_NodeEdit" style="text-align: left"
AppendDataBoundItems="false">
<ContextMenus>
<telerik:RadTreeViewContextMenu ID="RadTreeViewContextMenu1" runat="server" Skin="Sunset">
<Items>
<telerik:RadMenuItem runat="server" Text="Add"></telerik:RadMenuItem>
<telerik:RadMenuItem runat="server" Text="Edit" ></telerik:RadMenuItem>
<telerik:RadMenuItem runat="server" Text="Delete"></telerik:RadMenuItem>
</Items>
</telerik:RadTreeViewContextMenu>
</ContextMenus>
<Nodes>
</Nodes>
</telerik:RadTreeView>
<input id="Hidden1" type="hidden" value="" />
<br />
</div>
</form>
</body>
</html>
Code Behind File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using dataAccess;
using Sybase.Data.AseClient;
using System.Data;
using System.Configuration;
using Telerik.Web.UI;
public partial class Default4 : System.Web.UI.Page
{
//list of all nodes
SortedList<int, Location> myLocations = new SortedList<int, Location>();
//list of all created nodes
SortedList<int, RadTreeNode> myCreatedNodes = new SortedList<int, RadTreeNode>();
protected override void OnLoadComplete(EventArgs e)
{
base.OnLoadComplete(e);
if (!(IsPostBack))
{
RadTreeView1.ExpandAllNodes();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
popTree();
}
}
protected void RadTreeView1_NodeEdit(object sender, Telerik.Web.UI.RadTreeNodeEditEventArgs e)
{
RadTreeNode nodeEdited = e.Node;
string newText = e.Text;
IDBManager dbManager = new DBManager(DataProvider.Sybase);
dbManager.ConnectionString = ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString1"].ToString();
try
{
dbManager.Open();
dbManager.CreateParameters(2);
dbManager.AddParameters(0, "@oldText", nodeEdited.Text);
dbManager.AddParameters(1, "@newText", e.Text);
//string cmdString = "Update u_department Set [dept_name] =" + @newText + " WHERE [dept_name] =" + @oldText + ";
dbManager.ExecuteNonQuery(CommandType.StoredProcedure, "dbo.cSharpUpdateDepName");
// dbManager.ExecuteNonQuery(CommandType.StoredProcedure, "dbo.vb_insert_org");
dbManager.Close();
// dbManager.Dispose();
// popTree();
}
catch (Exception ex)
{
ex.Message.ToString();
}
finally
{
dbManager.Dispose();
}
}
protected void RadTreeView1_HandleDrop(object sender, RadTreeNodeDragDropEventArgs e)
{
foreach (RadTreeNode node in e.DraggedNodes)
{
e.DestDragNode.Nodes.Add(node);
}
e.DestDragNode.Expanded = true;
}
protected void RadTreeView1_ContextMenuItemClick(object sender, RadTreeViewContextMenuEventArgs e)
{
//get the collection that holds the node who owns the Menu
RadTreeNodeCollection collection = e.Node.ParentNode.Nodes;
//get the index of the node
string ORGid = e.Node.ParentNode.Value;
string nodeName = e.Node.Text;
string nodeID = e.Node.Value;
int curIndex = (e.Node.Value).Length;
switch (e.MenuItem.Text)
{
case "Add":
{
string strScript = "<script type='text/javascript'>";
strScript += "window.open('Add_Tree_Node.aspx');";
strScript += "</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "myKey", strScript);
break;
}
case "Edit":
{
string strScript = "<script type='text/javascript'>";
strScript += "window.open('Add_Tree_Node.aspx');";
strScript += "</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "myKey", strScript);
}
break;
case "Delete":
{
//ScriptManager.RegisterStartupScript(this.RadTreeView1, this.GetType, "myKey", strScript, false);
//remove the node
//collection.RemoveAt(curIndex);
RadTreeView1.SelectedNode.Remove();
break;
}
}
}
public void popTree()
{
//Definging the Connection String
string connectionstring = ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString1"].ToString();
//Initializing the Connection Object by passing the Connection String Info
AseConnection sybConnection = new AseConnection(connectionstring);
try
{
//Open the Connection
sybConnection.Open();
// Creating the Command Object
AseCommand sybCmd = sybConnection.CreateCommand();
//Declaring the Command Type in our Case a Stored procedure
sybCmd.CommandType = System.Data.CommandType.StoredProcedure;
//Passing the Stored procedure Name as Command Text
sybCmd.CommandText = "vb_build_org_chart";
AseDataReader myReader = sybCmd.ExecuteReader();
Location myLocation = null;
while (myReader.Read())
{
myLocation = new Location();
myLocation.orgName = Convert.ToString(myReader["org_name"]);
myLocation.orgId = Convert.ToInt32(myReader["org_id"]);
myLocation.parent = Convert.ToInt32(myReader["parent"]);
myLocations.Add(myLocation.orgId, myLocation);
}
/// TreeNode aNode = null;
RadTreeNode aNode = null;
foreach (int aKey in myLocations.Keys)
{
string code = myLocations[aKey].orgId.ToString();
aNode = new RadTreeNode(myLocations[aKey].orgName, code);
CreateNode(aNode);
}
// Closing the Database Connection
sybConnection.Close();
//Disposing the Database Connection object
sybConnection.Dispose();
}
catch (Exception ex)
{
Response.Write("error<BR>" + ex.Message);
Response.Write("Trace<BR>" + ex.StackTrace);
}
finally
{
sybConnection.Close();
sybConnection.Dispose();
}
}
public void CreateNode(RadTreeNode aNode)
{
//This list stores all the nodes id from the current node to the ultimate parent
List<int> myPath = new List<int>();
if (!myCreatedNodes.ContainsValue(aNode))//if the node was not alreazdy created
{
int nodeId = 1001;
nodeId = Convert.ToInt32(aNode.Value);
//Building the current node path untill the ultimate parent
myPath.Add(nodeId);
while (nodeId != 0)
{
if (nodeId != 0)
{
nodeId = myLocations[nodeId].parent;
myPath.Add(nodeId);
}
}
}
//descending from Ultimate parent until the node
//if the current node does not exists we create it and add it to created nodes collection.
//if it has not a parent we add it to the treeview
//if it has a parent,the parent was already created because we come from it, so we add the current node to the parent.
RadTreeNode nodeToAdd = null, ParentNodeTofind = null;
for (int j = myPath.Count - 1; j > -1; j--)
{
if (myPath[j] != 0)
{
//checking for each path if the nodes was already created
if (!myCreatedNodes.Keys.Contains(myLocations[myPath[j]].orgId))
{
//creating the node and adding it to the created nodes collection.
nodeToAdd = new RadTreeNode(myLocations[myPath[j]].orgName, myLocations[myPath[j]].orgId.ToString());
nodeToAdd.ToolTip = nodeToAdd.Text;
myCreatedNodes.Add(myLocations[myPath[j]].orgId, nodeToAdd);
int parentId = myLocations[myPath[j]].parent;
//checking if the node has a parent
if (parentId == 0)//this node has no parent we add it to the tree view
{
RadTreeView1.Nodes.Add(nodeToAdd);
// RadTreeView2.Nodes.Add(nodeToAdd);
}
else//this node has a parent
{
//rerieving parent node (sure to find it)
ParentNodeTofind = myCreatedNodes[myLocations[myPath[j]].parent];
//we add the node to its parent childNodes
ParentNodeTofind.Nodes.Add(nodeToAdd);
}
}
}
}
}
protected void AddButton_Click(object sender, EventArgs e)
{
Page.Validate("TextRequired");
if (Page.IsValid)
{
IRadTreeNodeContainer target = RadTreeView1;
if (RadTreeView1.SelectedNode != null)
{
RadTreeView1.SelectedNode.Expanded = true;
target = RadTreeView1.SelectedNode;
string id = RadTreeView1.SelectedNode.Value;
string stext = RadTreeView1.SelectedNode.Text;
string paren = RadTreeView1.SelectedNode.ParentNode.Value;
string parenName = RadTreeView1.SelectedNode.ParentNode.Text;
}
RadTreeNode addedNode = new RadTreeNode(NodeTextBox.Text);
addedNode.Selected = true;
target.Nodes.Add(addedNode);
}
}
}
Please let me know how to solve this issue.
Thanks
.aspx File
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %>
<!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">
<link href="StyleSheet.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript">
function OnClientContextMenuItemClicking(sender, eventArgs) {
var item = eventArgs.get_menuItem();
var node = eventArgs.get_node();
// alert(item.get_text());
curr_item = node.get_text();
var hiddenValue = curr_item;
// alert(hiddenValue);
document.getElementById('Hidden1').value = hiddenValue;
// alert(curr_item);
var w = 500;
var h = 300;
var winl = (screen.width - w) - 75;
var wint = (screen.height - h) / 2;
if (item.get_text() == "Add") {
// var curr_item = parent.frames.view.document.forms("main").curr_item.value;
newupd = window.open('autoComplete_textBox.aspx?curr_item=' + curr_item,
'add_org', 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,width=' + w + ',height=' + h + ',top=' + wint + ',left=' + winl);
}
else if (item.get_text() == "Edit") {
newupd = window.open('Add_Tree_Node.aspx?curr_item=' + curr_item,
'add_org', 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,width=' + w + ',height=' + h + ',top=' + wint + ',left=' + winl);
}
else if (item.get_text() == "Delete") {
alert('Are You Sure You want to delete the menu Item');
}
}
function ClientNodeClicked(sender, eventArgs) {
var node = eventArgs.get_node();
var x = node.get_text();
//var hiddenValue = x;
window.document.getElementById('Hidden1').value = x;
}
function onClicking(sender, eventArgs) {
var item = eventArgs.get_item();
var x = item.get_text();
var hiddenValue = document.getElementById('Hidden1').value;
// alert(hiddenValue);
if (hiddenValue == '')
{
alert('Please choose a Node First');
return;
}
var w = 500;
var h = 300;
var winl = (screen.width - w) - 75;
var wint = (screen.height - h) / 2;
if (x == 'Add') {
// var curr_item = parent.frames.view.document.forms("main").curr_item.value;
newupd = window.open('autoComplete_textBox.aspx?curr_item=' + x, 'add_org', 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,width=' + w + ',height=' + h + ',top=' + wint + ',left=' + winl);
}
else if (x == 'Edit') {
newupd = window.open('autoComplete_textBox.aspx?curr_item=' + x, 'add_org', 'toolbar=0,location=0,directories=0,status=1,menubar=0,scrollbars=0,resizable=1,width=' + w + ',height=' + h + ',top=' + wint + ',left=' + winl);
}
else if (x == 'Delete') {
alert('Do you really want to Delete the Node?');
}
}
</script>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<telerik:RadMenu ID="RadMenu1" runat="server" Style="float: none; " OnClientItemClicking="onClicking" >
<Items>
<telerik:RadMenuItem Text="Add" >
</telerik:RadMenuItem>
<telerik:RadMenuItem Text="Edit" >
</telerik:RadMenuItem>
<telerik:RadMenuItem Text="Delete" >
</telerik:RadMenuItem>
</Items>
</telerik:RadMenu>
<asp:Panel ID="Panel1" runat="server">
<telerik:RadTextBox runat="Server" ID="NodeTextBox" EmptyMessage="Enter node text"></telerik:RadTextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="Text is required"
Display="Dynamic" ControlToValidate="NodeTextBox" ValidationGroup="TextRequired" ></asp:RequiredFieldValidator>
<asp:Button ID="AddButton" runat="server" OnClick="AddButton_Click"
ValidationGroup="TextRequired" Text="Add new node"></asp:Button>
</asp:Panel>
</div>
<div style="text-align: left">
<telerik:RadTreeView ID="RadTreeView1" runat="server" Skin="Office2007"
EnableDragAndDrop="True" EnableDragAndDropBetweenNodes="True"
OnClientNodeClicked="ClientNodeClicked"
OnNodeDrop="RadTreeView1_HandleDrop"
OnClientContextMenuItemClicking="OnClientContextMenuItemClicking"
TabIndex="1" AccessKey="T" AllowNodeEditing="True"
OnNodeEdit="RadTreeView1_NodeEdit" style="text-align: left"
AppendDataBoundItems="false">
<ContextMenus>
<telerik:RadTreeViewContextMenu ID="RadTreeViewContextMenu1" runat="server" Skin="Sunset">
<Items>
<telerik:RadMenuItem runat="server" Text="Add"></telerik:RadMenuItem>
<telerik:RadMenuItem runat="server" Text="Edit" ></telerik:RadMenuItem>
<telerik:RadMenuItem runat="server" Text="Delete"></telerik:RadMenuItem>
</Items>
</telerik:RadTreeViewContextMenu>
</ContextMenus>
<Nodes>
</Nodes>
</telerik:RadTreeView>
<input id="Hidden1" type="hidden" value="" />
<br />
</div>
</form>
</body>
</html>
Code Behind File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using dataAccess;
using Sybase.Data.AseClient;
using System.Data;
using System.Configuration;
using Telerik.Web.UI;
public partial class Default4 : System.Web.UI.Page
{
//list of all nodes
SortedList<int, Location> myLocations = new SortedList<int, Location>();
//list of all created nodes
SortedList<int, RadTreeNode> myCreatedNodes = new SortedList<int, RadTreeNode>();
protected override void OnLoadComplete(EventArgs e)
{
base.OnLoadComplete(e);
if (!(IsPostBack))
{
RadTreeView1.ExpandAllNodes();
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
popTree();
}
}
protected void RadTreeView1_NodeEdit(object sender, Telerik.Web.UI.RadTreeNodeEditEventArgs e)
{
RadTreeNode nodeEdited = e.Node;
string newText = e.Text;
IDBManager dbManager = new DBManager(DataProvider.Sybase);
dbManager.ConnectionString = ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString1"].ToString();
try
{
dbManager.Open();
dbManager.CreateParameters(2);
dbManager.AddParameters(0, "@oldText", nodeEdited.Text);
dbManager.AddParameters(1, "@newText", e.Text);
//string cmdString = "Update u_department Set [dept_name] =" + @newText + " WHERE [dept_name] =" + @oldText + ";
dbManager.ExecuteNonQuery(CommandType.StoredProcedure, "dbo.cSharpUpdateDepName");
// dbManager.ExecuteNonQuery(CommandType.StoredProcedure, "dbo.vb_insert_org");
dbManager.Close();
// dbManager.Dispose();
// popTree();
}
catch (Exception ex)
{
ex.Message.ToString();
}
finally
{
dbManager.Dispose();
}
}
protected void RadTreeView1_HandleDrop(object sender, RadTreeNodeDragDropEventArgs e)
{
foreach (RadTreeNode node in e.DraggedNodes)
{
e.DestDragNode.Nodes.Add(node);
}
e.DestDragNode.Expanded = true;
}
protected void RadTreeView1_ContextMenuItemClick(object sender, RadTreeViewContextMenuEventArgs e)
{
//get the collection that holds the node who owns the Menu
RadTreeNodeCollection collection = e.Node.ParentNode.Nodes;
//get the index of the node
string ORGid = e.Node.ParentNode.Value;
string nodeName = e.Node.Text;
string nodeID = e.Node.Value;
int curIndex = (e.Node.Value).Length;
switch (e.MenuItem.Text)
{
case "Add":
{
string strScript = "<script type='text/javascript'>";
strScript += "window.open('Add_Tree_Node.aspx');";
strScript += "</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "myKey", strScript);
break;
}
case "Edit":
{
string strScript = "<script type='text/javascript'>";
strScript += "window.open('Add_Tree_Node.aspx');";
strScript += "</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "myKey", strScript);
}
break;
case "Delete":
{
//ScriptManager.RegisterStartupScript(this.RadTreeView1, this.GetType, "myKey", strScript, false);
//remove the node
//collection.RemoveAt(curIndex);
RadTreeView1.SelectedNode.Remove();
break;
}
}
}
public void popTree()
{
//Definging the Connection String
string connectionstring = ConfigurationManager.ConnectionStrings["ASPNETDBConnectionString1"].ToString();
//Initializing the Connection Object by passing the Connection String Info
AseConnection sybConnection = new AseConnection(connectionstring);
try
{
//Open the Connection
sybConnection.Open();
// Creating the Command Object
AseCommand sybCmd = sybConnection.CreateCommand();
//Declaring the Command Type in our Case a Stored procedure
sybCmd.CommandType = System.Data.CommandType.StoredProcedure;
//Passing the Stored procedure Name as Command Text
sybCmd.CommandText = "vb_build_org_chart";
AseDataReader myReader = sybCmd.ExecuteReader();
Location myLocation = null;
while (myReader.Read())
{
myLocation = new Location();
myLocation.orgName = Convert.ToString(myReader["org_name"]);
myLocation.orgId = Convert.ToInt32(myReader["org_id"]);
myLocation.parent = Convert.ToInt32(myReader["parent"]);
myLocations.Add(myLocation.orgId, myLocation);
}
/// TreeNode aNode = null;
RadTreeNode aNode = null;
foreach (int aKey in myLocations.Keys)
{
string code = myLocations[aKey].orgId.ToString();
aNode = new RadTreeNode(myLocations[aKey].orgName, code);
CreateNode(aNode);
}
// Closing the Database Connection
sybConnection.Close();
//Disposing the Database Connection object
sybConnection.Dispose();
}
catch (Exception ex)
{
Response.Write("error<BR>" + ex.Message);
Response.Write("Trace<BR>" + ex.StackTrace);
}
finally
{
sybConnection.Close();
sybConnection.Dispose();
}
}
public void CreateNode(RadTreeNode aNode)
{
//This list stores all the nodes id from the current node to the ultimate parent
List<int> myPath = new List<int>();
if (!myCreatedNodes.ContainsValue(aNode))//if the node was not alreazdy created
{
int nodeId = 1001;
nodeId = Convert.ToInt32(aNode.Value);
//Building the current node path untill the ultimate parent
myPath.Add(nodeId);
while (nodeId != 0)
{
if (nodeId != 0)
{
nodeId = myLocations[nodeId].parent;
myPath.Add(nodeId);
}
}
}
//descending from Ultimate parent until the node
//if the current node does not exists we create it and add it to created nodes collection.
//if it has not a parent we add it to the treeview
//if it has a parent,the parent was already created because we come from it, so we add the current node to the parent.
RadTreeNode nodeToAdd = null, ParentNodeTofind = null;
for (int j = myPath.Count - 1; j > -1; j--)
{
if (myPath[j] != 0)
{
//checking for each path if the nodes was already created
if (!myCreatedNodes.Keys.Contains(myLocations[myPath[j]].orgId))
{
//creating the node and adding it to the created nodes collection.
nodeToAdd = new RadTreeNode(myLocations[myPath[j]].orgName, myLocations[myPath[j]].orgId.ToString());
nodeToAdd.ToolTip = nodeToAdd.Text;
myCreatedNodes.Add(myLocations[myPath[j]].orgId, nodeToAdd);
int parentId = myLocations[myPath[j]].parent;
//checking if the node has a parent
if (parentId == 0)//this node has no parent we add it to the tree view
{
RadTreeView1.Nodes.Add(nodeToAdd);
// RadTreeView2.Nodes.Add(nodeToAdd);
}
else//this node has a parent
{
//rerieving parent node (sure to find it)
ParentNodeTofind = myCreatedNodes[myLocations[myPath[j]].parent];
//we add the node to its parent childNodes
ParentNodeTofind.Nodes.Add(nodeToAdd);
}
}
}
}
}
protected void AddButton_Click(object sender, EventArgs e)
{
Page.Validate("TextRequired");
if (Page.IsValid)
{
IRadTreeNodeContainer target = RadTreeView1;
if (RadTreeView1.SelectedNode != null)
{
RadTreeView1.SelectedNode.Expanded = true;
target = RadTreeView1.SelectedNode;
string id = RadTreeView1.SelectedNode.Value;
string stext = RadTreeView1.SelectedNode.Text;
string paren = RadTreeView1.SelectedNode.ParentNode.Value;
string parenName = RadTreeView1.SelectedNode.ParentNode.Text;
}
RadTreeNode addedNode = new RadTreeNode(NodeTextBox.Text);
addedNode.Selected = true;
target.Nodes.Add(addedNode);
}
}
}