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 PMS.Common;
namespace PMS.Manage
{
/// <summary>
/// SEPTypeManage 的摘要说明。
/// </summary>
public class SEPTypeManage : System.Web.UI.Page
{
protected System.Web.UI.WebControls.ValidationSummary ValidationSummary1;
protected System.Web.UI.WebControls.ImageButton ibtn_Del;
protected System.Web.UI.WebControls.ImageButton ibtn_Modify;
protected System.Web.UI.WebControls.ImageButton ibtn_Add;
protected System.Web.UI.WebControls.TextBox tbox_CurNote;
protected System.Web.UI.WebControls.Label lbl_CurNote;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.Label lbl_Msg;
protected System.Web.UI.HtmlControls.HtmlForm Form1;
protected System.Web.UI.WebControls.TextBox tbox_CurType;
protected System.Web.UI.WebControls.Label lbl_CurType;
protected System.Web.UI.WebControls.Label lbl_SupTypeName;
protected System.Web.UI.WebControls.Label lbl_SupType;
protected Microsoft.Web.UI.WebControls.TreeView TreeView_EP; protected OperateDataBase odb;
protected DataTable dt;
protected DataView dv;
// protected TreeNode tn;

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if (!IsPostBack)
{
InitData();
}
} private void InitData()
{
TreeNode tn = new TreeNode();
tn.Text = "企业分类管理";
tn.ID = "0";
TreeView_EP.Nodes.Clear();
TreeView_EP.Nodes.Add(tn); string sqlSelect = "SELECT CustomerTypeID, CustomerTypeName, SuperID FROM C_CustomerType ";
odb = new OperateDataBase();
try
{
dt = odb.getDataTable(sqlSelect);
}
catch{}
dv = new DataView();
dv.Table = dt;
InitTypeData(tn, 0);
TreeView_EP.GetNodeFromIndex("0").Expanded =true; this.lbl_SupType.Visible = false;
this.lbl_SupTypeName.Visible = false;
this.ibtn_Modify.Visible = false;
this.ibtn_Del.Visible = false;
this.lbl_CurType.Text = "当前功能:";
this.tbox_CurType.Text = "企业分类管理";
this.tbox_CurNote.Text = "本模块主要用于:\n    管理本公司客户企业分类,支持无限制多级分类管理。可进行添加、修改、删除操作!";
this.tbox_CurType.ReadOnly = true;
this.tbox_CurNote.ReadOnly = true;
} private void InitTypeData(TreeNode Supertn, int SuperID)  //此段代码为何不能递归调用显示节点树
{
dv.RowFilter = "SuperID=" + SuperID; if(dv.Count == 0)
{
return;
}
foreach(DataRowView drv in dv)
{
TreeNode tn = new TreeNode();
tn.ID = drv["CustomerTypeID"].ToString();
tn.Text = drv["CustomerTypeName"].ToString();
int SID = int.Parse(drv["SuperID"].ToString());
Supertn.Nodes.Add(tn);
// InitTypeData(tn, SID); //此处有何错误
}
} #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
}
}

解决方案 »

  1.   

    InitTypeData(tn, SID); 
    SID,也就是他的父结点ID,我看不出那里有错
      

  2.   

    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 System.Data .SqlClient ;
    using Microsoft.Web .UI.WebControls ;namespace ReportTest
    {
    /// <summary>
    /// WebForm3 的摘要说明。
    /// </summary>
    public class WebForm3 : System.Web.UI.Page
    {
    protected System.Data.SqlClient.SqlConnection sqlConnection1;
    protected Microsoft.Web.UI.WebControls.TreeView TreeView1;private void Page_Load(object sender, System.EventArgs e)
    {
    if (!this.IsPostBack )
    {
    this.sqlConnection1 .Open ();
    // this.TreeView1 .ExpandLevel=2;this.ShowAllTree (0,this.sqlConnection1,this.TreeView1.Nodes) ;}
    }private TreeNodeCollection ShowAllTree(int ddd ,SqlConnection SqlConnection1,Microsoft.Web.UI.WebControls.TreeNodeCollection nodes)
    {stringsqlSelect =
    "SELECT parent_id , this_id,this_name FROM tree where parent_id='"+ddd+"'" ;SqlDataAdapter da = new SqlDataAdapter(sqlSelect, this.sqlConnection1);
    DataTable dt=new DataTable();
    da.Fill(dt);for(int i=0;i<dt.Rows.Count;i++)
    {
    TreeNode newNode=new TreeNode();
    DataRow dr=dt.Rows[i];
    newNode.Text= dr["this_name"].ToString();
    nodes.Add(newNode);int newID = Convert.ToInt32 (dt.Rows[i][1]);;ShowAllTree(newID,this.sqlConnection1 ,newNode.Nodes); //递归调用,将子结点ID当做下一当前结点ID}dt.Dispose();
    return nodes;
    }
    #region Web 窗体设计器生成的代码
    override protected void OnInit(EventArgs e)
    {
    //
    // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
    //
    InitializeComponent();
    base.OnInit(e);
    }/// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
    this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
    //
    // sqlConnection1
    //
    this.sqlConnection1.ConnectionString = "workstation id=GW;packet size=4096;user id=sa;data source=GW;persist security inf" +
    "o=True;initial catalog=pubs;password=";
    this.Load += new System.EventHandler(this.Page_Load);}
    #endregion
    }
    }//数据结构
    create table tree(
    parent_id int not null,
    this_id int not null,
    this_name varchar(20) not null
    )
    insert into tree values(0,1,'第一个结点');
    insert into tree values(0,2,'第二个结点');
    insert into tree values(0,3,'第三个结点');
    insert into tree values(1,11,'结点1.1');
    insert into tree values(1,12,'结点1.2');
    insert into tree values(12,121,'结点121');
    insert into tree values(121,1211,'结点1211');
      

  3.   

    //创建树的例子有很多
    递归函数本身有问题,可以先学习查看一下递归函数的使用,然后再用在Tree上
      

  4.   

    TreeNode tn = new TreeNode();
    tn.ID = drv["CustomerTypeID"].ToString();
    tn.Text = drv["CustomerTypeName"].ToString();
    int SID = int.Parse(drv["SuperID"].ToString());
    Supertn.Nodes.Add(tn);
    // InitTypeData(tn, SID); SID是drv的父结点,不就是SuperID吗?这样就死循环了。应该:SID=tn.ID;就可以了。