利用TreeView控制項 從資料庫中動態載入資料

來源:互聯網
上載者:User
treeview|動態|控制項|資料|資料庫  

//*************************  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="宋體">
    <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>
 /// 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 = "根";
    node.NodeData = "0";
    TreeView1.Nodes.Add(node); // 添加根
    AddTestNodes(node); //添加"展開..."節點
   }
  }

  private void AddTestNodes(TreeNode node)
  {
   TreeNode testnode = new TreeNode();
   testnode.Text = "正在展開....";
   testnode.NodeData = "testnode";
   node.Nodes.Add(testnode);
  }
  private void AddNodes(TreeNode node)
  {
   string parentID = "0";
   if(node.Parent != null)
   {
    parentID = node.NodeData;
   }

  // deeptree 為表名
   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();

     //***************** 判斷是否有子節點 ************ //
// deeptree 為表名

     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



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。