筆者前言:本文要說(shuō)的內(nèi)容僅針對(duì)ASP.NET 2.0以上版本適用,內(nèi)容也非常的簡(jiǎn)單,本來(lái)不應(yīng)該放在金喜正規(guī)買(mǎi)球的,但發(fā)現(xiàn)不知道的開(kāi)發(fā)者非常多,有必要提醒一下。所以請(qǐng)熟悉Treeview的朋友包涵了,呵呵。
現(xiàn)在Ajax流行,各類(lèi)Tree的第三方控件支持異步加載的非常多。我們項(xiàng)目組的成員通常到處找些樹(shù)控件來(lái)用,給項(xiàng)目的穩(wěn)定性帶來(lái)很多隱患,出了幾次問(wèn)題。
我建議大家考慮直接使用.net自帶的TreeView控件。但這個(gè)控件這樣才能異步加載呢?
先看看示例代碼:
其中 SelectAction="Expand" PopulateOnDemand="true" 這兩個(gè)屬性是關(guān)鍵,在后臺(tái)代碼中,大家也可以看到給有子節(jié)點(diǎn)的節(jié)點(diǎn),需要設(shè)置相關(guān)屬性
<asp:TreeView ID="TreeView1" runat="server" EnableClientScript="true" OnTreeNodePopulate="PopulateNode" ImageSet="Arrows" ExpandDepth="0">
<Nodes>
<asp:TreeNode Text="部門(mén)樹(shù)" Value="部門(mén)樹(shù)" SelectAction="Expand" PopulateOnDemand="true"/>
</Nodes>
</asp:TreeView>
后臺(tái)代碼:
注意PopulateNode ,這個(gè)代碼中的寫(xiě)法,是因?yàn)槲疫B接的是一個(gè)每個(gè)層級(jí)的部門(mén)分別放在不同數(shù)據(jù)庫(kù)表,如果各個(gè)層級(jí)的部門(mén)數(shù)據(jù)都在同一個(gè)表里,代碼將更加簡(jiǎn)單。
TopSmart.UtilClass.Data.IDatabase 這個(gè)我自己封裝數(shù)據(jù)庫(kù)的類(lèi),大家可以替換使用自己常用的數(shù)據(jù)庫(kù)類(lèi),然后就可以了
public void PopulateNode(Object sender, TreeNodeEventArgs e)
{
PopulateMe(e.Node,e.Node.Depth);
}
public void PopulateMe(TreeNode node, int Depth)
{
TopSmart.UtilClass.Data.IDatabase db = TopSmart.UtilClass.Data.DataAccess.DatabaseCreate("hr");
DataTable dt;
if (Depth == 0)
dt = db.GetDataTableFromSQL("select t.onelevelname as name,t.onelevelno as no from hrt_onelevel_dic t where t.valid = 1");
else if (Depth == 1)
dt = db.GetDataTableFromSQL("select t.twolevelname as name,t.twolevelno as no from hrt_twolevel_dic t where t.valid = 1 and t.onelevelno = '" + node.Value + "'");
else
dt = db.GetDataTableFromSQL("select t.deptname as name,t.deptno as no from hrt_dept_dic t where t.valid = 1 and t.twolevelno = '" + node.Value + "'");
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
TreeNode NewNode = new TreeNode(row["name"].ToString(), row["no"].ToString());
NewNode.PopulateOnDemand = (Depth == 2) ? false : true;
NewNode.SelectAction = TreeNodeSelectAction.Expand;
node.ChildNodes.Add(NewNode);
}
}
}
NewNode.PopulateOnDemand = (Depth == 2) ? false : true; 這句是因?yàn)榈扔?的時(shí)候,我的系統(tǒng)沒(méi)有更低的子部門(mén)了。
標(biāo)簽:
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自:CSDN