在WEB的TreeView中可以使用CheckBox来设置TreeView的节点是否可以选中,是大家都知道的。我们在项目进程中也用到该方法,这里把具体实现的方法给出。希望大家有更好的方案提出。
先制定节点选择的规则:
子节点选中,自动添加父节点到记录中;父节点选中,但子节点未选中的,仅仅添加父节点。
实现的代码,C#代码页面如下:
###################################
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 Microsoft.Web.UI.WebControls;
using IDAL;
using ClassI;
namespace MSTreeView
{
/// <summary>
/// WebForm1
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Button Button1;
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.DropDownList DropDownList1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
DataTable dt=new DataTable();
dt=this.GetDataSource();
Session['dt']=dt;
BoundTreeview(this.TreeView1.Nodes,0);
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private DataTable GetDataSource()
{
DataTable dt=new DataTable('dt');
dt.Columns.Add(new DataColumn('ID',typeof(System.Int32)));
dt.Columns.Add(new DataColumn('PID',typeof(System.Int32)));
dt.Columns.Add(new DataColumn('NUM',typeof(System.Int32)));
dt.Columns.Add(new DataColumn('NAME',typeof(System.String)));
// for (int i=0;i<=10;i++)
// {
DataRow dr;
dr=dt.NewRow();
dr['ID']=1;
dr['PID']=0;
dr['NUM']=1;
dr['NAME']='1';
dt.Rows.Add(dr);
dr=dt.NewRow();
dr['ID']=2;
dr['PID']=1;
dr['NUM']=2;
dr['NAME']='2';
dt.Rows.Add(dr);
dr=dt.NewRow();
dr['ID']=3;
dr['PID']=1;
dr['NUM']=5;
dr['NAME']='ID3';
dt.Rows.Add(dr);
dr=dt.NewRow();
dr['ID']=4;
dr['PID']=0;
dr['NUM']=2;
dr['NAME']='ID4';
dt.Rows.Add(dr);
dr=dt.NewRow();
dr['ID']=5;
dr['PID']=4;
dr['NUM']=1;
dr['NAME']='ID5';
dt.Rows.Add(dr);
dr=dt.NewRow();
dr['ID']=6;
dr['PID']=0;
dr['NUM']=3;
dr['NAME']='ID6';
dt.Rows.Add(dr);
dr=dt.NewRow();
dr['ID']=7;
dr['PID']=0;
dr['NUM']=1;
dr['NAME']='ID7';
dt.Rows.Add(dr);
dr=dt.NewRow();
dr['ID']=8;
dr['PID']=7;
dr['NUM']=1;
dr['NAME']='ID8';
dt.Rows.Add(dr);
dr=dt.NewRow();
dr['ID']=9;
dr['PID']=8;
dr['NUM']=1;
dr['NAME']='ID9';
dt.Rows.Add(dr);
dr=dt.NewRow();
dr['ID']=10;
dr['PID']=8;
dr['NUM']=2;
dr['NAME']='ID10';
dt.Rows.Add(dr);
dr=dt.NewRow();
dr['ID']=11;
dr['PID']=7;
dr['NUM']=2;
dr['NAME']='ID11';
dt.Rows.Add(dr);
// }
return dt;
}
///此处使用递归绑定TreeView的数据
private void BoundTreeview(TreeNodeCollection TreeNodes ,int ParentID)
{
DataTable dt=new DataTable();
dt=(DataTable)Session['dt'];
DataView treeDV = new DataView();
int treeId;
TreeNode treeTemp;
string treeName;
treeDV.Table = dt;
treeDV.RowFilter = 'ID<>'' + ParentID + '' and PID='' + ParentID + ''';
treeDV.Sort='NUM';
foreach(DataRowView treeDVrow in treeDV)
{
treeTemp=new TreeNode();
treeName = treeDVrow['NAME'].ToString();
treeId = Convert.ToInt32(treeDVrow['ID'].ToString());
treeTemp.ID = treeId.ToString();
treeTemp.Text = treeName;
treeTemp.CheckBox=true;
treeTemp.Expanded=true;
TreeNodes.Add(treeTemp);
BoundTreeview(TreeNodes[TreeNodes.Count - 1].Nodes, treeId);
}
}
///递归TreeView,把所有被选中的节点ID记录到HashTable中
private void RecursiveTree(TreeView tree,ref Hashtable allID )
{
TreeNodeCollection nodes = tree.Nodes;
foreach (TreeNode n in nodes)
{
RecursiveNodes(n,ref allID);
AddCheckedID(n,ref allID);
}
}
///递归TreeView的节点,把所有被选中的节点ID记录到HashTable中
private void RecursiveNodes(TreeNode node,ref Hashtable allID)
{
foreach (TreeNode tn in node.Nodes)
{
RecursiveNodes(tn,ref allID);
AddCheckedID(tn,ref allID);
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
Hashtable aaa=new Hashtable();
RecursiveTree(this.TreeView1,ref aaa);
}
///把被选中的节点的父节点ID记录到HashTable中
private void AddParentID(TreeNode node,ref Hashtable allID)
{
try
{
TreeNode pnode=(TreeNode)node.Parent;
AddParentID(pnode,ref allID);
AddIdToHashTable(Convert.ToInt32(pnode.ID),ref allID);
}
catch
{
}
}
///把所有被选中的节点ID和其父节点ID记录到HashTable中
private void AddCheckedID(TreeNode node,ref Hashtable allID )
{
if(node.Checked)
{
string strvalue=node.ID.ToString();
int i=Convert.ToInt32(node.ID);
try
{
AddParentID(node,ref allID);
}
catch
{
}
AddIdToHashTable(i,ref allID);
}
}
///把所有被选中的节点ID记录到HashTable中,如果ID已经存在,则不添加
private void AddIdToHashTable(int nodeID,ref Hashtable allID)
{
if(!allID.ContainsKey(nodeID))
{
allID.Add(nodeID,nodeID.ToString());
}
}
private void Button2_Click(object sender, System.EventArgs e)
{
Hashtable aaa=new Hashtable();
if(Session['aaa']!=null) aaa=(Hashtable)Session['aaa'];
Session['aaa']=aaa;
}
}
}
###################################
个人感觉这种实现的方式不是最好的,但是项目进度决定了没有太多时间来考量算法和其他实现放松。希望其他网友有更好的方案,大家共享!