关闭

TreeView的ASP.NET实现

978人阅读 评论(0) 收藏 举报

Tree.aspx.cs代码如下:

using Microsoft.Web.UI.WebControls;
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace test
{
 /// <summary>
 /// Tree 的摘要说明。
 /// </summary>
 public class Tree : System.Web.UI.Page
 {
  protected Microsoft.Web.UI.WebControls.TreeView TreeView1;

  DataSet objDataSet = new DataSet();
 
  private void Page_Load(object sender, System.EventArgs e)
  {   
   if (!this.IsPostBack)
   {
    //CreateDataSet();
    intiTree(TreeView1.Nodes, "0");   
   }
  }

  private void intiTree(TreeNodeCollection Nds , int parent_id)
  {
   DataView dv = new DataView() ;
   TreeNode tmpNd;
   int strId;
   dv.Table = objDataSet.Tables["product_code_table"];
   dv.RowFilter = "parent_id='" + parent_id + "'";   
   
   foreach(DataRowView objRow in dv)
   {
    tmpNd = new TreeNode();
    strId = (int)objRow["node_id"];
    tmpNd.ID = strId.ToString();
    tmpNd.Text = objRow["node_name"].ToString();
    tmpNd.ExpandedImageUrl = objRow["img_expanded"].ToString();
    tmpNd.ImageUrl = objRow["img_original"].ToString() ;
    tmpNd.NavigateUrl = objRow["navigate_url"].ToString();
    tmpNd.CheckBox = true;
    Nds.Add(tmpNd);
    intiTree(Nds[Nds.Count-1].Nodes, strId);              
   }          
  }

  private void CreateDataSet()
  {
   OleDbConnection dbconn = new OleDbConnection(DataBaseConfiguration.ConnectionString);
   dbconn.Open();
   OleDbCommand cmd = dbconn.CreateCommand();
   cmd.CommandText = "select * from product_code_table";
   cmd.CommandType = CommandType.Text;
   OleDbDataAdapter objAdapter = new OleDbDataAdapter(cmd.CommandText, dbconn);
   objAdapter.Fill(objDataSet, "product_code_table");
   dbconn.Close(); 
  }

  #region Web 窗体设计器生成的代码
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 设计器支持所需的方法 - 不要使用代码编辑器修改
  /// 此方法的内容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}

 

treeview.aspx代码如下:

<%@ Register TagPrefix="iewc" Namespace="Microsoft.Web.UI.WebControls" Assembly="Microsoft.Web.UI.WebControls, Version=1.0.2.226, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Page language="c#" Codebehind="Tree.aspx.cs" AutoEventWireup="false" Inherits="test.Tree" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
 <HEAD>
  <title>Tree</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  <script language="JavaScript"> 
   function  initTree()
   {
    var tree = document.all["TreeView1"];
    //tree.attachEvent("onexpand", DoExpand); // 节点展开事件
    tree.attachEvent("onclick", DoClick); // 节点点击事件
    //tree.attachEvent("oncollapse", DoCollapse); // 节点收缩事件
   }
   
   function DoCollapse()
   {
   }
   
   function DoClick()
   {
    var currentNode = this.TreeView1.clickedNodeIndex;
    var product_code_table_id = this.TreeView1.getTreeNode(currentNode).getAttribute("ID");
    var url = "MainForm.aspx?product_code_table_id=" + product_code_table_id;
    parent.parent.parent.tabset.location = url;;
   }
   
   function DoExpand()
   {
    var temp = this.TreeView1.clickedNodeIndex;
    alert(this.TreeView1.getTreeNode(temp).getAttribute("NavigateUrl"));
    var nodes = new Array();
    nodes = this.TreeView1.getChildren();
    for(i=0;i<nodes.length;i++)
    {
     if(i!=temp)
     {
      if(i<10) nodeIndex = "0"+i;//我这只用到了四个节点的,十个以上的自已试试看吧。
      this.TreeView1.getTreeNode(nodeIndex).setAttribute('expanded','false');
     }
    }
   }
  </script>
 </HEAD>
 <body MS_POSITIONING="GridLayout" onload="initTree();">
  <form id="Form1" method="post" runat="server">
   <iewc:TreeView id="TreeView1" style="Z-INDEX: 101; LEFT: 40px; POSITION: absolute; TOP: 40px" runat="server"></iewc:TreeView>
  </form>
 </body>
</HTML>

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:35359次
    • 积分:442
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:31篇
    • 译文:0篇
    • 评论:1条