asp.net操作treeview

來源:互聯網
上載者:User

小弟使用了非常笨拙的寫法,有高手們請指點思路,讓小弟學習學習,拜託了。。。

 

載入樹 -- 使用遞迴寫法 

protected void LoadChannel()
        {
            DataTable dt = bllChannel.GetList();

            DataRow[] parent = dt.Select(" ParentId=0");
            foreach (DataRow parentRow in parent)
            {
                TreeNode node = new TreeNode(parentRow["Title"].ToString(), parentRow["Id"].ToString());
                node.ToolTip = parentRow["Title"].ToString() + "=>" + parentRow["Id"].ToString();//這裡是為了用 js擷取節點的文本、值---寫法感覺很土
                BindTree(dt, node, parentRow["Id"].ToString());
                TreeView1.Nodes.Add(node);
            }
        }
        void BindTree(DataTable dt, TreeNode node, string parentId)
        {
            DataRow[] row = dt.Select(" ParentId=" + parentId);
            foreach (DataRow r in row)
            {
                TreeNode n = new TreeNode(r["Title"].ToString(), r["Id"].ToString());
                n.ToolTip = r["Title"].ToString() + "=>" + r["Id"].ToString();
                node.ChildNodes.Add(n);
                BindTree(dt, n, r["Id"].ToString());
            }
        }

 

js 擷取選中節點的值,分別放入兩個數組

 

var textArr = new Array();
var valueArr = new Array();
 $("input[type='checkbox']:checked").each(function () {
        var tv = $(this).attr("title").split("=>"); //就是上面很土的tooltip值
        textArr.push(tv[0]);
        valueArr.push(tv[1]);
});

//使用join把數組轉成字串,以“||”分隔,並分別填入兩個文本域

$get("cataNam").setAttribute("value", textArr.join("||"));
$get("cataId").setAttribute("value", valueArr.join("||"));

 

 

//載入時,使用遞迴遍曆樹把符合的值選中

string[] cataIdArray=row["CategoryId"].ToString().Split(new string[]{"||"}, StringSplitOptions.RemoveEmptyEntries);//把剛才插入資料庫的值分隔
checkNode(TreeView1.Nodes, cataIdArray);

//遞迴方法

 void checkNode(TreeNodeCollection tnodes, string[] cataIdArray)
{
            foreach (TreeNode tn in tnodes)
            {
                for (int i = 0; i < cataIdArray.Length; i++)
                {
                    if (tn.Value == cataIdArray.GetValue(i).ToString())
                    {
                        tn.Checked = true;
                    }
                }
                checkNode(tn.ChildNodes, cataIdArray);
            }
}

  懇求高手們幫小弟指點指點,因為要是資料量多的話我怕會吃不消。。。。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.