hi,
I hava a treeview which have been populated dynamically.when user clicks on child node, the repective page or the page which have been navigated opens but the Child nodes get collapse.I need the childnodes to be expanded even on click of it.what should i do for it.Hoping for help.
Loading
theLizardPosted Dec 25, 2009, 3:02 PM
Good luck.
//---------------------------------------------------------------------------
public void expandNodes(TreeView tv, _sessions _session)
{
TreeNode node;
tv.Nodes[ 0 ].Selected = true; //root node
node = tv.SelectedNode;
if (_session.valuePath != "")
{
node = tv.FindNode ( _session.valuePath );
switch(node.Depth)
{
case 0:
break;
case 1:
node.Parent.Expand ( );
break;
case 2:
node.Parent.Parent.Expand ( );
node.Parent.Expand ( );
break;
case 3:
node.Parent.Parent.Parent.Expand ( );
node.Parent.Parent.Expand ( );
node.Parent.Expand ( );
break;
case 4:
node.Parent.Parent.Parent.Parent.Expand ( );
node.Parent.Parent.Parent.Expand ( );
node.Parent.Parent.Expand ( );
node.Parent.Expand ( );
break;
}
node.Expand ( );
node.Selected = true;
}
//this is the whole source file for the tree view for one page of the web site..
//---------------------------------------------------------------------------------------------------------
using System;
using System.Data;
using System.Configuration;
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;
///
/// Summary description for Class1
///
public class ownerTree
{
public ownerTree ( )
{
//
// TODO: Add constructor logic here
//
}
//---------------------------------------------------------------------------
public bool tvload ( TreeView tv, _sessions _session)
{
string s;
bool success = true;
string location, id, a, szToolTip, sz;
TreeNode locationNode = new TreeNode ( );
TreeNode locationParentNode = new TreeNode ( );
TreeNode childNode = new TreeNode ( );
TreeNode assetNode = new TreeNode ( );
TreeNode assetParentNode = new TreeNode ( );
TreeNode inLimboNode = new TreeNode ( );
TreeNode node = new TreeNode ( );
//add home location node to tree
//
clearCheckedNodes(tv, _session);
tv.Nodes.Clear ( );
if (tv.Nodes.Count == 0)
{
tv.Nodes.Add ( node );
}
// node.ImageUrl = "~/App_Themes/home.gif";
node.ImageUrl = "~/App_Themes/home_purple_16_h.gif";
if (_session.ownerName == null)
_session.ownerName =_session.getOwner();
node.Value = _session.ownerId;
node.Text = _session.ownerName;
if (_session.ownerName.Length > 15)
{
node.Text = _session.ownerName.Substring ( 0, 15 ) + "...";
node.ToolTip = _session.ownerName;
}
node.Selected = true;
node.ChildNodes.Add ( new TreeNode ( "Asset Locations", "-1" ) );
node.ChildNodes[ node.ChildNodes.Count - 1 ].Selected = true;
node.ChildNodes[ node.ChildNodes.Count - 1 ].ToolTip = "To add new Location, go to Location page, click this node, and enter name.";
node.ChildNodes[ node.ChildNodes.Count - 1 ].ImageUrl = "~/App_Themes/home_brown_16_h.gif";
locationParentNode = tv.SelectedNode;
node.ChildNodes.Add ( new TreeNode ( "Orphans", "Orphan" ) );
node.ChildNodes[ node.ChildNodes.Count - 1 ].Selected = true;
node.ChildNodes[ node.ChildNodes.Count - 1 ].ToolTip = "Removing a location will relocate all assets within the location to this node.Text";
node.ChildNodes[ node.ChildNodes.Count - 1 ].ImageUrl = "~/App_Themes/move_to_folder_16_h.gif";
inLimboNode = tv.SelectedNode;
//load asset locations
s = "SELECT * from location WHERE ownerId = '" + _session.ownerId + "' ORDER BY location";
sqlCon con = new sqlCon ( );
sqlCon connection = new sqlCon ( );
try
{
con.connect ( );
con.command ( s );
con.ExecuteReader ( );
while (con.read ( ))
{
//retrieve location record
location = con.get ( "location" );
szToolTip = location;
if (location.Length > 8)
{
a = location.Substring ( 0, 8 ) + "...";
szToolTip = location;
location = a;
}
id = con.get ( "locationId" );
locationParentNode.ChildNodes.Add ( new TreeNode ( location, id ) );
locationParentNode.ChildNodes[ locationParentNode.ChildNodes.Count - 1 ].Selected = true;
locationParentNode.ChildNodes[ locationParentNode.ChildNodes.Count - 1 ].ToolTip = szToolTip;
locationParentNode.ChildNodes[ locationParentNode.ChildNodes.Count - 1 ].ImageUrl = "~/App_Themes/folder_open_16_h.gif";
locationNode = tv.SelectedNode;
if (tv.SelectedNode.ValuePath == _session.nodeIsChecked)
tv.SelectedNode.Checked = true;
locationNode.Selected = true;
locationNode.ShowCheckBox = true; //(bool)Session["showCheck"];
if (_session.nodeToExpand == locationNode.ToolTip)
_session.valuePath = locationNode.ValuePath;
//add assets to location node here
sz = "SELECT * FROM asset WHERE ownerId = '";
sz += _session.ownerId + "' AND locationId = '";
sz += locationNode.Value + "' ORDER BY assetDescription";
//~/App_Themes/incrpt.gif
try
{
connection.connect ( );
connection.command ( sz );
connection.ExecuteReader ( );
string j;
while (connection.read ( ))
{
sz = connection.get ( "assetDescription" );
szToolTip = sz;
if (sz.Length == 0)
sz = "Unknown?";
if (sz.Length > 8)
{
a = sz.Substring ( 0, 8 ) + "...";
szToolTip = sz;
sz = a;
}
int status = int.Parse(connection.get("assetStatus"));
a = connection.get ( "assetId" );
locationNode.ChildNodes.Add ( new TreeNode ( sz, a ) );
locationNode.ChildNodes[ locationNode.ChildNodes.Count - 1 ].Selected = true;
locationNode.ChildNodes[ locationNode.ChildNodes.Count - 1 ].ToolTip = szToolTip;
locationNode.ChildNodes[ locationNode.ChildNodes.Count - 1 ].ImageUrl = getImage ( status );
assetNode = tv.SelectedNode;
//get report info if asset has a report!!
getAssetReports ( assetNode, a, _session );
if(_session.frmLoaded == "warranty")
getWarrantyRecords(assetNode, a, _session);
String k = _session.nodeToExpand;
if (szToolTip == k)
_session.valuePath = locationNode.ValuePath;
}
j = locationNode.Text + "(";
j += locationNode.ChildNodes.Count.ToString ( );
j += ")";
locationNode.Text = j;
}
catch (Exception err)
{
success = false;
}
finally
{
connection.terminate( );
}
//done adding assets to locationnode, set selected node to locationParent node
locationNode.Selected = true;
}
}
catch (Exception err)
{
_util.send(from, to, "Tree: Load Locations", err.Message);
success = false;
}
finally
{
con.terminate();
}
// ----------------------------------------------------------------
// get orpaned assets
sz = "SELECT * FROM asset WHERE ownerId = '";
sz += _session.ownerId + "' AND locationId = -1";
sz += " ORDER BY assetDescription";
inLimboNode.Selected = true;
try
{
connection.connect ( );
connection.command ( sz );
connection.ExecuteReader ( );
string j;
while (connection.read ( ))
{
sz = connection.get ( "assetDescription" );
szToolTip = sz;
if (sz.Length == 0)
sz = "Unknown?";
if (sz.Length > 8)
{
a = sz.Substring ( 0, 8 ) + "...";
szToolTip = sz;
sz = a;
}
a = connection.get ( "assetId" );
inLimboNode.ChildNodes.Add ( new TreeNode ( sz, a ) );
inLimboNode.ChildNodes[ inLimboNode.ChildNodes.Count - 1 ].Selected = true;
inLimboNode.ChildNodes[ inLimboNode.ChildNodes.Count - 1 ].ToolTip = szToolTip;
if (szToolTip == _session.nodeToExpand)
_session.valuePath = inLimboNode.ValuePath;
} //end while
j = inLimboNode.Text + "(";
j += inLimboNode.ChildNodes.Count.ToString ( );
j += ")";
inLimboNode.Text = j;
}
catch (Exception err)
{
success = false;
}
finally
{
connection.terminate();
locationParentNode.Text = "Asset Locations";
}
if (_session.orphanExpanded)
tv.SelectedNode.Parent.Expand ( );
return(success);
}
//---------------------------------------------------------------------------
public string checkStatus ( string a, _sessions _session )
{
string s;
string imgUrl;
s = "SELECT * FROM rptItems WHERE ownerId = '" + _session.ownerId;
s += "' AND assetId = " + a;
imgUrl = "~/App_Themes/ok.gif";
sqlCon con = new sqlCon ( );
try
{
con.connect ( );
con.command ( s );
con.ExecuteReader ( );
while (con.read ( ))
{
bool OK = bool.Parse ( con.get ( "recovered" ).ToString ( ) );
if (!OK) //if not recovered show stolen icon
{
return ("~/App_Themes/theft.gif");
}
else
imgUrl = "~/App_Themes/reco.gif";
}
}
catch (Exception err)
{
}
finally
{
con.terminate();
}
return (imgUrl);
}
//---------------------------------------------------------------------------
public void getAssetReports ( TreeNode assetNode, string a, _sessions _session )
{
string sz, szToolTip, id;
sz = "SELECT * FROM rptItems WHERE ownerId = '";
sz += _session.ownerId + "' AND assetId = " + int.Parse ( a );
sqlCon connection = new sqlCon ( );
try
{
connection.connect ( );
connection.command ( sz );
connection.ExecuteReader ( );
while (connection.read ( ))
{
id = connection.get ( "rptId" ).ToString ( ); //report id
szToolTip = getAssetStatus(a, _session);
szToolTip += getReportDescription ( id, _session );
if (szToolTip.Length == 0)
szToolTip = "Unknown?";
if (szToolTip.Length > 50)
{
a = szToolTip.Substring ( 0, 50 ) + "...";
szToolTip = a;
}
string dt = _util.dateToString(DateTime.Parse(_session.rptDate));
assetNode.ChildNodes.Add ( new TreeNode ( dt, a ) );
assetNode.ChildNodes[ assetNode.ChildNodes.Count - 1 ].Selected = true;
assetNode.ChildNodes[ assetNode.ChildNodes.Count - 1 ].ToolTip = szToolTip;
assetNode.ChildNodes[ assetNode.ChildNodes.Count - 1 ].ImageUrl = "~/App_Themes/copy_clipboard_16_h.gif";
assetNode.ChildNodes[ assetNode.ChildNodes.Count - 1 ].Value = id;
} //end while
}
catch (Exception err)
{
}
finally
{
connection.terminate();
}
}
//---------------------------------------------------------------------------
public void getWarrantyRecords ( TreeNode assetNode, string a, _sessions _session )
{
string sz, szToolTip="", id;
sz = "SELECT * FROM warrantyTransaction WHERE ownerId = '";
sz += _session.ownerId + "' AND assetId = " + int.Parse ( a );
sqlCon connection = new sqlCon ( );
try
{
connection.connect ( );
connection.command ( sz );
connection.ExecuteReader ( );
while (connection.read ( ))
{
id = connection.get ( "id" ).ToString ( ); //report id
szToolTip = "W: ";
szToolTip += connection.get("comment");
if (szToolTip.Length == 0)
szToolTip = "Unknown?";
a = szToolTip;
if (szToolTip.Length > 15)
{
a = szToolTip.Substring ( 0, 10 ) + "...";
if (szToolTip.Length > 30)
szToolTip = szToolTip.Substring ( 0, 30 ) + "..."; ;
}
string dt = _util.dateToString ( DateTime.Parse ( _session.rptDate ) );
assetNode.ChildNodes.Add ( new TreeNode ( a ) );
assetNode.ChildNodes[ assetNode.ChildNodes.Count - 1 ].Selected = true;
assetNode.ChildNodes[ assetNode.ChildNodes.Count - 1 ].ToolTip = szToolTip;
assetNode.ChildNodes[ assetNode.ChildNodes.Count - 1 ].ImageUrl = "~/App_Themes/info.gif";
assetNode.ChildNodes[ assetNode.ChildNodes.Count - 1 ].Value = id;
} //end while
}
catch (Exception err)
{
}
finally
{
connection.terminate ( );
}
}
//---------------------------------------------------------------------------
public string getReportDescription ( string id, _sessions _session )
{
string sz;
sz = "SELECT * FROM report WHERE rptId = " + id;
sz += " AND ownerId = '" + _session.ownerId + "'";
sqlCon connection = new sqlCon ( );
try
{
connection.connect ( );
connection.command ( sz );
connection.ExecuteReader ( );
if (connection.read ( ))
{
sz = connection.get ( "rptComment" ).ToString ( );
_session.rptDate = connection.get ( "rptDate" );
}
}
catch (Exception err)
{
}
finally
{
connection.terminate();
}
return (sz);
}
//---------------------------------------------------------------------------
public void expandNodes(TreeView tv, _sessions _session)
{
TreeNode node;
tv.Nodes[ 0 ].Selected = true; //root node
node = tv.SelectedNode;
if (_session.valuePath != "")
{
node = tv.FindNode ( _session.valuePath );
switch(node.Depth)
{
case 0:
break;
case 1:
node.Parent.Expand ( );
break;
case 2:
node.Parent.Parent.Expand ( );
node.Parent.Expand ( );
break;
case 3:
node.Parent.Parent.Parent.Expand ( );
node.Parent.Parent.Expand ( );
node.Parent.Expand ( );
break;
case 4:
node.Parent.Parent.Parent.Parent.Expand ( );
node.Parent.Parent.Parent.Expand ( );
node.Parent.Parent.Expand ( );
node.Parent.Expand ( );
break;
}
node.Expand ( );
node.Selected = true;
}
}
//-----------------------------------------------------------------
public bool reAssign ( string s )
{
sqlCon con = new sqlCon();
return(con.execSQL(s));
}
//---------------------------------------------------------------------------
public void setNewLocation(TreeView tv, _sessions _session)
{
//if in transfer mode && only one node checked, ok to transfer
if (_session.transferIsOn && nodesChecked (tv, tv.SelectedNode, _session ))
{
string s;
s = "UPDATE asset SET locationId = " + _session.transferNode;
s += " WHERE assetId = " + _session.node.Value;
//if parent node = orphan, orphaned asset has been selected
//or an asset within a location has been selected
if (tv.SelectedNode.Parent.Value == "Orphan" || _session.nodeDepth == 3)
{
_session.nodeToExpand = _session.node.ToolTip;
if (tv.SelectedNode.Depth == 3)
_session.nodeToExpand = tv.SelectedNode.ToolTip;
reAssign ( s );
tvload ( tv, _session );
expandNodes ( tv, _session );
}
}
}
//-----------------------------------------------------------------
public bool nodesChecked ( TreeView tv, TreeNode node, _sessions _session )
{
bool success = true;
//can only re-assign to one location at a time.
try
{
if (tv.CheckedNodes.Count > 1 && _session.transferIsOn)
{
//if more than one location is checked, uncheck all other than node selected
foreach (TreeNode n in tv.CheckedNodes)
{
if (n.Value == "Orphan")
{
n.Checked = false; // node is orphan node, this can never be checked
return (success);
}
if (n != node)
n.Checked = false; // (n) node is checked, un-check it
if (tv.CheckedNodes.Count == 1) //
break;
}
}
}
catch (Exception)
{
success = false;
}
return (success);
}
//-----------------------------------------------------------------
public string getAssetStatus ( string a, _sessions _session )
{
string s = "" ;
s = "SELECT * FROM asset WHERE assetId = " + a;
s += " AND ownerId = '" + _session.ownerId + "'";
sqlCon con = new sqlCon ( );
try
{
con.connect ( );
con.command ( s );
con.ExecuteReader ( );
if(con.read())
{
switch(int.Parse(con.get("assetStatus").ToString()))
{
case 0:
break;
case 1:
s = ".....: ";
break;
case 2:
s = ".....: ";
break;
case 3:
s = ".....: ";
break;
case 4:
s = ".....: ";
break;
case 5:
s = ".....: ";
break;
case 6:
s = ".....: ";
break;
default:
s = "";
break;
}
}
}
catch (Exception )
{
//System.Diagnostics.Debug.Write(err.ToString(), "reAssign");
return (" ");
}
finally
{
con.terminate ( );
int count = con.openConnectionsCount;
}
return (s);
}
//-----------------------------------------------------------------
public string getImage(int status)
{
string s= "";
switch (status)
{
case 0:
s = "~/App_Themes/ok.gif";
break;
case 1:
s = "~/App_Themes/fire.gif";
break;
case 2:
s = "~/App_Themes/theft.gif";
break;
case 3:
s = "~/App_Themes/redDot.gif";
break;
case 4:
s = "~/App_Themes/fire.gif";
break;
case 5:
s = "~/App_Themes/redDot.gif";
break;
case 6:
s = "~/App_Themes/reco.gif";
break;
default:
s = "~/App_Themes/ok.gif";
break;
}
return(s);
}
//-----------------------------------------------------------------
public void clearCheckedNodes(TreeView tv, _sessions _session)
{
TreeNode node=null;
foreach (TreeNode n in tv.CheckedNodes)
if (n.Checked)
{
node = n;
}
if(node != null)
node.Checked = false;
if (tv.CheckedNodes.Count > 0)
clearCheckedNodes(tv, _session);
}
}
Ruchi RPosted Dec 25, 2009, 12:22 AM
Hi theLizard, could u plz explain it in detail .my problem is as written above.
theLizardPosted Dec 24, 2009, 3:59 PM
Hiren SoniPosted Dec 24, 2009, 10:47 AM
Treeview1.ExpandAll();
to expand all treeview. You can write this in page load event.
if you need more help, then tell me...?