zoukankan      html  css  js  c++  java
  • TreeView(递归)

    很简单,不多说
    最终效果:

    数据库(Access):

    代码:

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        
    <title>无标题页</title>
    </head>
    <body>
        
    <form id="form1" runat="server">
            
    <div style="text-align:center;">
                
    <div style="margin:5px; float:left;"><asp:TreeView runat="server" ID="Tree1"></asp:TreeView></div>
                
    <div style="margin-left:12px;"><asp:GridView runat="server" ID="GridView1"></asp:GridView></div>
            
    </div> 
        
    </form>
    </body>
    </html>

    public partial class InfoSort : System.Web.UI.Page
        
    {
            DataSet ds 
    = null;
            
    protected void Page_Load(object sender, EventArgs e)
            
    {
                
    if (!IsPostBack)
                
    {
                    BindData();
                    loadtree();
                }

            }

            
    private void loadtree()
            
    {
                Tree1.Nodes.Clear();
                ds 
    = GetDataSet("SELECT * FROM sort");
                InitTree(Tree1.Nodes, 
    0);
            }

            
    private void InitTree(TreeNodeCollection Nds,int parentid)
            
    {
                DataView dv 
    = new DataView();
                TreeNode tmpNd 
    = null;
                dv.Table 
    = ds.Tables[0];
                dv.RowFilter 
    = String.Format("parentid={0}", parentid);
                
    foreach (DataRowView drv in dv)
                
    {
                    tmpNd 
    = new TreeNode();
                    tmpNd.Text 
    = (string)drv["sortname"];//节点名称
                    tmpNd.NavigateUrl = String.Format("?id={0}", drv["id"]);//节点URL
                    
    //tmpNd.ImageUrl = ""; //节点图片
                    if (parentid == 0)
                        tmpNd.Expanded 
    = true;
                    
    else
                        tmpNd.Expanded 
    = false;
                    Nds.Add(tmpNd);
                    InitTree(Nds[Nds.Count 
    - 1].ChildNodes, (int)drv["id"]);
                }

            }

            
    private void BindData()
            
    {
                GridView1.DataSource 
    = GetDataSet("select * from sort");
                GridView1.DataBind();
            }


            
    Dal
        }
  • 相关阅读:
    C# 实现向指定邮箱发送信息功能
    asp.net webapi 解决跨域问题
    电脑通电自动开机设置
    C# 多个控件绑定一个事件
    C# DataGridView 标题栏背景颜色改变
    C# 输出csv文件后缀乱码
    C# textbox设定为只读后如何改变文本字体颜色
    C# 命名规则
    C# 傅里叶变换 逆变换 调用MathNet包
    使用SharpDevelop配合MonoGame进行游戏开发
  • 原文地址:https://www.cnblogs.com/wfcfan/p/1233450.html
Copyright © 2011-2022 走看看