【问题标题】:create tree view without using tree view controller在不使用树视图控制器的情况下创建树视图
【发布时间】:2019-04-15 16:20:10
【问题描述】:

我想从数据库中显示一个树形视图。我确实使用了 asp.net TreeView 控制器。但我买不起我的项目中的 TreeView 控制器。因此,我正在尝试动态创建树视图。

代码

  using (SqlConnection Conn = new SqlConnection(connection))
                {
                    string State = "Select * from IN_State";
                    string City = "Select * from IN_City";

                    string Treeview = State + ";" + City;

                    DataSet ds = new DataSet();
                    SqlDataAdapter da = new SqlDataAdapter(Treeview, Conn);
                    da.Fill(ds);
                    ds.Tables[0].TableName = "IN_State";
                    ds.Tables[1].TableName = "IN_City";

                    DataRelation dr = new DataRelation("StateCity", ds.Tables["IN_State"].Columns["S_Id"], ds.Tables["IN_City"].Columns["S_Id"]);
                    ds.Relations.Add(dr);

                    foreach (DataRow drState in ds.Tables["IN_State"].Rows)
                    {
                        TreeNode NDState = new TreeNode();
                        NDState.Text = drState["S_Name"].ToString();
                        NDState.Value = drState["S_Id"].ToString();
                        tview.Nodes.Add(NDState);

                        foreach (DataRow drCity in drState.GetChildRows("StateCity"))
                        {
                            TreeNode NDCity = new TreeNode();
                            NDCity.Text = drCity["C_Name"].ToString();
                            NDCity.Value = drCity["C_Id"].ToString();
                            NDState.ChildNodes.Add(NDCity);
                        }
                    }
                }

也尝试使用 System.Web.UI.WebControls.TreeView

【问题讨论】:

  • 您的树是否只有一层(州 => 城市)?在这种情况下,它更像是一个简单的主详细信息视图。树是矫枉过正
  • 请你解释一下@maxence51

标签: c# asp.net .net treeview


【解决方案1】:

我认为,您正在寻找 TreeView 构造函数

<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

  void Page_Load(Object sender, EventArgs e)
  {

    // Create a new TreeView control.
    TreeView NewTree = new TreeView();

    // Set the properties of the TreeView control.
    NewTree.ID = "BookTreeView";
    NewTree.DataSourceID = "BookXmlDataSource";

    // Create the tree node binding relationship.

    // Create the root node binding.
    TreeNodeBinding RootBinding = new TreeNodeBinding();
    RootBinding.DataMember = "Book";
    RootBinding.TextField = "Title";

    // Create the parent node binding.
    TreeNodeBinding ParentBinding = new TreeNodeBinding();
    ParentBinding.DataMember = "Chapter";
    ParentBinding.TextField = "Heading";

    // Create the leaf node binding.
    TreeNodeBinding LeafBinding = new TreeNodeBinding();
    LeafBinding.DataMember = "Section";
    LeafBinding.TextField = "Heading";

    // Add bindings to the DataBindings collection.
    NewTree.DataBindings.Add(RootBinding);
    NewTree.DataBindings.Add(ParentBinding); 
    NewTree.DataBindings.Add(LeafBinding);

    // Manually register the event handler for the SelectedNodeChanged event.
    NewTree.SelectedNodeChanged += new EventHandler(this.Node_Change);

    // Add the TreeView control to the Controls collection of the PlaceHolder control.
    ControlPlaceHolder.Controls.Add(NewTree);

  }

  void Node_Change(Object sender, EventArgs e)
  {

    // Retrieve the TreeView control from the Controls collection of the PlaceHolder control.
    TreeView LocalTree = (TreeView)ControlPlaceHolder.FindControl("BookTreeView");

    // Display the selected node.
    Message.Text = "You selected: " + LocalTree.SelectedNode.Text;

  }

</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
  <head runat="server">
    <title>States</title>
</head>
<body>
    <form id="form1" runat="server">

      <h3>TreeView Constructor Example</h3>

      <asp:PlaceHolder id="ControlPlaceHolder" runat="server">
      </asp:PlaceHolder>

      <asp:XmlDataSource id="BookXmlDataSource"  
        DataFile="Book.xml"
        runat="server">
      </asp:XmlDataSource>

      <br /><br />

      <asp:Label id="Message" runat="server"/>

    </form>
  </body>
</html>

另请参阅 https://docs.microsoft.com/en-us/dotnet/api/system.web.ui.webcontrols.treeview.-ctor?view=netframework-4.7.2

【讨论】:

    【解决方案2】:

    创建一个简单的对象结构。例如,国家列表。 State 类本身包含 State 属性和城市对象列表。

    然后将它与显示列表的 Web 组件一起使用,其中每个列表项都有一个内部详细信息列表。您可以找到一个或自己创建它,因为您的层次结构只有一个级别。

    【讨论】:

    • 我需要树视图而不是列表
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 2012-02-24
    • 1970-01-01
    相关资源
    最近更新 更多