ASP.NET TreeView读取数据库实例_实用技巧

前台:

复制代码 代码如下:

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

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

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TreeView ID="TreeView1" runat="server" ShowLines="True">
        </asp:TreeView>
    </div>
    </form>
</body>
</html>

后台:

复制代码 代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace TreeView
{
    public partial class _Default : System.Web.UI.Page
    {
        public static string st = ConfigurationManager.ConnectionStrings["sql"].ToString();
        private DataTable dts=new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                dts = CreateTable();
                CreateNode();
            }
        }
        public void CreateNode()
        {
            DataRow[] dr = dts.Select("ParentID=0");
            if(dr.Length>0)
            {
                foreach(DataRow drr in dr)
                {
                    TreeNode tn = new TreeNode();
                    tn.Value = drr["MenuID"].ToString();
                    tn.Text = drr["MenuName"].ToString();
                    tn.Expanded = false;
                    tn.SelectAction = TreeNodeSelectAction.Expand;
                    TreeView1.Nodes.Add(tn);
                    CreateChild(tn,dts);
                }
            }
            else
            {
                TreeNode t=new TreeNode();
                t.Value="空";
                t.Text="空";
                t.Expanded=false;
                t.SelectAction=TreeNodeSelectAction.Expand;
                TreeView1.Nodes.Add(t);
            }
        }
        public void CreateChild(TreeNode tnn, DataTable dtt)
        {
            DataRow[] dr = dtt.Select("ParentID=" + tnn.Value);
            if (dr.Length > 0)
            {
                foreach (DataRow drw in dr)
                {
                    TreeNode ts = new TreeNode();
                    ts.Value = drw["MenuID"].ToString();
                    ts.Text = drw["MenuName"].ToString();
                    ts.SelectAction = TreeNodeSelectAction.Expand;
                    ts.Expanded = false;
                    tnn.ChildNodes.Add(ts);
                    CreateChild(ts, dtt);
                }
            }
        }
        public DataTable CreateTable()
        {
            DataTable d = new DataTable();
            using(SqlConnection sql=new SqlConnection(st))
            {

                SqlCommand sq=new SqlCommand("select * from TreeViewName",sql);
                SqlDataAdapter sda=new SqlDataAdapter();
                sda.SelectCommand = sq;
                sda.Fill(d);

            }
            return d;
        }
    }
}

时间: 2024-10-31 20:03:33

ASP.NET TreeView读取数据库实例_实用技巧的相关文章

ASP.NET(C#)中操作SQLite数据库实例_实用技巧

要想在ASP.NET项目中使用SQLite数据库,先需下载一个ADO.NET 2.0 SQLite Data Provider,下载地址为:http://sourceforge.net/project/showfiles.php?group_id=132486&package_id=145568,下载后安装完毕后,该安装程序自动在在系统注册(即可在"添加引用"中看到所安装的Provider).               然后,在项目中添加上图所选项即可.      aspx页

asp.net页面SqlCacheDependency缓存实例_实用技巧

缓存技术是asp.net程序设计中非常实用的技术,也是大型web程序设计中比较常见的技术.本文就以实例形式对此加以说明.具体如下: asp.net页面的SqlCacheDependency Sql缓存: Cahce/SqlCachePage.aspx页面代码如下: <%@ Page Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true" CodeFile=&qu

asp.net treeview checkbox 相关问题_实用技巧

private void Display() { dt = new TsOrganize().OrganizeDs(); DataView dv = new DataView(dt); TreeNode nodes = new TreeNode(); nodes.Text = "两岸咖啡"; nodes.Value = "0"; if (dt.Rows.Count >= 1) { dv.RowFilter = "fParentId=''";

asp.net cookie的读写实例_实用技巧

asp.net操作cookie的例子,包括创建cookie.读取cookie.清空cookies,有需要的朋友可以参考下. 1.创建cookie 复制代码 代码如下: HttpCookie cookie = new HttpCookie("AutoLogin"); cookie.Values.Add("name", user.UserName); cookie.Values.Add("pass", user.Password); cookie.E

asp.net实现简单分页实例_实用技巧

本文实例讲述了asp.net实现简单分页的方法.分享给大家供大家参考. 具体实现方法如下: 复制代码 代码如下: /// <summary> /// 分页内容 /// </summary> /// <param name="size">页面大小</param> /// <param name="count">页面数量</param> /// <param name="curren

asp.net实现批量删除实例_实用技巧

本文实例讲述了asp.net实现批量删除功能的方法.对于asp.net的学习有一定的参考价值.分享给大家供大家参考之用.具体实现方法入戏: .aspx文件代码如下: <asp:GridView ID="GridView1" runat="server" Width="100%" EmptyDataText="暂时无数据" BorderColor="White" OnRowDeleting="

ASP.Net页面生成饼图实例_实用技巧

本文实例讲述了ASP.Net页面生成饼图的方法.分享给大家供大家参考.具体实现方法如下: 1.生成普通饼图: 复制代码 代码如下: using System; using System.Collections.Generic; using System.Drawing; using System.Drawing.Imaging; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebCon

asp.net图片上传实例_实用技巧

第一.图片上传,代码如下:xxx.aspx 复制代码 代码如下:  <td class="style1">                 <asp:FileUpload ID="FileUpload1" runat="server"  />                <asp:Button ID="Button1" runat="server" Text="上传

一个ASP.Net下的WebShell实例_实用技巧

代码如下: 复制代码 代码如下: <%@ Page Language="C#" AutoEventWireup="true" %><%@ Import Namespace="System.Runtime.InteropServices" %><%@ Import Namespace="System.IO" %><%@ Import Namespace="System.Data