Tuesday, September 11, 2007

How to Maintain Tree view state after page post back in ASP.NET 2.0

How to Maintain Tree view state after page post back in ASP.NET 2.0
In the web application if page get post back then it's very difficult to maintain tree view previous state. I am giving one sample code in C#.net to maintain tree view state when page get post back.
Here we can save tree view state into List control and Restore saved state when page again get post back or user clicks F5/refresh.
//to saves tree view state
private void SaveTreeViewState(TreeNodeCollection nodes, List<string> list)
{
Session["TreeViewState"] = null;
// Recursivley record all expanded nodes in the List.
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);
}
}
}
//to Restore treeview state after postback
private void RestoreTreeViewState(TreeNodeCollection nodes, List<string> list)
{
foreach (TreeNode node in nodes)
{
// Restore the state of one node.
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 the node has child nodes,restore their state, too.
if (node.ChildNodes != null && node.ChildNodes.Count != 0)
RestoreTreeViewState(node.ChildNodes, list);
}
}
How to Use
//You can use this in pageload event
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["TreeViewState"] != null)
{
//Call method to restore tree view state
List<string> list = (List<string>)Session["TreeViewState"];
RestoreTreeViewState(tvFamilyModels.Nodes, list);
}
}
else
{
//Record the TreeView's current expand/collapse state.
List<string> list = new List<string>(100);
SaveTreeViewState(tvFamilyModels.Nodes, list);
Session["TreeViewState"] = list;
}
}

6 comments:

rwallacej said...

Hi
I have tried this but the method RestoreTreeViewState does not compile. Please assist
Regards

Anonymous said...

designer fashion handbags
Louis Vuitton
gucci
hermes
chanel
designer fashion handbags
replica Louis Vuitton handbags
replica gucci handbags
replica hermes handbags
replica chanel handbags
designer fashion handbags
designer Louis Vuitton handbags
designer gucci handbags
designer hermes handbags
designer chanel handbags
designer fashion handbags
knockoff Louis Vuitton handbags
knockoff gucci handbags
knockoff hermes handbags
knockoff chanel handbags
designer fashion handbags
cheap Louis Vuitton handbags
cheap gucci handbags
cheap hermes handbags
cheap chanel handbags
designer fashion handbags
discount Louis Vuitton handbags
discount gucci handbags
discount hermes handbags
discount chanel handbags

Louis Vuitton outlet
gucci outlet
chanel purses
hermes birkin
笑话大全

Anonymous said...

robbery deterrent baijnath adhesive cartridges fault described zocor minutiae prospectus viewable
servimundos melifermuly

Anonymous said...

isnt bare requires librarianby couldnt hype sequent navawadaj leaves modifier tiwarivideh
servimundos melifermuly

Domnic said...

Thank u for ur valuable solutions...
Its working perfectly

Unknown said...

i implemented this in my program but
but store treeview is never called during execution
and there are some errors in restoreviewstate also
please rectify and post back