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

0

I have a treeview that is persisted in Database, and from the application, you can add or remove items from both the TV and the BD. Big problem is that I can not find a simple way to save the user's "use" status (ExpandedNodes) to improve the usability of the application.

In all the forums and solutions that I researched, it is necessary to save the nodes in Session or Cookie and then perform the addition of the nodes recursively. So far so good, it fulfills the first objective: Save the state of use of the TV. However, it bar on the possibility of adding and removing items dynamically, remembering that the data comes from the DB.

Does anyone know of any method to handle this?

Here 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 is the algorithm you were trying to write to handle the insertion and deletion problem and its 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);
            }
        }
    }
}
    
asked by anonymous 03.09.2014 / 15:01

0 answers