Welcome Guest Search | Active Topics | Sign In | Register

Problem accessing treenode parentnode in tree that is populated from a datatable Options
Michael Earley
Posted: Tuesday, October 16, 2007 4:47:38 AM
Rank: Newbie
Groups: Member

Joined: 6/16/2007
Posts: 8
I am trying to access properties of a node and a parent node when the ItemCheckStateChanged is fired. The tree appears to be populated correctly by the datatable and I can access the properties of the checked node but if I try to access the parentnode the following error is reported:


[NullReferenceException: Object reference not set to an instance of an object.]
EnterpriseManagement_ContactTree.TreeView1_ItemCheckStateChanged(Object sender, TreeNodeCheckStateChangedEventArgs e) in E:\Websites\PQP.Net\EnterpriseManagement\ContactTree.aspx.vb:70
EO.Web.TreeView.RaisePostDataChangedEvent(String name, EventArgs e) +108
EO.Web.WebControlBase.p() +170
EO.Web.BaseNavigator.h() +5
System.Web.UI.Page.RaiseChangedEvents() +137
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4778


If I manually add nodes to the same tree and do not populate it with the datatable, the ItemCheckStateChanged will allow me to access the selected node and any parent nodes.

Any help would be appreciated.
Thanks,
Michael

Code:

Imports STW.EnterpriseManagement
Imports System.Data
Imports System.Collections.ObjectModel

Partial Class EnterpriseManagement_ContactTree
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim ContactDB As New ContactDB
Dim itmCollection As New DataTable

itmCollection = ContactDB.selectDataTableContacts()

TreeView1.DataSource = itmCollection
TreeView1.DataFields = "DisciplineType|Discipline|Organisation|Contact"

Dim binding1 As New EO.Web.DataBinding()
binding1.Depth = 3
binding1.DataField = "ContactID"
binding1.Property = "ItemID"

Dim binding2 As New EO.Web.DataBinding()
binding2.Depth = 2
binding2.DataField = "Status"
binding2.Property = "Tooltip"

Dim binding3 As New EO.Web.DataBinding()
binding3.Depth = 3
binding3.DataField = "ContactLink"
binding3.Property = "NavigateUrl"

Dim binding4 As New EO.Web.DataBinding()
binding4.Depth = 2
binding4.DataField = "OrganisationLink"
binding4.Property = "NavigateUrl"

Dim binding5 As New EO.Web.DataBinding()
binding5.Depth = 2
binding5.DataField = "OrganisationImage"
binding5.Property = "ImageUrl"

Dim binding6 As New EO.Web.DataBinding()
binding6.Depth = 2
binding6.DataField = "AppointmentID"
binding6.Property = "ItemID"


TreeView1.Bindings.Add(binding1)
TreeView1.Bindings.Add(binding2)
TreeView1.Bindings.Add(binding3)
TreeView1.Bindings.Add(binding4)
TreeView1.Bindings.Add(binding5)
TreeView1.Bindings.Add(binding6)

TreeView1.DataBind()

TreeView1.SaveStateCrossPages = True
TreeView1.ExpandAll()

End Sub

Protected Sub TreeView1_ItemCheckStateChanged(ByVal sender As Object, ByVal e As EO.Web.TreeNodeCheckStateChangedEventArgs) Handles TreeView1.ItemCheckStateChanged
Label2.Text = e.Node.Text & " and " & e.Node.ParentNode.Text

'Dim AppointmentContact As New AppointmentContact
'Dim AppointmentContactDB As New AppointmentContactDB

'If e.Node.IsLeafNode Then 'Contact
'If e.Node.Checked = True Then

'AppointmentContact = AppointmentContactDB.insertAppointmentContact(CInt(e.Node.ParentNode.ItemID), CInt(e.Node.ItemID))
'If Not e.Node.ParentNode Is Nothing Then
'Label2.Text = e.Node.ParentNode.Text
'Else
'Label2.Text = e.Node.Path
'End If

'Else
'remove
'End If
'End If
End Sub
eo_support
Posted: Tuesday, October 16, 2007 5:08:42 AM
Rank: Administration
Groups: Administration

Joined: 5/27/2007
Posts: 24,217
Hi Michael,

That makes sense :). What happened was:

1. The page posts back;
2. The TreeView reconstructs all the TreeNode from view state, and locate the node whose check state has changed and keeps a reference to it;
3. Page_Load is called, during which you repopulate all the nodes. At this point, all the old nodes were taken off from the TreeView. It is at this point the node step 2 keeps become an "orphan" node since it no longer belongs to the TreeView;
4. ItemCheckStateChanged is triggered. Since the node whose check state has changed is an orhan node, it has no parent node;

The easiest way to avoid this error is to add an if condition in your Page_Load:

Code: Visual Basic.NET
If Not Page.IsPostBack Then
    ....do your data binding stuff....
End If


Thanks
Michael Earley
Posted: Tuesday, October 16, 2007 5:47:45 AM
Rank: Newbie
Groups: Member

Joined: 6/16/2007
Posts: 8
Angel
Problem sorted. Will get the hang of posbacks yet.
Thanks for your help,
Michael


You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.