Dynamically load data from a database with the TreeView control

Source: Internet
Author: User
treeview| Dynamic | control | data | database

ASPX *************************//

<%@ Page language= "C #" codebehind= "WebForm1.aspx.cs" autoeventwireup= "false" inherits= "Treetest.webform1"%>
<%@ Register tagprefix= "IEWC" namespace= "Microsoft.Web.UI.WebControls" assembly= "Microsoft.Web.UI.WebControls"% >

<HTML>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<font face= "Song Body" >
<iewc:treeview id= "TreeView1" style= "Z-INDEX:101; left:8px; Position:absolute; TOP:16PX "runat=" Server "
autopostback= "True" ></iewc:TreeView>
<asp:label id= "Label1" style= "z-index:102; left:440px; Position:absolute; TOP:56PX "runat=" Server "
Width= "152px" height= "64px" >Label</asp:Label>
<asp:label id= "Label2" style= "z-index:103; left:456px; Position:absolute; TOP:144PX "runat=" Server "
Width= "144px" height= "48px" >Label</asp:Label>
<asp:label id= "Label3" style= "z-index:104; left:696px; Position:absolute; TOP:64PX "runat=" Server >Label</asp:Label></FONT>
</form>
</body>
</HTML>

CS *************************//

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.Data.OleDb;
Using Microsoft.Web.UI.WebControls;
Namespace Treetest
{
<summary>
Summary description of the WebForm1.
</summary>
public class WebForm1:System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label3;

OleDbConnection Con;
String dataconstr = "";

  private void Page_Load (object sender, System.EventArgs e)
  {
    Dataconstr = "Provider=Microsoft.Jet.OLEDB.4.0;Data source=" +server.mappath ("Tree.mdb") + "; User id=; password=; ";
   
   con = new OleDbConnection (DATACONSTR);
   if (! IsPostBack)
   {
                    
    treenode node = new TreeNode ();
    node. Text = "root";
    node. Nodedata = "0";
    treeview1.nodes.add (node);//Add root
    addtestnodes (node);  //Add "Expand ..." node
   
  }

private void Addtestnodes (TreeNode node)
{
TreeNode Testnode = new TreeNode ();
Testnode. Text = "is expanding ...";
Testnode. Nodedata = "Testnode";
Node. Nodes.Add (Testnode);
}
private void AddNodes (TreeNode node)
{
String parentid = "0";
if (node. Parent!= null)
{
ParentID = node. Nodedata;
}

Deeptree for Table name
String cmdstr = "SELECT * from Deeptree where parentid=" +parentid;

OleDbCommand cmd = new OleDbCommand (Cmdstr,con);
Try
{
Con.open ();
OleDbDataReader reader = cmd. ExecuteReader ();

while (reader. Read ())
{
TreeNode Childnode = new TreeNode ();
Childnode.text = reader["Content"]. ToString ();
Childnode.nodedata = reader["id"]. ToString ();

Determine if there are child nodes ************//
Deeptree for Table name

CMDSTR = "SELECT count (*) from Deeptree where parentid=" +childnode.nodedata;
OleDbConnection Con1 = new OleDbConnection (DATACONSTR);
OleDbCommand cmd1 = new OleDbCommand (cmdstr,con1);
Con1.open ();
OleDbDataReader reader1 = cmd1. Executereade



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.