• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 796
  • Last Modified:

ASP.NET TreeView InsertButton at selected node.

I have an access database which I am displaying the data from just fine. My issue lies within adding a new node. I have made a button, with assistance from another post on this site, that inserts a node at a hardcoded position. I would like for this button to insert at my currently selected node within the tree.

My other issue, a small one, whenever I select a node in my tree it will always show the first node as selected. I see for a split second that the proper node is selected but it does not remain selected within the browser.

Any ideas?
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        LinksTreeView.Nodes[0].ChildNodes[0].Selected = true;
        TreeNode tnParent = new TreeNode();
        tnParent = this.LinksTreeView.SelectedNode.Parent;
        TreeNode sn = new TreeNode(this.TextBox1.Text);
        tnParent.ChildNodes.Add(sn);
    }
}
 
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.OleDb" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<script runat="server">
 
    void PopulateNode(Object sender, TreeNodeEventArgs e)
    {
      PopulateParents(e.Node);
    }
    
    void PopulateParents(TreeNode node)
    {
        OleDbConnection myConn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("test_db.mdb"));
        OleDbCommand myComm = new OleDbCommand("SELECT id, parent, html, expanded, icon FROM test_tree WHERE parent = 0", myConn);
        myConn.Open();
        OleDbDataReader myReader = myComm.ExecuteReader();
        
        while (myReader.Read())
        {
            TreeNode newNode = new TreeNode();
            newNode.Text = myReader.GetString(2).ToString();
            newNode.Value = "1";
            node.ChildNodes.Add(newNode);
        }
        myReader.Close();
        myConn.Close();
    }
    
</script>
 
<html>
<head id="Head1" runat="server">
<title>TreeView Population</title>
</head>
<body>
    <form id="form1" runat="server">
      <asp:TreeView id="LinksTreeView" Font-Names= "Arial" ForeColor="Blue" EnableClientScript="true" PopulateNodesFromClient="true" OnTreeNodePopulate="PopulateNode" runat="server">
      <SelectedNodeStyle ForeColor="Red" />
        <Nodes>
          <asp:TreeNode Text="TestDB" 
            SelectAction="Expand"  
            PopulateOnDemand="true"/>
        </Nodes>
      </asp:TreeView>
      <br /><br />
      <asp:Label id="Message" runat="server"/>
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Insert Node" />
    <asp:TextBox ID="TextBox1" runat="server" />
    </form>
  </body>
</html>

Open in new window

0
Seditio
Asked:
Seditio
  • 3
  • 2
2 Solutions
 
Bob LearnedCommented:
Do you want to add the node as a sibling to the selected node, or a child?
0
 
SeditioAuthor Commented:
As a child to the selected node.

One other thing I noticed while trying things out is that the nodes that I add work just fine when I select them, as in they get highlighted as red and remain that way. The nodes I add initially still however do no remain selected.
0
 
Bob LearnedCommented:
Are you looking for something like this?



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title>CSS Example</title>
  <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
  <form id="form1" runat="server">
    <div>
      &nbsp;</div>
    &nbsp;
    <asp:TreeView ID="TreeView1" runat="server" Height="292px" Width="377px" OnSelectedNodeChanged="TreeView1_SelectedNodeChanged">
      <SelectedNodeStyle ForeColor="White" BackColor="SteelBlue" />
      <NodeStyle ForeColor="Black" />
    </asp:TreeView>
    <asp:Label ID="ResourceLabel" runat="server" Text="Resource:" />
    <asp:DropDownList ID="ResourceList" runat="server" Enabled="False">
      <asp:ListItem Text="(Select)" Value="0" Selected="True" />
      <asp:ListItem Text="Jim" Value="1" />
      <asp:ListItem Text="John" Value="2" />
      <asp:ListItem Text="Greg" Value="3" />
    </asp:DropDownList>
    <asp:Button ID="AddResourceButton" runat="server" OnClick="AddResourceButton_Click"
      Text="Add Resource" Enabled="False" />
  </form>
</body>
</html>

Open in new window

0
 
Bob LearnedCommented:
Code-behind:
using System;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Web.UI;
using System.ComponentModel;
using System.Data.SqlClient;
 
public partial class _Default : System.Web.UI.Page
{
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!this.Page.IsPostBack)
            this.LoadTree();
    }
 
    private void LoadTree()
    {
        TreeNode rootNode = new TreeNode("Application");
 
        string[] applicationList = new string[] { "CLEAR", "Aqua", "SmartFlow" };
 
        foreach (string name in applicationList)
        {
            TreeNode applicationNode = new TreeNode(name);
 
            rootNode.ChildNodes.Add(applicationNode);
        }
        this.TreeView1.Nodes.Add(rootNode);
        rootNode.Expand();
    }
 
    protected void AddResourceButton_Click(object sender, EventArgs e)
    {
        this.AddResource(this.ResourceList.SelectedItem.Text);
    }
 
    private void AddResource(string resourceName)
    {
        if (this.TreeView1.SelectedNode != null)
        {
            TreeNode resourceNode = new TreeNode(resourceName);
 
            this.TreeView1.SelectedNode.ChildNodes.Add(resourceNode);
 
            this.TreeView1.SelectedNode.Expand();
        }
    }
    protected void TreeView1_SelectedNodeChanged(object sender, EventArgs e)
    {
        this.AddResourceButton.Enabled = true;
        this.ResourceList.Enabled = true;
    }
}

Open in new window

0
 
SeditioAuthor Commented:
Yes, exactly. Thanks!
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now