How to save the state of a Treeview in ASP.NET / C#?

Asked

Viewed 210 times

0

I have a treeview that is persisted in Database and, from the application, it is possible to add or remove items, both from TV and BD. Big problem is that I can’t find a simple way to save the user’s "usage" state (Expandednodes) to improve application usability.

In all forums and solutions I searched put as necessary to save the nodes in Session or Cookie and then perform the addition of nodes recursively. So far so good, meets with the first goal: Save the state of use of TV. However, bar the possibility to add and remove items dynamically, remembering that the data comes from the BD.

Does anyone know any method of dealing with this issue?

Below are the codes I’m using to save and restore the TV:

public void SaveTreeView(TreeView treeView, string key, HttpContext context)
{
    context.Session[key] = treeView.Nodes;
}

public void RestoreTreeView(TreeView treeView, string key, HttpContext context)
{
    if (new TreeViewState(key).IsSaved)
    {
        treeView.Nodes.Clear();

        TreeNodeCollection nodes = (TreeNodeCollection)context.Session[key];
        for (int index = nodes.Count - 1; index >= 0; index--)
        {
            treeView.Nodes.AddAt(0, nodes[index]);
        }
        this.SaveTreeView(treeView, key, context);
    }
}

Below follows the algorithm you were trying to write to deal with the problem of insertions and deletions and his call on page_load():

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        state.RestoreTreeViewAfterUpdate(tvwCadastro, "TreeViewState", HttpContext.Current);
    }
}

public void RestoreTreeViewAfterUpdate(TreeView treeView, string key, HttpContext context)
{
    RestoreTreeView(treeView, key, context);

    SystemUnderTestDB sutDB = new SystemUnderTestDB();
    TesteDB tesDB = new TesteDB();

    foreach (DataRow drTeste in tesDB.SelecionarTesteSUT(Convert.ToInt32(context.Session["SUT"])).Tables[0].DefaultView)
    {
        TreeNode c = new TreeNode() { Text = drTeste["tes_titulo"].ToString(), Value = "2" + drTeste["tes_codigo"].ToString() };

        foreach (TreeNode nodeSUT in treeView.Nodes)
        {
            if (!treeView.Nodes.Contains(c))
            {
                TreeNode node = new TreeNode() { Text = drTeste["tes_titulo"].ToString(), Value = "2" + drTeste["tes_codigo"].ToString() };
                treeView.Nodes.Add(node);
            }
        }
    }
}
  • From what I realize your problem is purely of algorithm (how you are saving and getting these values), really the best alternative to these cases is the use of Session, managing it whenever you add or remove a node.

  • Exactly, it’s certainly an algorithm problem, but I haven’t been able to identify anything practical for checking the tuples in relation to the Treeview nodes. I will edit the text and include the algorithm I was trying to write to deal with this problem.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.