日期:2014-05-18 浏览次数:20482 次
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 System.Data.SqlClient; namespace hzquery.menu { /// <summary> /// menu_Left 的摘要说明。 /// </summary> public class menu_Left : System.Web.UI.Page { [color=#FF0000]protected Microsoft.Web.UI.WebControls.TreeView tvMenu;[/color] SqlConnection Conn; SqlDataAdapter myCmd; DataSet ds; string cmdSelect; private void Page_Load(object sender, System.EventArgs e) { Conn=new SqlConnection(Application["ConnString"].ToString()); CreateDataSet(); [color=#FF0000]InitTree(tvMenu.Nodes,"0");[/color] } //建立数据集 private DataSet CreateDataSet() { cmdSelect="select * from S_Menu"; myCmd=new SqlDataAdapter(cmdSelect,Conn); ds=new DataSet(); myCmd.Fill(ds,"tree"); return ds; } //建树的基本思路是:从根节点开始递归调用显示子树 [color=#FF0000] private void InitTree(TreeNodeCollection Nds,string parentId)[/color] { DataView dv=new DataView(); [color=#FF0000]TreeNode tmpNd;[/color] string intId; dv.Table=ds.Tables["tree"]; dv.RowFilter="ParentId="" + parentId + """ ; foreach(DataRowView drv in dv) { tmpNd=new TreeNode(); tmpNd.ID=drv["NodeId"].ToString(); tmpNd.Text=drv["NodeName"].ToString(); tmpNd.ImageUrl="../images/"+drv["Icon"].ToString(); tmpNd.NavigateUrl="../"+drv["Address"].ToString(); Nds.Add(tmpNd); intId=drv["ParentId"].ToString(); InitTree(tmpNd.Nodes,tmpNd.ID); } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { InitializeComponent(); base.OnInit(e); } private void InitializeComponent() { this.Load += new System.EventHandler(this.Page_Load); } #endregion } }
<asp:TreeView ID="tvMenu" runat="server">
------解决方案--------------------