html
- <asp:Panel id="panel1" runat="server" Height="300" ScrollBars="Auto">
- <asp:TreeView ID="PART_VIEW" runat="server" ImageSet="Simple" NodeIndent="10" ShowLines="True" AutoGenerateDataBindings="False" ExpandDepth="1" OnSelectedNodeChanged="PART_VIEW_SelectedNodeChanged">
- <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="0px" NodeSpacing="0px" VerticalPadding="0px" />
- <SelectedNodeStyle Font-Underline="True" ForeColor="#DD5555" HorizontalPadding="0px" VerticalPadding="0px" />
- <HoverNodeStyle Font-Underline="True" ForeColor="#DD5555" />
- <ParentNodeStyle Font-Bold="False" />
- </asp:TreeView>
- </asp:Panel>
C# Code
- public class TreeView_Class :TreeView
- {
- private DataSet ds = null;
- /// <summary>
- ///
- /// </summary>
- ///
- public TreeView_Class(): base()
- {
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="ds"></param>
- public TreeView_Class(DataSet ds)
- {
- this.ds = ds;
- }
- public void SetDataSet(DataSet ds)
- {
- this.ds = ds;
- }
- /// <summary>
- /// 建樹不帶參數
- /// </summary>
- public TreeNode BuildTree()
- {
- //if (ds == null)
- //{
- // return "";
- //}
- this.Nodes.Clear();
- TreeNode root = new TreeNode("物料類別");
- CreatTreeNodes newNode = null;
- for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
- {
- newNode = new CreatTreeNodes(ds.Tables[0].Rows[i][1].ToString());
- newNode.Value = ds.Tables[0].Rows[i][0].ToString();
- // newNode.FunctionCode = ds.Tables[0].Rows[i][3].ToString();
- if (ds.Tables[0].Rows[i]["cata_pid"].ToString() == "0")
- {
- root.ChildNodes.Add(newNode);
- }
- else
- {
- AddChildNodes(root, newNode, ds.Tables[0].Rows[i]["cata_pid"].ToString());
- }
- }
- root.Expand();
- return root;
- }
- /// <summary>
- ///
- /// </summary>
- /// <param name="root">父節點</param>
- /// <param name="newnode">節點</param>
- /// <param name="prentID">節點的序列號</param>
- public void AddChildNodes(TreeNode root, TreeNode newnode, string prentID)
- {
- if (root.Value!= null && prentID.Equals(root.Value.ToString()))
- {
- root.ChildNodes.Add(newnode);
- }
- else
- {
- for (int i = 0; i < root.ChildNodes.Count; i++)
- {
- if (prentID == root.ChildNodes[i].Value.ToString())
- {
- root.ChildNodes[i].ChildNodes.Add(newnode);
- }
- else
- {
- AddChildNodes(root.ChildNodes[i], newnode, prentID);
- }
- //root.Expand();
- }
- }
- }
- /// <summary>
- /// 建樹(包含權限的字段)
- /// </summary>
- /// <param name="ch">字符要顯示的標題</param>
- /// <param name="one">要顯示給客戶看的字段(指數據庫的字段)</param>
- /// <param name="two">權限的字段(指數據庫的字段)</param>
- public void BuildTree(string ch, string one, string two)
- {
- this.Nodes.Clear();
- TreeNode root = new TreeNode(ch);
- CreatTreeNodes newnode = null;
- this.Nodes.Add(root);
- int j = ds.Tables[0].Rows.Count;
- for (int i = 0; i < j; i++)
- {
- newnode = new CreatTreeNodes(ds.Tables[0].Rows[i][one].ToString());
- newnode.Value = ds.Tables[0].Rows[i][0].ToString();
- if (newnode.FunctionCode != null)
- {
- newnode.FunctionCode = ds.Tables[0].Rows[i][two].ToString();
- }
- if (ds.Tables[0].Rows[i][two].ToString() == null || ds.Tables[0].Rows[i][two].ToString() == "")
- {
- root.ChildNodes.Add(newnode);
- }
- else
- {
- AddChildNodes(root, newnode, ds.Tables[0].Rows[i][two].ToString());
- }
- root.Expand();
- }
- }
- //
- public class CreatTreeNodes : TreeNode
- {
- private object functionCode;
- //凳婖滲杅參趼睫珆尨堤懂
- //彆羶衄參趼睫蛌徹饒繫攷憩珆尨諾
- public CreatTreeNodes(string text)
- : base(text)
- {
- }
- //權限
- public object FunctionCode
- {
- get { return functionCode; }
- set { functionCode = value; }
- }
- //蘇凳婖源楊
- //彆狟醱羶衄赽誹萸憩珆尨諾
- public CreatTreeNodes()
- : base()
- {
- this.functionCode = null;
- }
- }
- }
aspx.cs
- private void bind()
- {
- DataSet ds=new DataSet();
- sql ="SELECT CATA_ID,CATA_NAME,CATA_PID FROM BASE_CATA";
- ds = return_PartMater(sql); --方法返回一个DataSet
- TreeNode node=new TreeNode();
- TreeView_Class treeview = new TreeView_Class(ds);
- this.PART_VIEW.Nodes.Add(treeview.BuildTree());
- --调用上面写的一个TreeView类
- }