当前位置:Gxlcms > mysql > 用C#+SqlServer2005实现的treeview动态树形菜单

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

时间:2021-07-01 10:21:17 帮助过:66人阅读

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);

}

}

}

人气教程排行