< DOCTYPE html PUBLIC -WCDTD XHTML StrictEN httpwwwworgTRxhtmlDTDxhtml-strictdtd>
c#树获得方法,虽然TreeView效果很差,但是还是要记录下来 ,以备日后项目中应用
以下是项目中应用到树的部份代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
IList<NewTypeInfo> list = new List<NewTypeInfo>();
NewType oNewType = new NewType();
list = oNewType.GetClass(0);
InitItemTree(trvInfoClass.Nodes, 0, list);
}
}
private void InitItemTree(TreeNodeCollection tncCurrent, int intParentId, IList<NewTypeInfo> list)
{
foreach (NewTypeInfo oNewTypeInfo in list)
{
if (oNewTypeInfo.intParentId == intParentId)
{
TreeNode nodTemp;
nodTemp = new TreeNode();
nodTemp.Value = oNewTypeInfo.id + "";
nodTemp.Text = oNewTypeInfo.strTypeName;
tncCurrent.Add(nodTemp);
InitItemTree(nodTemp.ChildNodes, oNewTypeInfo.id, list);
}
}
}
以下是项目中应用到树的部份代码:
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
IList<NewTypeInfo> list = new List<NewTypeInfo>();
NewType oNewType = new NewType();
list = oNewType.GetClass(0);
InitItemTree(trvInfoClass.Nodes, 0, list);
}
}
private void InitItemTree(TreeNodeCollection tncCurrent, int intParentId, IList<NewTypeInfo> list)
{
foreach (NewTypeInfo oNewTypeInfo in list)
{
if (oNewTypeInfo.intParentId == intParentId)
{
TreeNode nodTemp;
nodTemp = new TreeNode();
nodTemp.Value = oNewTypeInfo.id + "";
nodTemp.Text = oNewTypeInfo.strTypeName;
tncCurrent.Add(nodTemp);
InitItemTree(nodTemp.ChildNodes, oNewTypeInfo.id, list);
}
}
}