treeview实例
aspx页面:
View Code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeView.aspx.cs" Inherits="TreeView" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>无标题页</title> </head> <body> <form id="form1" runat="server"> <div> <asp:TreeView ID ="treeVType" runat ="server"> </asp:TreeView> </div> </form> </body> </html>
aspx.cs代码:
View Code
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using DBHelper; public partial class TreeView : System.Web.UI.Page { public static readonly string ConnectionString= ConfigurationManager.AppSettings["pubsConnectionString"].ToString(); protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack){ DataSet ds=new DataSet(); ds=SqlHelper.ExecuteDataSet(ConnectionString, CommandType.StoredProcedure, "proc_getallou", null); dt=ds.Tables[0]; TreeViewLoad(dt);// } } public void TreeViewLoad(DataTable dataSource) { if (dataSource == null) return; DataView athreads = null; athreads = new DataView(dataSource); athreads.RowFilter = "parentid =''";//过滤父节点ID空的节点 即为根节点 foreach (DataRowView row in athreads) { TreeNode threadNode = new TreeNode(); threadNode.Text = stringformat(HttpUtility.HtmlEncode(row["nodename"].ToString()), 30);//截取长度 if (GetStrlength(HttpUtility.HtmlEncode(row["nodename"].ToString())) > 30)//截取长度 { threadNode.ToolTip = HttpUtility.HtmlEncode(row["nodename"].ToString()); } threadNode.Value = row["nodeid"].ToString(); threadNode.Expand();//展开节点 treeVType.Nodes.Add(threadNode);//往树添加根节点 AddChilds(dataSource, threadNode);//调用循环添加节点方法 } } protected void AddChilds(DataTable dt, TreeNode node) { DataView replies = new DataView(dt); replies.RowFilter = "parentid='" + node.Value + "'";//过滤父节点ID foreach (DataRowView row in replies) { TreeNode replyNode = new TreeNode(); replyNode.Text = stringformat(HttpUtility.HtmlEncode(row["nodename"].ToString()), 30);//截取长度 if (GetStrlength(HttpUtility.HtmlEncode(row["nodename"].ToString())) > 30)//截取长度 { replyNode.ToolTip = HttpUtility.HtmlEncode(row["nodename"].ToString()); } replyNode.Value = row["nodeid"].ToString(); replyNode.Expand(); node.ChildNodes.Add(replyNode); AddChilds(dt, replyNode);//调用循环添加节点方法 } } private int GetStrlength(string str) { int t = 0; char[] q = str.ToCharArray(); for (int i = 0; i < q.Length; i++) { if ((int)q[i] >= 0x4E00 && (int)q[i] <= 0x9FA5)//是否汉字 { t += 2; } else { t++; } } return t; } private string stringformat(string str, int n) { /// ///格式化字符串长度,超出部分显示省略号,区分汉字跟字母。汉字2个字节,字母数字一个字节 /// string temp = string.Empty; if (System.Text.Encoding.Default.GetByteCount(str) <= n)//如果长度比需要的长度n小,返回原字符串 { return str; } else { int t = 0; char[] q = str.ToCharArray(); for (int i = 0; i < q.Length && t < n; i++) { if ((int)q[i] >= 0x4E00 && (int)q[i] <= 0x9FA5)//是否汉字 { temp += q[i]; t += 2; } else { temp += q[i]; t++; } } return (temp + "..."); } } }