TreeView 樹形控制項 – asp.net

來源:互聯網
上載者:User
前台:
<asp:TreeView ID="TreeView1" runat="server" ImageSet="Msdn" NodeIndent="10" ShowLines="True" ExpandDepth="0">        <ParentNodeStyle Font-Bold="False" />        <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"                        NodeSpacing="1px" VerticalPadding="2px" />                              <SelectedNodeStyle BackColor="#4a5bb1" ForeColor="white" /> </asp:TreeView>

後台:

//已經定義好的資料protected DataSet ds = new DataSet();CreateTree(this.TreeView1.Nodes,""); //用遞迴的方法顯示樹形目錄        private void CreateTree(TreeNodeCollection TNCs, string strParentId)        {            TreeNode tmpTNCs = new TreeNode();            DataView dv = new DataView();            dv.Table = ds.Tables[0];            string strID;            if (strParentId == "")            {                dv.RowFilter = "isnull(ParentFunctionID,0)=0";            }            else            {                dv.RowFilter = "ParentFunctionID = " + strParentId;            }            foreach (DataRowView drv in dv)            {                tmpTNCs = new TreeNode();//定義結點                strID = drv["subID"].ToString();                tmpTNCs.Value = strID;//定義結點ID                tmpTNCs.Text = drv["subName"].ToString();//定義結點文字                //tmpTNCs.ImageUrl = "img/folderclose.gif";//定義圖片                //tmpTNCs.ExpandedImageUrl = "img/folderopen.gif";//定義開啟的圖片                tmpTNCs.NavigateUrl = drv["Url"].ToString();//定義結點聯結                tmpTNCs.Target = "main";                if (tmpTNCs.NavigateUrl == "right.aspx")                    tmpTNCs.SelectAction = TreeNodeSelectAction.None;                           //選擇不觸發事件                TNCs.Add(tmpTNCs);//添加結點                CreateTree(TNCs[TNCs.Count - 1].ChildNodes, strID);//遞迴調用                //選定某個結點                if (Request.Params["subID"] != null)                {                    if (strID == Request.Params["subID"].ToString())                    {                        //將選定結點的所有上級結點的Expanded屬性全設為true,即展開結點                        string strNodeIndex = TNCs[TNCs.Count - 1].Value;                        string[] strExpandLevel = strNodeIndex.Split('.');                        TreeNode TN = this.TreeView1.Nodes[int.Parse(strExpandLevel[0])];                        TN.Expanded = true;                        for (int i = 1; i < strExpandLevel.Length; i++)                        {                            int ii = int.Parse(strExpandLevel[i]);                            TN = TN.ChildNodes[ii];                            TN.Expanded = true;                        }                        //選定結點                        this.TreeView1.SelectedNode.Value = strNodeIndex;                    }                }            }        }

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.