Casting error with extended treenode in treeview




I just made an extended treenode that I want to add to my treenode.

On for example a 'SelectedNodeChanged' event, my selected treenode is
not my extended treenode anymore but a regular treenode.

Running the sample code below results in the following error:
"Unable to cast object of type 'System.Web.UI.WebControls.TreeNode' to
type 'MyNode'".

Seems quite basic to me, what do I do wrong?

Many thanks,

Partial Class _Default
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim objTreeNode As New MyNode
objTreeNode.Value = "value"
objTreeNode.Text = "text"
objTreeNode.OtherValue = "test"
End If
End Sub

Protected Sub TreeView1_SelectedNodeChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles TreeView1.SelectedNodeChanged
Dim objTreeNode2 As MyNode
objTreeNode2 = TreeView1.Nodes(0)
lblOtherValue.Text = objTreeNode2.OtherValue
End Sub
End Class

Public Class MyNode
Inherits TreeNode

Public OtherValue As String

End Class


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question