using System; using System.Data; using System.Configuration; 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.OleDb; using System.Data.SqlClient; public partial class _Default : System.Web.UI.Page ... { SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["tree_ConnectionString"].ConnectionString); DataSet ds = new DataSet(); protected void Page_Load(object sender, EventArgs e) ...{ if (!(IsPostBack)) ...{ SqlDataAdapter da = new SqlDataAdapter("select * from t_tree order by parentid ", conn); da.Fill(ds, "T_Tree"); InitTree(); } } private void InitTree() ...{ TV.Nodes.Clear(); DataRow[] rows = ds.Tables["t_tree"].Select("parentid=0"); for (int i = 0; i < rows.Length; i++) ...{ TreeNode T_root = new TreeNode(); DataRow dr = rows[i]; T_root.Text = dr["Descricpt"].ToString(); TV.Nodes.Add(T_root); DropDownList1.Items.Add(new ListItem(dr["Descricpt"].ToString(), dr["Descricpt"].ToString())); ShowTree(T_root, dr["id"].ToString()); } } private void ShowTree(TreeNode Nd, String Parent_id) ...{ DataRow[] rows = ds.Tables["t_tree"].Select("parentid=" + Parent_id); if (rows != null) ...{ for (int i = 0; i < rows.Length; i++) ...{ string AddTo = ""; TreeNode Tnd = new TreeNode(); DataRow dr = rows[i]; Tnd.Text = dr["Descricpt"].ToString(); Nd.ChildNodes.Add(Tnd); //string AddTo = new String(' ', (dr["parent_string"].ToString().Split('|').Length - 1) * 2) + "├"; int c_path = dr["parent_string"].ToString().Split('|').Length; for (int n = 1; n < c_path; n++) ...{ AddTo = " | " + AddTo; } DropDownList1.Items.Add(new ListItem(AddTo + " |-" + dr["Descricpt"].ToString(), dr["Descricpt"].ToString())); ShowTree(Tnd, dr["id"].ToString()); } } } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) ...{ if (e.Row.RowType == DataControlRowType.DataRow) ...{ e.Row.Attributes.Add("OnMouseOver", "c=this.style.backgroundColor;this.style.backgroundColor='#E0e0e0'"); e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor=c"); e.Row.Attributes.Add("ondblclick", "alert(this.cells[0].innerText)"); string nullstr = ""; if (e.Row.RowType == DataControlRowType.DataRow) ...{ int c_path = e.Row.Cells[1].Text.ToString().Split('|').Length; for (int n = 1; n < c_path; n++) ...{ nullstr = " | " + nullstr; } e.Row.Cells[0].Text = nullstr + " |-" + e.Row.Cells[0].Text; e.Row.Cells[1].Text = ""; } } } } 數據庫結構: 初次書寫,肯定有不足之處,多多指教