欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  数据库

用C# + SqlServer2005 实现的treeview动态树形菜单

程序员文章站 2024-02-15 23:36:17
...

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 Syste

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 TreeView : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void btnTV_Click(object sender, EventArgs e)
{
CreateTreeView(TreeView1.Nodes, "0");
}


/**/
///


/// 根据递归查询结果动态生成TreeView
///

private DataSet CreateDataSet()
{
string connetion = "database=test;server=.;uid=sa;pwd=sa123456;";
using (SqlConnection con = new SqlConnection(connetion))
{
SqlCommand com = new SqlCommand("select * from QuHua", con);

SqlDataAdapter da = new SqlDataAdapter(com);
DataSet ds = new DataSet();
da.Fill(ds,"tree");

return ds;
}

}

///


/// 递归查询
///

/// TreeView的节点集合
/// 所属父级的ID
private void CreateTreeView(TreeNodeCollection nodes, string parentid)
{
DataView TempDV = new DataView();
TreeNode TempNode;
//string intId;
TempDV.Table = CreateDataSet().Tables["tree"];
TempDV.RowFilter = "ParentID=" + parentid + "";
foreach (DataRowView drv in TempDV)
{
TempNode = new TreeNode();
TempNode.Value = drv["TreeID"].ToString();
TempNode.Text = drv["TreeName"].ToString();
TempNode.NavigateUrl = drv["NodeUrl"].ToString();
nodes.Add(TempNode);

//intId = drv["ParentID"].ToString();

CreateTreeView(TempNode.ChildNodes, TempNode.Value);

}

}

}