[此贴子已经被bygg于2006-12-5 17:00:33编辑过]
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class shu2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
TreeNode tn = null;
TreeViewaa(this.TreeView1,tn,"1");
}
}
protected void TreeViewaa(TreeView tv, TreeNode Btn, string privED)
{
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.AppSettings["connStrings"]);
string sql = "select * from tbtree where parentID=" + privED;
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(sql, conn);
da.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
for (int i=0; i < dt.Rows.Count; i++)
{
TreeNode tn1 = new TreeNode(dt.Rows[i]["name"].ToString(),dt.Rows[i]["parentID"].ToString());
if (Btn == null)
{
tv.Nodes.Add(tn1);
}
else
{
Btn.ChildNodes.Add(tn1);
}
this.TreeView1.Nodes[i].ToolTip = "";
TreeViewaa(tv, tn1, dt.Rows[i]["id"].ToString());
}
}
}
第2种方法