How to: Add New Nodes in Code (Bound Mode)

This example shows how to add new nodes in code. The ASPxTreeList is bound to a Sql Database.

using System.Data.SqlClient;

protected void Button1_Click(object sender, EventArgs e) {
    SqlConnection conn = new SqlConnection();
    conn.ConnectionString= "Data Source=(local);Initial Catalog=TestDb;Integrated Security=True";
    conn.Open();
    int id = GenerateId();
    int parentId = 0;

    if (ASPxTreeList1.FocusedNode != null)
        parentId = (int)ASPxTreeList1.FocusedNode["ID"];

    string textCmd = string.Format("INSERT INTO [Table1] ([ID], [ParentID], [Name]) VALUES ({0},
            {1}, '{2}')", id, parentId, ASPxTextBox1.Text);
    SqlCommand cmd = new SqlCommand(textCmd, conn);
    cmd.ExecuteNonQuery();
    conn.Close();
    ASPxTreeList1.DataBind();
}

The animation below shows the result:

ASPxTreeList_AddNewNodes