當前位置:開發者網絡 >> 技術教程 >> ASP教程 >> 數據庫相關 >> 內容
精彩推薦
分類最新教程
分類熱點教程
    
利用TreeView控件 從數據庫中動態載入數據
作者:未知
日期:2005-04-22
人氣:
投稿:(轉貼)
來源:未知
字體:
收藏:加入瀏覽器收藏
以下正文:
 

//*************************  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.ExecuteReader();
     if(reader1.Read())
     {    
      if(System.Convert.ToInt32(reader1[0]) > 0)
      {
       AddTestNodes(childNode);  // 若有子節點,則添加測試節點     
      }
     }
     reader1.Close();
     Con1.Close();

     //************************************************* //

     node.Nodes.Add(childNode);
               
    }
   }
   catch(Exception ex)
   {
    Label3.Text = ex.Message;
   }
   finally
   {
    if(Con.State != ConnectionState.Closed)
     Con.Close(); 
   }

  }

  #region Web 窗體設計器生成的代碼
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.TreeView1.Expand += new Microsoft.Web.UI.WebControls.ClickEventHandler(this.TreeView1_Expand);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void TreeView1_Expand(object sender, Microsoft.Web.UI.WebControls.TreeViewClickEventArgs e)
  {
   Label1.Text = e.Node;
   TreeNode node = new TreeNode();
   node = TreeView1.GetNodeFromIndex(e.Node);
   //node = TreeView1.GetNodeFromIndex("0.2");
   if(node != null)
   {
    Label2.Text = node.NodeData;

    // 判斷該節點下子節點數,若1個且為testnode說明未填充所以子節點
    if((node.Nodes.Count == 1)&& (node.Nodes[0].NodeData == "testnode"))
    {
     node.Nodes.Clear(); //清空
     AddNodes(node);  // 填充子節點
    }
   }
  }
 }
}

數據庫中設置字段為 id, content,parentid

PS: 缺點,因為AutoPostBack為TRUE,提交到服務器時每次都會閃爍. 而且載入數據越多越慢..
沒有 用ASP 來的快.

相關文章: