aspnet-4015-components-tree-list-examples-how-to-bind-the-aspxtreelist-to-data-created-at-runtime.md
To bind an ASPxTreeList control to data created at runtime, handle the Page_Init or Page_Load event. In the event handler, assign a data source to the tree list’s DataSource property and call the DataBind() method:
<dx:ASPxTreeList ID="ASPxTreeList1" runat="server"></dx:ASPxTreeList>
using DevExpress.Web.ASPxTreeList;
using DevExpress.Web.Internal;
using System;
using System.Data;
using System.Data.OleDb;
using System.Web;
public partial class _Default : System.Web.UI.Page {
protected override void Page_Init(EventArgs e) {
InitTreeList(ASPxTreeList1);
ASPxTreeList1.DataBind();
}
private void InitTreeList(ASPxTreeList tl) {
tl.SettingsSelection.AllowSelectAll = true;
tl.SettingsSelection.Enabled = true;
tl.KeyFieldName = "ID";
tl.ParentFieldName = "ParentID";
tl.DataSource = CreateDataTable(CreateDataAdapter());
}
OleDbDataAdapter CreateDataAdapter() {
string connectionString =
@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Databases\Departments.mdb";
OleDbConnection myConnection = new OleDbConnection(connectionString);
string query =
"SELECT [ID], [ParentID], [Department], [Budget], [Location] FROM [Departments]";
return new OleDbDataAdapter(query, myConnection);
}
DataTable CreateDataTable(OleDbDataAdapter myAdapter) {
DataTable dt = new DataTable();
DataSet testData = new DataSet();
myAdapter.Fill(testData);
return testData.Tables[0];
}
}
Imports DevExpress.Web.ASPxTreeList
Imports DevExpress.Web.Internal
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Web
Public Partial Class _Default
Inherits System.Web.UI.Page
Protected Overrides Sub Page_Init(ByVal e As EventArgs) Handles Me.Init
InitTreeList(ASPxTreeList1)
ASPxTreeList1.DataBind()
End Sub
Private Sub InitTreeList(ByVal tl As ASPxTreeList)
tl.SettingsSelection.AllowSelectAll = True
tl.SettingsSelection.Enabled = True
tl.KeyFieldName = "ID"
tl.ParentFieldName = "ParentID"
tl.DataSource = CreateDataTable(CreateDataAdapter())
End Sub
Private Function CreateDataAdapter() As OleDbDataAdapter
Dim connectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Databases\Departments.mdb"
Dim myConnection As OleDbConnection = New OleDbConnection(connectionString)
Dim query As String = "SELECT [ID], [ParentID], [Department], [Budget], [Location] FROM [Departments]"
Return New OleDbDataAdapter(query, myConnection)
End Function
Private Function CreateDataTable(ByVal myAdapter As OleDbDataAdapter) As DataTable
Dim dt As DataTable = New DataTable()
Dim testData As DataSet = New DataSet()
myAdapter.Fill(testData)
Return testData.Tables(0)
End Function
End Class