![]() That is, when the TreeView’s TreeNodePopulate event is raised, PopulateSubLevel is called. PopulateSubLevel( CInt(e.Node.Value), e.Node) Protected Sub TreeView1_TreeNodePopulate( ByVal sender As Object, _īyVal e As .WebControls.TreeNodeEventArgs) Handles TreeView1.TreeNodePopulate The trick to triggering the populating of the child nodes is as follows. Here, the idea is the same as with the root level, but with the distinction that only child nodes of the given node are queried and populated with the PopulateNodes method (described earlier). & "WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where _ Private Sub PopulateSubLevel( ByVal parentid As Integer, _ This happens with the PopulateSubLevel method. Next, we want to create the routine to populate the child nodes of a given node. This has the effect that the expand/collapse icon is shown if the node has child nodes. The PopulateNodes method iterates through all rows in the DataTable, and if the given node has child nodes, it sets the PopulateOnDemand property of the TreeNode object according to that. ![]() Tn.PopulateOnDemand = ( CInt(dr( "childnodecount")) > 0) 'If node has child nodes, then enable on-demand populating Private Sub PopulateNodes( ByVal dt As DataTable, _ This happens by connecting to the database, querying the first set of nodes (having null as the parent id), and creating TreeNode objects with the PopulateNodes routine, which follows next. & "WHERE parentid=sc.id) childnodecount FROM SampleCategories sc where parentID IS NULL", _ "server=JOTEKE\SQLExpress Trusted_Connection=true DATABASE=TreeViewSampleDB")ĭim objCommand As New SqlCommand( "select id,title,(select count(*) FROM SampleCategories " _ Protected Sub Page_Load( ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoadĮnd If End Sub Private Sub PopulateRootLevel() Next, we start looking at the code in the file. We also want to show lines between the nodes and the expand/collapse icon if our tree nodes have child nodes. Note that this way, no postbacks are involved. We want to use the client-side node populating feature so that we can provide our users with a better usage experience. This means that we are setting the initial expand depth of the TreeView to the first level of nodes. Then, we have TreeViewVB.aspx page with the TreeView control declaration as follows. Note: It doesn’t matter if you use SQL Server 2005 or a previous version to try this sample. Let’s assume we have TreeViewSampleDB database with the SampleCategories table as follows. It means that the TreeView implementation should be capable of displaying data from any level, no matter how deep. A requirement is that the implementation should not be dependant on the hierarchy level in the database. I’m going to provide a simple example how to display hierarchical data from SQL Server database in the TreeView. TreeView has not just been rewritten it now also includes a major set of new features such as support for client-side populating, on-demand populating, postback events, hyper link navigation, and a lot more. Microsoft has developed it based on feedback which arose from the release of the TreeView control in the open-source IE Web Controls package, targeted at the previous version of ASP.NET. One of the new controls, also my favorite, is the TreeView control. Along with the new Framework, we have a major set of new controls to use and to ease our development work. Now that ASP.NET 2.0 is about to be released to manufacturing, we ASP.NET developers have very interesting times ahead of us. Teemu Keiski demonstrates how to utilize the new TreeView control in ASP.NET 2.0 to display hierarchical data from a database. ![]() Print Add To Favorites Email To Friend Rate This Articleĭisplay Hierarchical Data with TreeView in ASP.NET 2.0 ASP.NET Tutorials » Display Hierarchical Data with TreeView in ASP.NET. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |