Treeview Datasource for Linq to Entities versus a SQL Command in Code-Behind versus Code-In-Front

In this example, I'll show the differences between populating a Treeview using Linq to Entities, versus a SQL Command in Code-Behind using a function, versus Code-In-Front using a SelectCommand.

In creating a Treeview control, I found some differences in assigning the datasource. I list 3 different methods. In my example treeview, I am getting the menu name, preceded by the numeric list order. (This treeview is used for admin purposes to list all menu items, including menu dividers, so they can be selected and populate a form for editing of the menu item, thus the Home item #3 shows a --- to represent an editable menu divider.)

To help you understand what we're populating, our finished admin treeview will look like this:

Here is my TreeView (I happen to be using the Telerik Treeview, but hopefully enough will be applicable to the .net treeview. Feel free to let me know any discrepancies for our readers!):

<telerik:RadTreeView ID="tvMenu" runat="server">
</
telerik:RadTreeView>


1) Populating with a Datasource and SelectCommand in the code-in-front:

<asp:SqlDataSource ID="SqlDataSource1"
runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT * FROM [myMenu]">
</asp:SqlDataSource>

Codebehind:

With tvMenu .DataSourceID = "sqldatasource1" ' if defined a datasource in the code-in-front
.DataFieldID = "menuId"
.DataFieldParentID = "parentMenuId"
.DataTextField = "menuName"
.DataValueField = "menuId"
.CausesValidation = False
.Height = 300
.DataBind()
.ExpandAllNodes()

End With

Notice in the above example, we use a string with the datasource name from the code in front to define the datasourceID.

Our NodeDataBound looks like this:

Protected Sub tvMenu_NodeDataBound(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadTreeNodeEventArgs) Handles tvMenu.NodeDataBound

Try

Dim item As RadTreeNode = e.Node

Dim dataRow As DataRowView = CType(e.Node.DataItem, DataRowView)
item.Text = dataRow(
"listorder").ToString & " " & _
item.Text

Catch ex As Exception
SendError(ex.Message)
End Try

End Sub

Our dataRow("listorder") should be wrapped in a function that checks for nulls. But in this example I'm simply converting it to string.


2) Populating a Datasource in our CodeBehind: 

In this example, remove the datasource in the code-in-front. We'll define it programmatically in the codebehind:

Dim oMenuAdo As New MyMenuAdo()
Dim sqldatasource1 As New SqlDataSource
sqldatasource1.SelectCommand = oMenuAdo.GetAllMenusQueryString()
sqldatasource1.ConnectionString = GetMyConnectionString()

With tvMenu
.DataSource = sqldatasource1
' if defined a datasource in the code-behind

... the rest remains the same as the first example, including the NodeDataBound...

Notice in the above, we use the name of the datasource, rather than the string name.

The GetMyConnectionString() function simply retrieves the connection string from the ConfigurationManager.AppSettings.

Our GetAllMenusQueryString() looks like this simply returns our query string:

Public Function GetAllMenusQueryString()
Dim sql As String = "SELECT * FROM [myMenu] order by listOrder"
Return sql
End Function


3) Populating with a Linq to Entities Datasource in our CodeBehind:

In our third example, we will define our datasource as an object:

Dim oMenuLinq As New MyMenuLinq()
With tvMenu
.DataSource = oMenuLinq.GetAllMenusDatasourceLinq()
' if defined a datasource as an object

... the rest of our tvMenu remains the same...

Our GetAllMenusDatasourceLinq looks like this:

Public Function GetAllMenusDatasourceLinq() As IEnumerable(Of myMenu) Dim query As IEnumerable(Of myMenu) = Nothing
Try

query = (
From m In oEntities.myMenu _
Order By m.listOrder _
Select m)

Catch ex As Exception
End Try Return query
End Function

Our NodeDataBound must be updated to cast to the object's type instead of a DataRowView:

Dim item As RadTreeNode = e.Node

' with linq datasource, cast to my object's type
Dim dataRow As myMenu = DirectCast(e.Node.DataItem, myMenu )
item.Text = dataRow.listOrder.ToString
& " " & item.Text


If I've made any mistakes, please feel free to educate me!

May your dreams be in ASP.NET!

Nannette Thacker

2 Comments

Comments have been disabled for this content.