.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> ASP.Net >> Post New QuestionBookmark and Share Subscribe to Forum

treeview expand only selected parent nodes in asp.net

Posted By: Dom     Posted Date: February 16, 2010    Points:2   Category :ASP.Net
Im using Treeview in asp.net
if i select any parent node then it should be expand and other parents shoulb be collapsed after page postback(navigation)...im using code below.

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["TreeViewState"] != null)
{
List<string> list = (List<string>)Session["TreeViewState"];
RestoreTreeViewState(TreeViewApplicationSetup.Nodes, list);
}
}
else
{
List<string> list = new List<string>(100);
SaveTreeViewState(TreeViewApplicationSetup.Nodes, list);
Session["TreeViewState"] = list;
}
}



private void SaveTreeViewState(TreeNodeCollection nodes, List<string> list)
{
Session["TreeViewState"] = null;
foreach (TreeNode node in nodes)
{
if (node.ChildNodes != null)
{
if (node.Expanded.HasValue && node.Expanded == true && !String.IsNullOrEmpty(node.Text))
{
list.Add(node.Text);
}
if (node.ShowCheckBox == true && node.ChildNodes.Count == 0 && node.Parent.Expanded == true)
{
if (node.Checked == true)
{
list.Add(node.ValuePath + "-T");
}
else
{
list.Add(node.ValuePath + "-F");
}
}
SaveTreeViewState(node.ChildNodes, list);
}
}
}
private void RestoreTreeViewState(TreeNodeCollection nodes, List<string> list)
{
foreach (TreeNode node in nodes)
{

if (list.Contains(node.Text) || list.Contains(node.ValuePath + "-T") || list.Contains(node.ValuePath + "-F"))
{
if (node.ChildNodes != null && node.ChildNodes.Count != 0 && node.Expanded.HasValue && node.Expanded == false)
{
if (node.Parent != null)
{
if (list.Contains(node.ChildNodes[0].ValuePath + "T") || list.Contains(node.ChildNodes[0].ValuePath + "-F"))
{
node.Expand();
}
}
else
{
node.Expand();
}
}
else if (node.ChildNodes != null && node.Expanded.HasValue && node.Expanded == false)
{
if (node.ShowCheckBox == true && list.Contains(node.Parent.Text) && list.Contains(node.Parent.Parent.Text))
{
if (list.IndexOf(node.ValuePath + "-T") != -1)
{
node.Checked = true;

}
else if (list.IndexOf(node.ValuePath + "-F") != -1)
{
node.Checked = false;

}
}
}
}
else
{
if (node.ChildNodes != null && node.ChildNodes.Count != 0 && node.Expanded.HasValue && node.Expanded == true)
{
node.Collapse();
}
}
if (node.ChildNodes != null && node.ChildNodes.Count != 0)
{
RestoreTreeViewState(node.ChildNodes, list);
}
}
}



its jsut help me to expand parent nodes in every postback but othere parents node didnt collapsed....

Responses
Author: Narayanan             
Posted Date: October 12, 2011     Points: 20   

hi,
refer this
http://www.codeproject.com/KB/webforms/ClientSideTreeView.aspx

Regards, Lakshmi Naraayanan.S
http://dotnettechrocks.blogspot.in/


Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend