国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
asp.net為Treeview動(dòng)態(tài)增加節(jié)點(diǎn)實(shí)例

asp.net為Treeview動(dòng)態(tài)增加節(jié)點(diǎn)實(shí)例

  • 標(biāo)簽:Treeview 樹形菜單  更新時(shí)間:2014-01-07
  • 在asp.net中使用TreeView,如果是靜態(tài)的增加節(jié)點(diǎn)數(shù)據(jù),這個(gè)很好辦,但一般情況下,TreeView是要?jiǎng)討B(tài)顯示菜單項(xiàng)的,大部分都是從XML或Access、SqlServer數(shù)據(jù)庫(kù)中加載內(nèi)容,要從數(shù)據(jù)庫(kù)中讀出內(nèi)容動(dòng)態(tài)增加結(jié)點(diǎn),其實(shí)也不難,比如以SQL2000的PUBS數(shù)據(jù)庫(kù)為例子,我們以樹型列表方式取出“作者”做為根結(jié)點(diǎn),再取出對(duì)應(yīng)作者的作品作為子節(jié)點(diǎn),來實(shí)現(xiàn)動(dòng)態(tài)展開并加載數(shù)據(jù)的TreeView,我們可以這樣做:

    <%@ Page Language="C#"%><%@ Import Namespace="System.Data"%><%@ Import Namespace="System.Data.SqlClient"%><%@ Import Namespace="System.Configuration"%><!DOCTYPE htmlPUBLIC"-//W3C//DTD XHTML 1.1//EN""http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title>Dynamic Population of the TreeView Control</title><script runat=server>void Node_Populate(object sender,System.Web.UI.WebControls.TreeNodeEventArgs e){if(e.Node.ChildNodes.Count == 0){switch( e.Node.Depth ){case 0:FillAuthors(e.Node);break;case 1:FillTitlesForAuthors(e.Node);break;}}}void FillAuthors(TreeNode node){string connString = System.Configuration.ConfigurationSettings.ConnectionStrings["NorthwindConnnection"].ConnectionString;SqlConnection connection = new SqlConnection(connString);SqlCommand command = new SqlCommand("Select * Fromauthors",connection);SqlDataAdapter adapter = new SqlDataAdapter(command);DataSet authors = new DataSet();adapter.Fill(authors);if (authors.Tables.Count > 0){foreach (DataRow row in authors.Tables[0].Rows){TreeNode newNode = newTreeNode(row["au_fname"].ToString() + " " +row["au_lname"].ToString(),row["au_id"].ToString());newNode.PopulateOnDemand = true;newNode.SelectAction = TreeNodeSelectAction.Expand;node.ChildNodes.Add(newNode);}}}void FillTitlesForAuthors(TreeNode node){string authorID = node.Value;string connString = System.Configuration.ConfigurationSettings.ConnectionStrings["NorthwindConnnection"].ConnectionString;SqlConnection connection = new SqlConnection(connString);SqlCommand command = new SqlCommand("Select T.title,T.title_id From titles T" +" Inner Join titleauthor TA on
    T.title_id = TA.title_id " +
    " Where TA.au_id = '" + authorID + "'", connection);
    SqlDataAdapter adapter = new SqlDataAdapter(command);
    DataSet titlesForAuthors = new DataSet();
    adapter.Fill(titlesForAuthors);
    if (titlesForAuthors.Tables.Count > 0)
    {
    foreach (DataRow row in titlesForAuthors.Tables[0].Rows)
    {
    TreeNode newNode = new TreeNode(
    row["title"].ToString(), row["title_id"].ToString());
    newNode.PopulateOnDemand = false;
    newNode.SelectAction = TreeNodeSelectAction.None;
    node.ChildNodes.Add(newNode);
    }
    }
    }
    </script>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <asp:TreeViewRunat="Server" ExpandImageUrl="Images/closed.gif"
    CollapseImageUrl="Images/open.gif"
    OnTreeNodePopulate="Node_Populate" ID="tvwauthors">
    <Nodes>
    <asp:TreeNodeText="Authors" PopulateOnDemand=true
    Value="0"/>
    </Nodes>
    </asp:TreeView>
    </div>
    </form>
    </body>
    </html>

    其中,要注意ontreenodepopulate事件,是在展開樹結(jié)點(diǎn)時(shí)發(fā)生的,這里自定義了node_populate來檢查當(dāng)前結(jié)點(diǎn)的深度,如果是0,就是根結(jié)點(diǎn),于是就調(diào)用FillAuthors過程,取出所有的作者,如果深度是1,則是子節(jié)點(diǎn),調(diào)用FillTitlesForAuthors過程讀取作品信息。其中,要注意動(dòng)態(tài)建立樹結(jié)點(diǎn)的過程,如下代碼:

    TreeNode newNode = new TreeNode(row["au_fname"].ToString() + " " +
    row["au_lname"].ToString(),
    row["au_id"].ToString());
    newNode.PopulateOnDemand = true;
    newNode.SelectAction = TreeNodeSelectAction.Expand;
    node.ChildNodes.Add(newNode);

    從popluateondemand的屬性來看,該節(jié)點(diǎn)可以動(dòng)態(tài)擴(kuò)展。

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
C#生成無限級(jí)別菜單 treeview綁定數(shù)據(jù)庫(kù) 實(shí)踐成果
Asp.Net中使用TreeView連接數(shù)據(jù)庫(kù)動(dòng)態(tài)加載節(jié)點(diǎn)問題
TreeView綁定產(chǎn)品信息
數(shù)據(jù)庫(kù)驅(qū)動(dòng)的asp.net treeview
TreeView動(dòng)態(tài)構(gòu)造多級(jí)樹并實(shí)現(xiàn)拖動(dòng)(收藏)
ASP.NET TreeView樹型菜單操作實(shí)例(代碼調(diào)試通過) - mdl821120的...
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服