Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old October 19th, 2007, 02:11 AM
Authorized User
Points: 270, Level: 5
Points: 270, Level: 5 Points: 270, Level: 5 Points: 270, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2006
Location: thrissur, kerala, India.
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
Reply With Quote
  #2 (permalink)  
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
Reply With Quote
  #3 (permalink)  
Old September 16th, 2009, 06:12 AM
Authorized User
Points: 198, Level: 3
Points: 198, Level: 3 Points: 198, Level: 3 Points: 198, Level: 3
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2008
Location: singapore, singapore, Singapore.
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..
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


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



All times are GMT -4. The time now is 04:21 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.