'Reusing a treeview control accross different aspx pages
I am building an intranet application for my company to manage departments and employees. It has a masterpage with the menu and some authentication code.
In the admin section, I need to load a treeview with all departments and all employees. I load it recursively from an SQL database. It takes about 30 seconds or more to load and display (it is 4 or 5 levels deep, has about 60 departments and 500 employees).
Some of the admin pages display this treeview, others a different treeview and yet others no treeview at all, so puting it in the masterpage doesn't seem right.
I need a way to store this treeview if the user navigates between pages so that it would not take 30 seconds each time a user goes to a page with this treeview. So far, I came up with the approach to save the whole treeview in a session variable and when the user visits a page that has this treeview, if it exists in the session variables, I load it from there, if not, I load it from the database and put it in the session variables as well. I was wonderring if this is a good method to do it or not, especially since I noticed that after I retrieve the treeview for the frst time from the session variables, it seems to dissapear from there, so I have to put it back after each retrieval. This gives me the odd feeling that I may not properly understand what is going on.
My code is:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["SessionStoredTreeView"] == null)
{
RecursivelyLoarTreeView(); //takes 30 seconds
Session["SessionStoredTreeView"] = TreeViewOnPage;
}
else
{
TreeView tv = (TreeView)Session["SessionStoredTreeView"];
TreeViewOnPage.Nodes.Clear();
TreeViewOnPage.Nodes.Add(tv.Nodes[0]);
Session["SessionStoredTreeView"] = TreeViewOnPage;
}
}
}
Solution 1:[1]
Try caching some parts of your page or user control :
- https://asp.net-tutorials.com/caching/output-cache/
- https://docs.microsoft.com/en-us/previous-versions/aspnet/h30h475z(v=vs.100)
- https://docs.microsoft.com/en-us/previous-versions/aspnet/k4he1ds5(v=vs.100)
Add the treeview control only in the content place holder of the ASPX pages where it is supposed to appear. Of course, your master page must be declared in these pages.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
