ASP.NET Tutorial/Data Binding/TreeView

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

Fibonacci Tree On Demand

<%@ Page Language="C#" Title="Fibonacci Tree On Demand" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
protected void PopulateNode(Object source, TreeNodeEventArgs e)
{
    double nodeText = double.Parse(e.Node.Text);
    double nodeValue = double.Parse(e.Node.Value);
    
    TreeNode newNode = new TreeNode(nodeValue.ToString(), (nodeText + nodeValue).ToString());
    newNode.PopulateOnDemand = true;
    
    e.Node.ChildNodes.Add(newNode);    
}
    
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server" />
<body>
    <form id="form1" runat="server">
    <div>
      <h3>Fibonacci Tree</h3>
        <asp:TreeView ID="_fibonacciTree" OnTreeNodePopulate="PopulateNode" 
                      runat="server" ExpandDepth="1">
            <Nodes>
                <asp:TreeNode Text="1" Value="1">
                  <asp:TreeNode PopulateOnDemand="true" Text="1" Value="2" />
                </asp:TreeNode>
            </Nodes>
        </asp:TreeView>
    </div>
    </form>
</body>
</html>


On demand file

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="OnDemandFiles" %>
<!DOCTYPE html PUBLIC "-//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>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView1" OnTreeNodePopulate="PopulateNode" runat="server" ExpandDepth="0">
            <Nodes>
                <asp:TreeNode PopulateOnDemand="True" Text="Demos" Value="Demos"></asp:TreeNode>
            </Nodes>
        </asp:TreeView>
    
    </div>
    </form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Collections;
using System.Data;
using System.Data.rumon;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class OnDemandFiles : System.Web.UI.Page
{
  static readonly char[] _slashArray = new char[] { "/" };
  protected void PopulateNode(Object source, TreeNodeEventArgs e)
  {
    TreeNode node = e.Node;
    if (node.Value == "Demos")
      node.Value = "~/";
    String rootDirectory = Request.MapPath("~/", Request.ApplicationPath, false);
    String fullPath = Request.MapPath(node.Value, Request.ApplicationPath, false);
    if (fullPath.StartsWith(rootDirectory) == false)
    {
      return;
    }
    String[] dirs = Directory.GetDirectories(fullPath);
    foreach (String dir in dirs)
    {
      String virtualDir = node.Value.TrimEnd(_slashArray) + "/" + Path.GetFileName(dir);
      TreeNode newNode = new TreeNode(Path.GetFileName(dir), virtualDir);
      if (Directory.GetFiles(dir).Length > 0
          || Directory.GetDirectories(dir).Length > 0)
      {
        newNode.PopulateOnDemand = true;
      }
      node.ChildNodes.Add(newNode);
    }
    String[] files = Directory.GetFiles(fullPath);
    foreach (String file in files)
    {
      TreeNode newNode = new TreeNode(Path.GetFileName(file), Path.GetFileName(file));
      node.ChildNodes.Add(newNode);
    }
  }
}


The ASP.NET 3.5 Framework includes two hierarchical DataBound controls

Menu: Displays data items in a static or dynamic menu.
TreeView: Displays data items in a tree.
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<body>
    <form id="form1" runat="server">
    <div class="floater">
    <h3>TreeView</h3>
    <asp:TreeView
        id="CheckBoxList1"
        DataSourceId="srcData"
        Runat="server" />
    </div>
    <div class="floater">
    <h3>Menu</h3>
    <asp:Menu
        id="BulletedList1"
        DataSourceId="srcData"
        Runat="server" />
    </div>
    <asp:XmlDataSource
        id="srcData"
        DataFile="~/Data.xml"
        XPath="/movies/*"
        Runat="server" />
    </form>
</body>
</html>
File: Data.xml
<movies>
  <Adventure>
    <StarWars />
    <JurassicPark />
    <IndependenceDay />
  </Adventure>
  <Animation>
    <IceAge />
    <Shrek />
  </Animation>
  <Drama>
    <Titanic />
    <Ghost />
    <ForrestGump />
  </Drama>
  <Horrer>
    <Jaws />
    <TheRing />
  </Horrer>
</movies>