none
treeview的問題 - TreeView1_TreeNodePopulate怎樣可以動作 RRS feed

  • 問題

  • hi 各位達人好

     

    在使用treeview展出sql資料庫的資料時,只可以成功展出第一層,請問以下的代碼要怎麼修改,才能成功展出兩層內容?

     

    謝謝您!!

        <h3>TreeView1 -
        <asp:TreeView ID="TreeView1" ExpandDepth="10" runat="server"  OnTreeNodePopulate="TreeView1_TreeNodePopulate" ImageSet="Inbox" Width="100%" BackColor="White" BorderColor="#404040" LineImagesFolder="~/TreeLineImages" ShowLines="True" ShowCheckBoxes="All"  >
            <ParentNodeStyle Font-Bold="False" BackColor="#FFFFC0" />
            <HoverNodeStyle Font-Underline="True" />
            <SelectedNodeStyle Font-Underline="True" HorizontalPadding="0px" VerticalPadding="0px" BackColor="Peru" />
            <NodeStyle Font-Names="Verdana" Font-Size="8pt" ForeColor="Black" HorizontalPadding="5px"
                NodeSpacing="0px" VerticalPadding="0px" />
            <RootNodeStyle BackColor="#FFE0C0" />
        </asp:TreeView>

     

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Data.SqlClient;
    using System.Collections.Specialized;


    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
                PopulateRootLevel();

        }
        //TreeView 1//

        private void PopulateRootLevel()
        {
            //Response.Write("PopulateRootLevel");
            string sql;
            sql = "select * from menu WHERE (node_type = 'FOLDER')";

            SqlConnection conn;
            SqlCommand cmd;
            NameValueCollection appSettings = ConfigurationManager.AppSettings;
            conn = new SqlConnection();
            conn.ConnectionString = appSettings[0];
            conn.Open();

            //cmd = new SqlCommand(sql, conn);

            SqlDataAdapter da = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            PopulateNodes(dt, TreeView1.Nodes);
            conn.Close();
        }

        private void PopulateSubLevel(int parentid, TreeNode parentNode)
        {
            //Response.Write("456564");
            string sql;
            sql = "select * FROM menu WHERE (node_type = 'FILE')"; 

            SqlConnection conn;
            SqlCommand cmd;
            NameValueCollection appSettings = ConfigurationManager.AppSettings;
            conn = new SqlConnection();
            conn.ConnectionString = appSettings[0];
            conn.Open();


            SqlDataAdapter da = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            PopulateNodes(dt, parentNode.ChildNodes);
            conn.Close();
        }

        protected void TreeView1_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            Response.Write("TreeView1_TreeNodePopulate");
            PopulateSubLevel(Int32.Parse(e.Node.Value), e.Node);

        }

        private void PopulateNodes(DataTable dt, TreeNodeCollection nodes)
        {
            foreach (DataRow dr in dt.Rows)
            {
                TreeNode tn = new TreeNode();
                tn.Text = dr["node_text"].ToString();
                tn.Value = dr["node_id"].ToString();
                tn.PopulateOnDemand = true;
                nodes.Add(tn);

                //If node has child nodes, then enable on-demand populating
                //tn.PopulateOnDemand = ((int)(dr["parent_node_id"]) > 0);
            }
        }


    }

     

    2007年12月21日 上午 08:25