Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 19th, 2007, 02:11 AM
Authorized User
 
Join Date: Aug 2006
Posts: 52
Thanks: 0
Thanked 0 Times in 0 Posts
Default TreeView Binding With DataBase

Hi All,
     I Need Your Help For Binding A TreeView With DatabAse(sqlexpress) Data.
I Am Working With Vb.net 2.0 And SqlExpress.

I Am Having Two Tables Such As ParentTable(Fields:-ParentId(PK),ParentName) And ChildTable(Fields:-ChildName,ParentId).
How Can I Bind The TreeView With The Table Data?

Thank You,
Sumith.
__________________
sumith
 
Old September 4th, 2009, 05:01 AM
Registered User
 
Join Date: Sep 2009
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default TreeView Binding With DataBase

Sumith,

Try this code.

ASPX

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeViewCS.aspx.cs" Inherits="TreeViewCS" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>TreeView Sample in C#</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TreeView
ID="TreeView1"
ExpandDepth="0"
PopulateNodesFromClient="true"
ShowLines="true"
ShowExpandCollapse="true"
runat="server"
OnTreeNodePopulate="TreeView1_TreeNodePopulate" />
</div>
</form>
</body>
</html>


C#


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.Odbc;

public partial class TreeViewCS : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
PopulateRootLevel();
}

private void PopulateRootLevel()
{
OdbcConnection objConn = new OdbcConnection(@"Driver={MySQL ODBC 3.51 Driver};server=localhost;user=root;password=root;o ption=3;DATABASE=ems");
OdbcCommand objCommand = new OdbcCommand(@"select id,title,imageurl,(select cast(count(*) as unsigned) FROM menu_details WHERE parentid=sc.id) as childnodecount FROM menu_details sc where parentID =0", objConn);
OdbcDataAdapter da = new OdbcDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, TreeView1.Nodes);

}

private void PopulateSubLevel(int parentid,TreeNode parentNode)
{
OdbcConnection objConn = new OdbcConnection(@"Driver={MySQL ODBC 3.51 Driver};server=localhost;user=root;password=root;o ption=3;DATABASE=ems");
OdbcCommand objCommand = new OdbcCommand(@"select id,title,imageurl, (select cast(count(*) as unsigned) FROM menu_details WHERE parentid=sc.id) as childnodecount FROM menu_details sc where parentID=@parentID", objConn);
objCommand.Parameters.Add("@parentID", OdbcType.Int).Value = parentid;
OdbcDataAdapter da = new OdbcDataAdapter(objCommand);
DataTable dt = new DataTable();
da.Fill(dt);
PopulateNodes(dt, parentNode.ChildNodes);
}


protected void TreeView1_TreeNodePopulate(object sender,TreeNodeEventArgs e)
{
PopulateSubLevel(Int32.Parse(e.Node.Value),e.Node) ;
}

private void PopulateNodes(DataTable dt,TreeNodeCollection nodes)
{
foreach( DataRow dr in dt.Rows)
{
TreeNode tn=new TreeNode();
tn.Text = dr["title"].ToString();
tn.Value = dr["id"].ToString();
nodes.Add(tn);

//If node has child nodes, then enable on-demand populating
tn.PopulateOnDemand = ((int)(dr["childnodecount"]) > 0);
}
}

}


bye
murugavel
 
Old September 16th, 2009, 06:12 AM
Authorized User
 
Join Date: Apr 2008
Posts: 54
Thanks: 0
Thanked 4 Times in 4 Posts
Default

Hi if you want to do it by using databinding as we do in gridview here is a sample
TreeView Asp.Net DataBinding from Dataset. It is using XmlDatasource to do Databinding
__________________
Pon Saravanan
VbKnowledgebase.com

Last edited by pons_saravanan; October 8th, 2009 at 02:42 AM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
Binding UserID to new record in Database sterreyl ASP.NET 2.0 Basics 3 March 1st, 2010 07:18 PM
Populating TreeView Control using Access Database dotnetDeveloper ADO.NET 0 November 18th, 2008 12:58 PM
How to binding picture file in database ? mojo VB.NET 1 September 7th, 2005 06:49 PM
treeview and database zouky ADO.NET 1 October 8th, 2004 04:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.