RadTreeView for ASP.NET

Editing TreeNodes' Text when using templates Send comments on this topic.
TroubleShooting > Editing TreeNodes' Text when using templates

Glossary Item Box

When using templates for the TreeNodes, the edit of the Text of these TreeNodes becomes a complicated task.

The approach shown in the example below shows an elegant way to edit the nodes' text, save it in the database and then populate the treeview with the new data.

Example:

This example shows a treeview that is bound to a database. It uses templates for its TreeNodes. The template displays the Text of the TreeNodes using DataBinder.Eval expression.

ASPX Copy Code
<rad:RadTreeView
   
ID="RadTree1"
   
runat="server"
   
AutoPostBackOnCheck="False"
   
AllowNodeEditing="true"
   
OnNodeEdit="HandleNodeEdit">
   
<NodeTemplate>
       
node text:
       
<%# DataBinder.Eval(Container.DataItem, "Text") %>
   
</NodeTemplate>
</
rad:RadTreeView>
        
C# Copy Code
protected void Page_Load(object sender, EventArgs e)
{
       
if (!Page.IsPostBack)
       {
           GenerateTreeView();
       }

}

protected void HandleNodeEdit(object sender, RadTreeNodeEventArgs NodeEvents)
{
   RadTreeNode nodeEdited = NodeEvents.NodeEdited;
   
string newText = NodeEvents.NewText;

   OleDbConnection dbCon =
new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/Tree.mdb"));

   
//Update the text of the corresponding row in the database

   
OleDbParameter pNewText = new OleDbParameter("@newText",newText);
   OleDbParameter pOldText =
new OleDbParameter("@oldText",nodeEdited.Text);

   
string cmdString = "Update Links Set [Text] = @NewText WHERE [Text] = @oldText";
   OleDbCommand dbCommand =
new OleDbCommand(cmdString,dbCon);
   dbCommand.CommandType = CommandType.Text;

   dbCommand.Parameters.Add(pNewText);
   dbCommand.Parameters.Add(pOldText);
   
try
   {
       
dbCon.Open();

       dbCommand.ExecuteNonQuery();
   }
   
finally
   {
       
dbCon.Close();
   }
   GenerateTreeView();


}

private void GenerateTreeView()
{
   OleDbConnection dbCon =
new OleDbConnection ("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/Tree.mdb"));
   dbCon.Open();

   OleDbDataAdapter adapter =
new OleDbDataAdapter("SELECT * FROM Links", dbCon);
   DataSet ds =
new DataSet();
   adapter.Fill(ds);

   RadTree1.DataFieldID =
"id";
   RadTree1.DataFieldParentID =
"parentId";
   RadTree1.DataTextField =
"Text";
   RadTree1.DataSource = ds;
   RadTree1.DataBind();
   RadTree1.ExpandAllNodes();
}
        
VB.NET Copy Code
Protected Sub Page_Load(sender As Object, e As EventArgs)
   If Not Page.IsPostBack Then
      GenerateTreeView()
   End If
End Sub 'Page_Load


Protected Sub HandleNodeEdit(sender As Object, NodeEvents As RadTreeNodeEventArgs)
   Dim nodeEdited As RadTreeNode = NodeEvents.NodeEdited
   Dim newText As String = NodeEvents.NewText

   Dim dbCon As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/Tree.mdb"))

   'Update the text of the corresponding row in the database
   Dim pNewText As New OleDbParameter("@newText", newText)
   Dim pOldText As New OleDbParameter("@oldText", nodeEdited.Text)

   Dim cmdString As String = "Update Links Set [Text] = @NewText WHERE [Text] = @oldText"
   Dim dbCommand As New OleDbCommand(cmdString, dbCon)
   dbCommand.CommandType = CommandType.Text

   dbCommand.Parameters.Add(pNewText)
   dbCommand.Parameters.Add(pOldText)
   Try
      dbCon.Open()

      dbCommand.ExecuteNonQuery()
   Finally
      dbCon.Close()
   End Try
   GenerateTreeView()
End Sub 'HandleNodeEdit



Private Sub GenerateTreeView()
   Dim dbCon As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/App_Data/Tree.mdb"))
   dbCon.Open()

   Dim adapter As New OleDbDataAdapter("SELECT * FROM Links", dbCon)
   Dim ds As New DataSet()
   adapter.Fill(ds)

   RadTree1.DataFieldID = "id"
   RadTree1.DataFieldParentID = "parentId"
   RadTree1.DataTextField = "Text"
   RadTree1.DataSource = ds
   RadTree1.DataBind()
   RadTree1.ExpandAllNodes()
End Sub 'GenerateTreeView