none
treeview的OnTreeNodePopulate如何作用? RRS feed

  • 問題

  • hi 各位達人好

     

    在嘗試用treeview連sql秀出資料時,可以長出第一層,但第二層沒出現,以下是我的程式碼,

    請問要怎麼改??  初步以為是 OnTreeNodePopulate沒作用...

     

    謝謝您!!

    <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>

     

     

    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')"; //<> 'FOLDER')";
            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:37

所有回覆

  • HI,

     

    您必須設定TreeView控制項的DataBinding屬性, 指定TreeView的節點要顯示那一個Tag的那一個屬性值(指定ValueField和TextField), 例如使用TreeView控制項讀取以下的XML文件:

     

    <?xml version="1.0" encoding="utf-8" ?>
    <!-- This file represents a fragment of a Categories inventory database -->
    <Categories>
      <Category ID="1" Name="Beverages">
        <Description Value="Soft drinks, coffees, teas, beers, and ales"/>
      </Category>
      <Category ID="2" Name="Condiments">
          <Description Value="Sweet and savory sauces, relishes, spreads, and seasonings"/>
      </Category>
      <Category ID="3" Name="Confections">
          <Description Value="Desserts, candies, and sweet breads"/>
      </Category>
      <Category ID="4" Name="Dairy Products">
          <Description Value="Cheeses"/>
      </Category>
    </Categories>

     

    搭配以下的TreeView控制項設定, 就可以呈現階層的效果:

     

    <asp:TreeView ID="TreeView1" runat="server" DataSourceID="XmlDataSource1">
                <DataBindings>
                    <asp:TreeNodeBinding DataMember="Category" TextField="Name" ValueField="ID" />
                    <asp:TreeNodeBinding DataMember="Description" TextField="Value" ValueField="Value" />
                </DataBindings>
    </asp:TreeView>
    <asp:XmlDataSource ID="XmlDataSource1" runat="server" DataFile="~/Categories.xml"></asp:XmlDataSource>

     

    tihs

     

    2007年12月22日 上午 05:42