TreeView Caching

B

Bishop

I have a treeview in my master page. It's built off a sql table that looks
like this:
TreeID, Name, ParentID
1,shoes,0
2,boots,0
3,slippers,0
4,shoe laces, 1
5, shoe soles, 1
6, boot laces, 2
7, slipper ears, 3
8, slipper ears blue, 7

To populate I do a number of small sql queries. The data is dynamic but
typically will change no more than once a day.

I'm looking for suggestions on how I can cache the treeview so it only runs
the queries a few times a day instead of each time the page loads.
 
G

Guest

Hello Bishop,

One way to do this would be to cache the TreeNodeCollection which is
attached to the TreeView. Something along the lines of the following:

=-=-=-=-
protected void LoadTreeView()
{
TreeNodeCollection tnc;

if (Cache["tnc"] == null) // treenodes not in cache
{
// this is where you load the data from SQL before it's cached.
// in this sample, I'm building the treenode collection
// dynamically.
TreeNodeCollection tncNew = new TreeNodeCollection();

for (int i = 0; i < 5; i++)
{
TreeNode tn = new TreeNode("Node" + i.ToString());
for (int j = 0; j < 3; j++)
{
TreeNode tnChild = new TreeNode("ChildNode" +
j.ToString());
tn.ChildNodes.Add(tnChild);
}
tncNew.Add(tn);
}
// Add it to the cache
Cache.Add("tnc", tncNew, null, DateTime.Now.AddMinutes(2),
TimeSpan.Zero, CacheItemPriority.High, null);
tnc = tncNew;
}
else // treenodes have been cached
{
// Get the collection from the cache
tnc = (TreeNodeCollection) Cache["tnc"];
}
foreach (TreeNode tn in tnc)
{
// TreeView1 is the control id
TreeView1.Nodes.Add(tn);
}
}
 
B

Bishop

This got me on the right course, thanks!

brians said:
Hello Bishop,

One way to do this would be to cache the TreeNodeCollection which is
attached to the TreeView. Something along the lines of the following:

=-=-=-=-
protected void LoadTreeView()
{
TreeNodeCollection tnc;

if (Cache["tnc"] == null) // treenodes not in cache
{
// this is where you load the data from SQL before it's cached.
// in this sample, I'm building the treenode collection
// dynamically.
TreeNodeCollection tncNew = new TreeNodeCollection();

for (int i = 0; i < 5; i++)
{
TreeNode tn = new TreeNode("Node" + i.ToString());
for (int j = 0; j < 3; j++)
{
TreeNode tnChild = new TreeNode("ChildNode" +
j.ToString());
tn.ChildNodes.Add(tnChild);
}
tncNew.Add(tn);
}
// Add it to the cache
Cache.Add("tnc", tncNew, null, DateTime.Now.AddMinutes(2),
TimeSpan.Zero, CacheItemPriority.High, null);
tnc = tncNew;
}
else // treenodes have been cached
{
// Get the collection from the cache
tnc = (TreeNodeCollection) Cache["tnc"];
}
foreach (TreeNode tn in tnc)
{
// TreeView1 is the control id
TreeView1.Nodes.Add(tn);
}
}
--
enjoy - brians
http://www.limbertech.com


Bishop said:
I have a treeview in my master page. It's built off a sql table that
looks
like this:
TreeID, Name, ParentID
1,shoes,0
2,boots,0
3,slippers,0
4,shoe laces, 1
5, shoe soles, 1
6, boot laces, 2
7, slipper ears, 3
8, slipper ears blue, 7

To populate I do a number of small sql queries. The data is dynamic but
typically will change no more than once a day.

I'm looking for suggestions on how I can cache the treeview so it only
runs
the queries a few times a day instead of each time the page loads.
 
B

Bishop

I'm having a weird issue with caching the treeview and the problem is not
exibited in IE only in FireFox. Everything looks like it's working except
it dosn't let me expand the listings. If I set the treeview to fully
expand, I see all the nodes but the compress (un-expand?) dosn't work. From
what I can tell the javascript tags are not getting put in when it's being
displayed from cache. Below is the code I'm using. Any thoughts
appreciated.





If (IsNothing(Cache("Tree-" &
Replace(Request.ServerVariables("SERVER_NAME"), "www.", "")))) Then

popTree(Session("SiteID"))

Dim myTreeNodeCollection As TreeNodeCollection = trvCategories.Nodes

Dim myTreeNodeArray(myTreeNodeCollection.Count - 1) As TreeNode

myTreeNodeCollection.CopyTo(myTreeNodeArray, 0)

Cache.Insert("Tree-" & Replace(Request.ServerVariables("SERVER_NAME"),
"www.", ""), myTreeNodeArray, Nothing, DateTime.Now.AddMinutes(15),
TimeSpan.Zero)

Else

Dim myTreeNodeArray() As TreeNode

myTreeNodeArray = Cache("Tree-" &
Replace(Request.ServerVariables("SERVER_NAME"), "www.", ""))

Dim myNode As TreeNode

For Each myNode In myTreeNodeArray

myNode.Text = myNode.Text

trvCategories.Nodes.Add(myNode)

Next

End If







Bishop said:
This got me on the right course, thanks!

brians said:
Hello Bishop,

One way to do this would be to cache the TreeNodeCollection which is
attached to the TreeView. Something along the lines of the following:

=-=-=-=-
protected void LoadTreeView()
{
TreeNodeCollection tnc;

if (Cache["tnc"] == null) // treenodes not in cache
{
// this is where you load the data from SQL before it's
cached.
// in this sample, I'm building the treenode collection
// dynamically.
TreeNodeCollection tncNew = new TreeNodeCollection();

for (int i = 0; i < 5; i++)
{
TreeNode tn = new TreeNode("Node" + i.ToString());
for (int j = 0; j < 3; j++)
{
TreeNode tnChild = new TreeNode("ChildNode" +
j.ToString());
tn.ChildNodes.Add(tnChild);
}
tncNew.Add(tn);
}
// Add it to the cache
Cache.Add("tnc", tncNew, null, DateTime.Now.AddMinutes(2),
TimeSpan.Zero, CacheItemPriority.High, null);
tnc = tncNew;
}
else // treenodes have been cached
{
// Get the collection from the cache
tnc = (TreeNodeCollection) Cache["tnc"];
}
foreach (TreeNode tn in tnc)
{
// TreeView1 is the control id
TreeView1.Nodes.Add(tn);
}
}
--
enjoy - brians
http://www.limbertech.com


Bishop said:
I have a treeview in my master page. It's built off a sql table that
looks
like this:
TreeID, Name, ParentID
1,shoes,0
2,boots,0
3,slippers,0
4,shoe laces, 1
5, shoe soles, 1
6, boot laces, 2
7, slipper ears, 3
8, slipper ears blue, 7

To populate I do a number of small sql queries. The data is dynamic but
typically will change no more than once a day.

I'm looking for suggestions on how I can cache the treeview so it only
runs
the queries a few times a day instead of each time the page loads.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top