日期:2014-05-20 浏览次数:20426 次
public partial class TreeView : System.Web.UI.Page { DataTable dt = new DataTable(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { dt.Columns.Add("a1"); dt.Columns.Add("a2"); dt.Columns.Add("a3"); dt.Columns.Add("num", Type.GetType("System.Int32")); dt.Columns.Add("url"); dt.Rows.Add(new object[] { "1", "0", "我的文件夹", 2, "http://www.sina.com.cn" }); dt.Rows.Add(new object[] { "2", "0", "工作文件夹", 1, "http://www.baidu.com" }); dt.Rows.Add(new object[] { "3", "1", "图片", 1, "http://www.qq.com" }); dt.Rows.Add(new object[] { "4", "1", "文档", 2, "http://www.hao123.com" }); dt.Rows.Add(new object[] { "5", "3", "风景", 2, "http://www.163.com" }); dt.Rows.Add(new object[] { "6", "3", "人物", 1, "http://bbs.csdn.net" }); dt.Rows.Add(new object[] { "7", "2", "报告", 1, "http://www.taobao.com" }); dt.Rows.Add(new object[] { "8", "3", "写真", 3, "http://www.liuzhou.gov.cn" }); bindData("0", TreeView1.Nodes, "根目录"); } } private void bindData(string parentid, TreeNodeCollection tnc,string path) { DataRow[] ary_row = dt.Select("a2=" + parentid, "num"); foreach (DataRow item in ary_row) { TreeNode node = new TreeNode(); string txtpath = path + "//" + item["a3"].ToString(); node.Text = string.Format("<font onclick=\"selval(this,'{1}','{2}')\" style=\"cursor:pointer;\">{0}</font>", item["a3"].ToString(), item["url"].ToString(),"Right"); node.Value = item["a1"].ToString(); node.Expanded = true;//是否展开 //node.ShowCheckBox = true;//是否显示选择框 node.SelectAction = TreeNodeSelectAction.None; //node.NavigateUrl = "javascript:selval('" + txtpath + "');";//连接路径 //node.SelectAction = TreeNodeSelectAction.Expand;//选择事件 tnc.Add(node); bindData(item["a1"].ToString(), tnc[tnc.Count - 1].ChildNodes, txtpath); } } }