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");
}
/**/
/// <summary>
/// 根据递归查询结果动态生成TreeView
/// </summary>
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;
}
}
/// <summary>
/// 递归查询
/// </summary>
/// <param name="nodes">TreeView的节点集合</param>
/// <param name="parentid">所属父级的ID</param>
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);
}
}
}