New to Telerik UI for ASP.NET AJAXStart a free 30-day trial

Save Made Changes to TreeView to Database

This example demonstrates a simple mechanism using Access database with single self-referencing table containing columns id, parentId and Text.

The structure of the TreeView is loaded from this database in the Page_Load event. Then the user edits the text of random nodes in the TreeView and hits the Save button on the page. Thus, a postback is triggered and the updated information is saved back in the database file.

The code below shows the approach:

C#
protected System.Web.UI.WebControls.Button Button1;
protected Telerik.Web.UI.RadTreeView RadTree1;
protected OleDbCommand command;
protected OleDbConnection con;
protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        GenerateTreeView();
    }
}
private void GenerateTreeView()
{
    OleDbConnection dbCon = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("treeSave.mdb"));
    //the database is in the web app root!
    dbCon.Open();
    OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Links", dbCon);
    DataSet ds = new DataSet();
    adapter.Fill(ds);
    ds.Relations.Add("NodeRelation", ds.Tables[0].Columns["id"], ds.Tables[0].Columns
    ["parentId"]);
    foreach (DataRow dbRow in ds.Tables[0].Rows)
    {
        if (dbRow.IsNull("parentId"))
        {
            RadTreeNode node = CreateNode(dbRow["Text"].ToString(), true);
            RadTree1.Nodes.Add(node);
            RecursivelyPopulate(dbRow, node);
        }
    }
}
private void RecursivelyPopulate(DataRow dbRow, RadTreeNode node)
{
    foreach (DataRow childRow in dbRow.GetChildRows("NodeRelation"))
    {
        RadTreeNode childNode = CreateNode(childRow["Text"].ToString(), true);
        node.Nodes.Add(childNode);
        RecursivelyPopulate(childRow, childNode);
    }
}
private RadTreeNode CreateNode(string text, bool expanded)
{
    RadTreeNode node = new RadTreeNode(text);
    node.Expanded = true;
    return node;
}

private void RadTree1_NodeEdit(object o, Telerik.Web.UI.RadTreeNodeEditEventArgs e)
{
    RadTreeNode edit = e.Node;
    String text = e.Text;
    edit.Text = text;
}
private void Button1_Click(object sender, System.EventArgs e)
{
    con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
    Server.MapPath("treeSave.mdb"));
    con.Open();
    command = new OleDbCommand("DELETE * from Links", con);
    command.ExecuteNonQuery();
    con.Close();
    UpdateNodes();
}
private void UpdateNodes()
{
    OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;DataSource=" + Server.MapPath("treeSave.mdb"));
    con.Open();
    IList<RadTreeNode> allNodes = RadTree1.GetAllNodes();
    for (int i = 0; i < allNodes.Count; i++)
    {
        RadTreeNode node = (RadTreeNode)allNodes[i];
        int parentId = node.ParentNode == null ? -1 : allNodes.IndexOf(node.ParentNode);
        command = new OleDbCommand("INSERT into Links([id],parentId,[Text]) values (@ID,@parentId,@Text)", con);
        command.Parameters.AddWithValue("ID", i.ToString());
        if (parentId > -1)
            command.Parameters.AddWithValue("parentId", parentId);
        else
            command.Parameters.AddWithValue("parentId", System.DBNull.Value);
        command.Parameters.AddWithValue("Text", node.Text);
        command.ExecuteNonQuery();
    }
    con.Close();
} 

See Also

In this article
See Also
Not finding the help you need?
Contact Support