TreeViewツリーコントロール-asp.net

3273 ワード

フロント:
<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;

                    }

                }

            }

        }