推荐微软发布的treeview的web控件,在asp.net网站上有。
解决方案 »
- objectdatasource與業務邏輯類綁定問題
- 为什么母版页的 Page_PreInit 事件没执行呢?
- 求简单工厂和抽象工厂在项目中应用的例子
- 关于滚筒式打印得问题
- vs.net2003Ajax对象序列化问题
- 各位大侠帮忙指教gridview编辑更新问题
- 求ASP.NET中时间录入的选择对话框,或时间录入的相关解决方案,能有解决方案者请留盐.闲人勿进.
- 在网上搜集了一些精品的.net 控件,提供给大家共享。:)
- 求大虾帮忙,谁手上有Quartz的实例代码或自己写的例子
- 一个有一点难度的问题
- DataGrid有两个问题搞的不是很明白
- 想做个有选择性的列表,但不知是用label还是datagrid?谢谢。在线中~~
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 TreeCS
{
///
/// WebForm1 的摘要说明
///
public class WebForm1 : System.Web.UI.Page
{
protected Microsoft.Web.UI.WebControls.TreeView TreeView2;
protected Microsoft.Web.UI.WebControls.TreeView TreeView1;
private void Page_Load(object sender, System.EventArgs e)
{
// 定义数据库连接
SqlConnection CN = new SqlConnection();
try
{
//初始化连接字符串
CN.ConnectionString=
"data source=zhangce;initial catalog=tree;persist security info=False;user id=sa;Password=;";
CN.Open(); SqlDataAdapter adp = new SqlDataAdapter("select * from tbTree",CN);
DataSet ds=new DataSet();
adp.Fill(ds);
this.ViewState["ds"]=ds;
}
catch (Exception ex)
{
Session["Error"] = ex.ToString();
Response.Redirect("error.aspx"); //̀跳转程序的公共错误处理页面
}
finally
{
CN.Close();
}
//调用递归函数,完成树形结构的生成
AddTree(0, (TreeNode)null);
} //递归添加树的节点
public void AddTree(int ParentID,TreeNode pNode)
{
DataSet ds=(DataSet) this.ViewState["ds"];
DataView dvTree = new DataView(ds.Tables[0]);
//过滤ParentID,得到当前的所有子节点
dvTree.RowFilter = "[PARENTID] = " + ParentID; foreach(DataRowView Row in dvTree)
{
TreeNode Node=new TreeNode() ;
if(pNode == null)
{ //添加根节点
Node.Text = Row["ConText"].ToString();
TreeView2.Nodes.Add(Node);
Node.Expanded=false;
AddTree(Int32.Parse(Row["ID"].ToString()), Node); //再次递归
}
else
{ //̀添加当前节点的子节点
Node.Text = Row["ConText"].ToString();
pNode.Nodes.Add(Node);
Node.Expanded = false;
AddTree(Int32.Parse(Row["ID"].ToString()),Node); //再次递归
}
}
} #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
///设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load); }
#endregion
}
}