动态生成treeview
一次性的把数据库里的数据构造成treeview,但是如果数据量特别大那种方法就不行了,下面是动态构造treeview的办法,就是先把根(第一级)构造出来,然后每一个根节点下面增加一个虚拟的假子结点(为了使+号显示),当点击跟时构造出他下面的真正的子结点,依此类推。
代码如下:
#region 初始化物资树
//--------------------------------------------------------------------------------------------------------
/// <summary>
/// 初始化根treenode(rootnode)
/// </summary>
/// <param name="toTreeView"></param>
public static void InvTree_InitEx1(TreeView toTreeView)
{
toTreeView.Nodes.Clear();
int iLevel = 1;// tmpCorp.iTreeLevel;
ORtInv corp = new ORtInv();
ORDataReader<ORtInv> reader = corp.DataAccessor.ExecuteReader(CommandType.Text, "select * from tInv where iTreeLevel=1 and sStatus = '1'", corp, null);
![](/Images/OutliningIndicators/InBlock.gif)
TreeNode tmpNode = new TreeNode();
foreach (ORtInv u in reader)//循环每行数据
{
TreeNode rootNode = new TreeNode();
rootNode.Text = u.sInv_no.ToString().Trim() + u.sInv_name.ToString().Trim();
rootNode.Value = u.pk_ID.ToString().Trim();
rootNode.ToolTip = u.sInv_name;
toTreeView.Nodes.Add(rootNode);
rootNode.Expanded = false;
}
reader.Close();
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < toTreeView.Nodes.Count; i++)
{
TreeNode nothingNode = new TreeNode();
nothingNode.Text = "Node";
nothingNode.Value = "0";
toTreeView.Nodes[i].ChildNodes.Add(nothingNode);
nothingNode.Expanded = true;
//toTreeView.Nodes[i].ChildNodes.
}
![](/Images/OutliningIndicators/InBlock.gif)
}
/// <summary>
/// 为某个treenode增加下面的childnode
/// </summary>
/// <param name="toTreeView"></param>
public static void InvTree_InitEx2(TreeView toTreeView, TreeNode dadNode)
{
dadNode.ChildNodes.Clear();//先清空子结点
ORtInv tmpInv = new ORtInv();
tmpInv.pk_ID = Convert.ToInt64(dadNode.Value);
tmpInv.DataAccessor.Select("pk_ID=@ipk_ID");
![](/Images/OutliningIndicators/InBlock.gif)
string sDadValue = tmpInv.sInv_no;
![](/Images/OutliningIndicators/InBlock.gif)
ORtInv corp = new ORtInv();
ORDataReader<ORtInv> reader = corp.DataAccessor.ExecuteReader(CommandType.Text, "select * from tInv where sStatus = '1' and sParent_no=" + Common.QuotedStr(sDadValue), corp, null);
![](/Images/OutliningIndicators/InBlock.gif)
TreeNode tmpNode = new TreeNode();
foreach (ORtInv u in reader)//循环每行数据
{
tmpNode = dadNode;
if (tmpNode != null)
{
TreeNode subNode = new TreeNode();
subNode.Text = u.sInv_no.ToString().Trim() + u.sInv_name.ToString().Trim();
subNode.Value = u.pk_ID.ToString().Trim();
subNode.ToolTip = u.sInv_name;
tmpNode.ChildNodes.Add(subNode);
subNode.Expanded = false;
}
}
reader.Close();
![](/Images/OutliningIndicators/InBlock.gif)
for (int i = 0; i < dadNode.ChildNodes.Count; i++)
{
TreeNode nothingNode = new TreeNode();
nothingNode.Text = "Node";
nothingNode.Value = "0";
dadNode.ChildNodes[i].ChildNodes.Add(nothingNode);
nothingNode.Expanded = false;
//toTreeView.Nodes[i].ChildNodes.
}
}
/// <summary>
/// 递归查找父节点
/// </summary>
/// <param name="tnParent">指定一个根节点,然后遍历它</param>
/// <param name="strValue">所要查找的节点的value</param>
public static TreeNode FindNode(TreeNode tnParent, string strValue)
{
if (tnParent == null) return null;
if (tnParent.Value == strValue) return tnParent;
TreeNode tnRet = null;
foreach (TreeNode tn in tnParent.ChildNodes)
{
tnRet = FindNode(tn, strValue);
if (tnRet != null) break;
}
return tnRet;
}
![](/Images/OutliningIndicators/InBlock.gif)
#endregion
![](/Images/OutliningIndicators/None.gif)
protected void tvLeft_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
{
//lbStatus.Text = "Expanded:"+e.Node.Text;
InvTree_InitEx2(tvLeft, e.Node);
//e.Node.Selected = true;
}
开始时以为点击treeview的+号只能触发客户端事件,后来加进去TreeNodeExpanded以后发现是可以触发服务器代码的:)
代码如下:
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/None.gif)
![](/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/InBlock.gif)
![](/Images/OutliningIndicators/ExpandedBlockEnd.gif)
开始时以为点击treeview的+号只能触发客户端事件,后来加进去TreeNodeExpanded以后发现是可以触发服务器代码的:)