照着例子写了一个,发现不会动态添加。看看微软说明
http://msdn2.microsoft.com/zh-cn/library/system.web.ui.webcontrols.treenode.populateondemand(VS.80).aspx
需要TreeNode.PopulateOnDemand=true
但是第一个Node需要手工添加,然后设置PopulateOnDemand=true
protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
{
if (e.Node.ChildNodes.Count == 0)
{
if (e.Node.Depth == 0)
{
FillBom(e.Node);
}
else
{
FillBomDetail(e.Node);
}
}
}
void FillBom(TreeNode node)
{
string ID = node.Value;
BOMTableAdapters.BomModelTableAdapter oAdapter = new BOMTableAdapters.BomModelTableAdapter();
BOM.BomModelDataTable oBomTable=oAdapter.GetData();
if (oBomTable.Rows.Count > 0)
{
foreach (DataRow row in oBomTable.Rows)
{
TreeNode newNode = new TreeNode(row["model"].ToString(), row["model"].ToString());
newNode.PopulateOnDemand = true;
newNode.SelectAction = TreeNodeSelectAction.None;
node.ChildNodes.Add(newNode);
}
}
}
void FillBomDetail(TreeNode node)
{
string ID = node.Value;
BOMTableAdapters.BomModelItemTableAdapter oAdapter = new BOMTableAdapters.BomModelItemTableAdapter();
BOM.BomModelItemDataTable oBomTable = oAdapter.GetDataByModel(ID);
if (oBomTable.Rows.Count > 0)
{
foreach (DataRow row in oBomTable.Rows)
{
TreeNode newNode = new TreeNode(row["code"].ToString(), row["code"].ToString());
newNode.PopulateOnDemand = true;
newNode.SelectAction = TreeNodeSelectAction.None;
node.ChildNodes.Add(newNode);
}
}