Contents
Licensing
Installation and deployment
DNN
New to Telerik RadTreeView?
Already using the control
Standards Compliance
AJAX Support
Feature OverView
How RadTreeView compares to ASP.NET Treeview
ASP.NET 2.0 Features
Templates
Defining TreeView Structure
Data Binding
Controlling the visual appearance
Context Menus
Custom Attributes
Using Checkboxes
Using the Drag and Drop Functionality
Load On Demand Support
Example scenarios (How to)
Telerik RadTreeView Client-Side
Telerik RadTreeView Server-Side
Interoperability with Other Controls
TroubleShooting
API Reference
|
|
| Saving changes made in Telerik RadTreeView to a database |
Send comments on this topic. |
|
|
Example scenarios (How to) > Server-side > Saving changes made in Telerik RadTreeView to a 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:
Example:
| C# |
Copy Code |
|
protected System.Web.UI.WebControls.Button Button1; protected Telerik.WebControls.RadTreeView RadTree1; protected OleDbCommand command; protected OleDbConnection con;
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.AddNode(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.AddNode(childNode); RecursivelyPopulate(childRow, childNode); } }
private RadTreeNode CreateNode(string text, bool expanded) { RadTreeNode node = new RadTreeNode(text); node.Expanded = true;
return node; }
private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { GenerateTreeView(); } }
private void RadTree1_NodeEdit(object o, Telerik.WebControls.RadTreeNodeEventArgs e) { RadTreeNode edit = e.NodeEdited; String text = e.NewText; 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;Data Source=" + Server.MapPath("treeSave.mdb")); con.Open();
ArrayList allNodes = RadTree1.GetAllNodes(); for(int i = 0; i < allNodes.Count; i++) { RadTreeNode node = (RadTreeNode)allNodes[i]; int parentId = allNodes.IndexOf(node.Parent);
command = new OleDbCommand("INSERT into Links([id],parentId,[Text]) values (@ID,@parentId,@Text)",con); command.Parameters.Add("ID",i.ToString()); if(parentId > -1) command.Parameters.Add("parentId",parentId); else command.Parameters.Add("parentId",System.DBNull.Value); command.Parameters.Add("Text",node.Text); command.ExecuteNonQuery();
} con.Close(); } |
| VB.NET |
Copy Code |
|
Imports Telerik.WebControls Imports System.Data.OleDb
Private Sub GenerateTreeView() Dim dbCon As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("treeSave.mdb")) dbCon.Open()
Dim adapter As New OleDbDataAdapter("SELECT * FROM Links", dbCon) Dim ds As New DataSet adapter.Fill(ds)
ds.Relations.Add("NodeRelation", ds.Tables(0).Columns("id"), ds.Tables(0).Columns("parentId"))
Dim dbRow As DataRow For Each dbRow In ds.Tables(0).Rows If dbRow.IsNull("parentId") Then Dim node As RadTreeNode = CreateNode(dbRow("Text").ToString(), True) RadTree1.AddNode(node) RecursivelyPopulate(dbRow, node) End If Next dbRow End Sub
Private Sub RecursivelyPopulate(ByVal dbRow As DataRow, ByVal node As RadTreeNode) Dim childRow As DataRow For Each childRow In dbRow.GetChildRows("NodeRelation") Dim childNode As RadTreeNode = CreateNode(childRow("Text").ToString(), True) node.AddNode(childNode) RecursivelyPopulate(childRow, childNode) Next childRow End Sub
Private Function CreateNode(ByVal [text] As String, ByVal expanded As Boolean) As RadTreeNode Dim node As New RadTreeNode([text]) node.Expanded = True
Return node End Function
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then GenerateTreeView() End If End Sub Private Sub RadTree1_NodeEdit(ByVal o As Object, ByVal e As Telerik.WebControls.RadTreeNodeEventArgs) Handles RadTree1.NodeEdit Dim edit As RadTreeNode = e.NodeEdited Dim [text] As String = e.NewText edit.Text = [text] End Sub
Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 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() End Sub
Private Sub UpdateNodes() Dim con As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("treeSave.mdb")) con.Open()
Dim allNodes As ArrayList = RadTree1.GetAllNodes() Dim i As Integer For i = 0 To allNodes.Count - 1 Dim node As RadTreeNode = CType(allNodes(i), RadTreeNode) Dim parentId As Integer = allNodes.IndexOf(node.Parent)
command = New OleDbCommand("INSERT into Links([id],parentId,[Text]) values (@ID,@parentId,@Text)", con) command.Parameters.Add("ID", i.ToString()) If parentId > -1 Then command.Parameters.Add("parentId", parentId) Else command.Parameters.Add("parentId", System.DBNull.Value) End If command.Parameters.Add("Text", node.Text) command.ExecuteNonQuery() Next i con.Close() End Sub End Class
|
|