中国站长下载-为中国站长提供最好最全的建站资源! 首 页发布资源有事留言繁體中文
设为首页
加入收藏
联系我们
 
您当前的位置:中国站长下载 -> 文章中心 -> 网页编程 -> .NET编程 -> 文章内容  虚拟主机 域名注册 退出登录 用户管理
栏目导航
· ASP编程 · .NET编程
· PHP编程 · JSP编程
· CGI 专区
热门文章
· sndvol32 - sndvol3...
· [组图] FLASH:《大话李白》...
· 个人网站到底能赚多...
· [图文] Rundll.exe是病毒吗...
· [组图] Flash:制作MV
· 价值12万元的网站SE...
· 网站创业者,你需要...
· 一个成功的网站设计...
· [图文] FLASH:韩国导航条解...
· 中国网站的赚钱模式...
相关文章
· [图文] 用ASP.NET 2.0设计网...
· [图文] 用ASP.NET 2.0设计网...
· [图文] 用ASP.NET 2.0设计网...
· [图文] 用ASP.NET 2.0设计网...
· [图文] 用ASP.NET 2.0设计网...
· ASP.NET中WebForm组...
· ASP.NET中WebForm组...
· ASP.NET中WebForm组...
· ASP.NET中WebForm组...
· ASP.NET计数器
asp.net 2.0中TREEVIEW中动态增加结点
作者:不详  来源:不详  发布时间:2006-8-14 9:52:07  发布人:chinazhan

 减小字体 增大字体

     在asp.net 2.0中,要动态从数据库中取出内容,动态增加结点,其实不难,比如以SQL SERVER 2000的PUBS数据库为例子,要以树型列表方式,取出作者,做为根结点,然后取出每位作者写过什么书,作为子结点,可以这样
  
  <%@ 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 * From
   authors",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 = 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);
   }
   }
   }
  
   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事件,是在展开树的结点时发生的,这里定义了自定义的NODE_POPULATE,在node_populate中,检查当前结点的深度,如果是0,就是根结点,于是就调用FillAuthors过程,取出所有的作者,如果深度是1,则是叶子结点,调用FillTitlesForAuthors过程。其中,要注意它们中的动态建立树结点的过程,如:
   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属性表明,该结点会动态扩展。
  
    做人要厚道,请注明转自chinazhan中国站长(www.ChinaZhan.com)。

 
[] [返回上一页] [打 印] [收 藏]
上一篇文章:Asp.Net常用函数
∷相关文章评论∷    (评论内容只代表网友观点,与本站立场无关!) [更多评论...]
中国站长下载
中国站长下载

本页只接受PR>=4 IT类站点连接,申请连接,谢谢您们的支持!希望我们的下载站能够真正帮到中国的站长们!
关于本站 - 网站帮助 - 广告合作 - 下载声明 - 友情连接 - 网站地图
Copyright © 2005-2006 ChinaZhan.Net. All Rights Reserved .