Custom TreeNode for asp.net 2.0 treeview control

Discussion in 'ASP .Net Web Controls' started by Rickey Whitworth, Jan 18, 2006.

  1. I want to display a treeview control that is tied to a sql database.

    The child nodes will be of different types, meaning they will have different
    attributes and different tables

    -Root
    --Service1 (select * from services) value = serviceID = 1
    ---Database1 (select * from databases where serviceID = ?)
    ---Website1 (select * from websites where serviceID = ?)

    When I click on a node, I want to display attributes of that node in the
    right pane. I have handled this before in a windows application by creating
    a custom treeNode that has an extra field for type (service, database or
    website).

    Here is my custom tree node and my enumeration for type

    Public Enum NodeType
    Service
    Application
    Database
    Website
    End Enum

    Public Class CustomTreeNode
    Inherits TreeNode

    Private _ntype As NodeType

    Public Property NType() As NodeType
    Get
    Return _ntype
    End Get
    Set(ByVal value As NodeType)
    _ntype = value
    End Set
    End Property

    Public Sub New(ByVal text As String, ByVal value As String, ByVal
    nType As NodeType)
    MyBase.New(text, value)
    _ntype = nType
    End Sub

    Public Sub New()
    MyBase.New()
    End Sub
    End Class

    And I get an error in this function

    Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e As
    System.Web.UI.WebControls.TreeNodeEventArgs) Handles
    TreeView1.TreeNodePopulate
    If e.Node.ChildNodes.Count = 0 Then
    Select Case e.Node.Depth
    Case 0
    FillAuthors(e.Node)
    Case 1
    FillTitlesForAuthors(e.Node)
    Case 2
    FillDatabase(e.Node)
    End Select
    End If
    End Sub


    Unable to cast object of type 'System.Web.UI.WebControls.TreeNode' to type
    'CustomTreeNode'

    I'm not really sure how to make this work. I can give more code samples as
    needed, but I just need someone who can point me in the right direction. Is
    there some other function or property of the treenode I need to override, do
    I need to create a custom treeview control and override some event in it?
    Rickey Whitworth, Jan 18, 2006
    #1
    1. Advertising

  2. Hi Rickey,

    Welcome.
    As for the ASP.NET 2.0 TreeView control, we can create custom control to
    derived from it , also define our custom TreeNode class.... Also,
    currently I'm also discussing such similiar question with other guy in the
    newsgroup, here is the web link to the thread:

    #Inheriting from the TreeNode class
    http://groups.google.com/group/microsoft.public.dotnet.framework.aspnet.webc
    ontrols/browse_thread/thread/b0f3459b35105353/df2f38d4c2b2dc20?lnk=st&q=Inhe
    riting+from+the+TreeNode+class&rnum=1&hl=en#df2f38d4c2b2dc20

    Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)

    --------------------
    | From: "Rickey Whitworth" <>
    | Subject: Custom TreeNode for asp.net 2.0 treeview control
    | Date: Wed, 18 Jan 2006 10:48:31 -0600
    | Lines: 76
    | X-Priority: 3
    | X-MSMail-Priority: Normal
    | X-Newsreader: Microsoft Outlook Express 6.00.2900.2527
    | X-RFC2646: Format=Flowed; Original
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.2527
    | Message-ID: <#>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
    | NNTP-Posting-Host: 66.182.150.119
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet.webcontrols:32567
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
    |
    | I want to display a treeview control that is tied to a sql database.
    |
    | The child nodes will be of different types, meaning they will have
    different
    | attributes and different tables
    |
    | -Root
    | --Service1 (select * from services) value = serviceID = 1
    | ---Database1 (select * from databases where serviceID = ?)
    | ---Website1 (select * from websites where serviceID = ?)
    |
    | When I click on a node, I want to display attributes of that node in the
    | right pane. I have handled this before in a windows application by
    creating
    | a custom treeNode that has an extra field for type (service, database or
    | website).
    |
    | Here is my custom tree node and my enumeration for type
    |
    | Public Enum NodeType
    | Service
    | Application
    | Database
    | Website
    | End Enum
    |
    | Public Class CustomTreeNode
    | Inherits TreeNode
    |
    | Private _ntype As NodeType
    |
    | Public Property NType() As NodeType
    | Get
    | Return _ntype
    | End Get
    | Set(ByVal value As NodeType)
    | _ntype = value
    | End Set
    | End Property
    |
    | Public Sub New(ByVal text As String, ByVal value As String, ByVal
    | nType As NodeType)
    | MyBase.New(text, value)
    | _ntype = nType
    | End Sub
    |
    | Public Sub New()
    | MyBase.New()
    | End Sub
    | End Class
    |
    | And I get an error in this function
    |
    | Protected Sub TreeView1_TreeNodePopulate(ByVal sender As Object, ByVal e
    As
    | System.Web.UI.WebControls.TreeNodeEventArgs) Handles
    | TreeView1.TreeNodePopulate
    | If e.Node.ChildNodes.Count = 0 Then
    | Select Case e.Node.Depth
    | Case 0
    | FillAuthors(e.Node)
    | Case 1
    | FillTitlesForAuthors(e.Node)
    | Case 2
    | FillDatabase(e.Node)
    | End Select
    | End If
    | End Sub
    |
    |
    | Unable to cast object of type 'System.Web.UI.WebControls.TreeNode' to
    type
    | 'CustomTreeNode'
    |
    | I'm not really sure how to make this work. I can give more code samples
    as
    | needed, but I just need someone who can point me in the right direction.
    Is
    | there some other function or property of the treenode I need to override,
    do
    | I need to create a custom treeview control and override some event in it?
    |
    |
    |
    Steven Cheng[MSFT], Jan 19, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    411
  2. tanya foster
    Replies:
    3
    Views:
    3,712
    tanya foster
    May 18, 2006
  3. mazdotnet
    Replies:
    0
    Views:
    764
    mazdotnet
    Aug 1, 2006
  4. abargaddon
    Replies:
    0
    Views:
    1,050
    abargaddon
    Jan 17, 2008
  5. Jerry C

    Add a control to a treenode. TreeView

    Jerry C, Jul 14, 2006, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    323
    Walter Wang [MSFT]
    Jul 17, 2006
Loading...

Share This Page