点滴积累【ASP.NET】---ASP.NET TreeView读取数据库

  1 前台:

  2 <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="TreeView._Default" %>

  3 

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

  5 

  6 <html xmlns="http://www.w3.org/1999/xhtml" >

  7 <head runat="server">

  8     <title></title>

  9 </head>

 10 <body>

 11     <form id="form1" runat="server">

 12     <div>

 13         <asp:TreeView ID="TreeView1" runat="server" ShowLines="True">

 14         </asp:TreeView>

 15     </div>

 16     </form>

 17 </body>

 18 </html>

 19 

 20 

 21 

 22 后台:

 23 using System;

 24 using System.Collections.Generic;

 25 using System.Linq;

 26 using System.Web;

 27 using System.Web.UI;

 28 using System.Web.UI.WebControls;

 29 using System.Data;

 30 using System.Data.SqlClient;

 31 using System.Configuration;

 32 

 33 namespace TreeView

 34 {

 35     public partial class _Default : System.Web.UI.Page

 36     {

 37         public static string st = ConfigurationManager.ConnectionStrings["sql"].ToString();

 38         private DataTable dts=new DataTable();

 39         protected void Page_Load(object sender, EventArgs e)

 40         {

 41             if (!IsPostBack)

 42             {

 43                 dts = CreateTable();

 44                 CreateNode();

 45             }

 46         }

 47         public void CreateNode()

 48         {

 49             DataRow[] dr = dts.Select("ParentID=0");

 50             if(dr.Length>0)

 51             {

 52                 foreach(DataRow drr in dr)

 53                 {

 54                     TreeNode tn = new TreeNode();

 55                     tn.Value = drr["MenuID"].ToString();

 56                     tn.Text = drr["MenuName"].ToString();

 57                     tn.Expanded = false;

 58                     tn.SelectAction = TreeNodeSelectAction.Expand;

 59                     TreeView1.Nodes.Add(tn);

 60                     CreateChild(tn,dts);

 61                 }

 62             }

 63             else

 64             {

 65                 TreeNode t=new TreeNode();

 66                 t.Value="";

 67                 t.Text="";

 68                 t.Expanded=false;

 69                 t.SelectAction=TreeNodeSelectAction.Expand;

 70                 TreeView1.Nodes.Add(t);

 71             }

 72         }

 73         public void CreateChild(TreeNode tnn, DataTable dtt)

 74         {

 75             DataRow[] dr = dtt.Select("ParentID=" + tnn.Value);

 76             if (dr.Length > 0)

 77             {

 78                 foreach (DataRow drw in dr)

 79                 {

 80                     TreeNode ts = new TreeNode();

 81                     ts.Value = drw["MenuID"].ToString();

 82                     ts.Text = drw["MenuName"].ToString();

 83                     ts.SelectAction = TreeNodeSelectAction.Expand;

 84                     ts.Expanded = false;

 85                     tnn.ChildNodes.Add(ts);

 86                     CreateChild(ts, dtt);

 87                 }

 88             }

 89         }

 90         public DataTable CreateTable()

 91         {

 92             DataTable d = new DataTable();

 93             using(SqlConnection sql=new SqlConnection(st))

 94             {

 95                 

 96                 SqlCommand sq=new SqlCommand("select * from TreeViewName",sql);

 97                 SqlDataAdapter sda=new SqlDataAdapter();

 98                 sda.SelectCommand = sq;

 99                 sda.Fill(d);

100                 

101             }

102             return d;

103         }

104     }

105 }

 

你可能感兴趣的:(treeview)