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

SeditioAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.