ASP.NET - 多级分类
2015-09-08 17:05
686 查看
表结构:
表数据:
最终效果:
前端代码:
后端代码:
表数据:
最终效果:
前端代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Test.aspx.cs" Inherits="APManage.Test" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:TreeView ID="TreeView1" runat="server"> </asp:TreeView> </div> </form> </body> </html>
后端代码:
using System; using System.Data; using System.Web.UI.WebControls; using System.Data.SqlClient; namespace APManage { public partial class Test : System.Web.UI.Page { private readonly string ConnString = @"server = HUANGFU-PC; database = DB_APManage; integrated security = true"; private DataTable dt = null; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { dt = new DataTable(); GetMenuToDataTable("select * from Tb_APCategory", dt); BindTree(dt, null, "1000"); } } private void BindTree(DataTable dtSource, TreeNode parentNode, string parentID) { DataRow[] rows = dtSource.Select(string.Format("ParentID={0}", parentID)); foreach (DataRow row in rows) { TreeNode node = new TreeNode(); node.Text = row["CategoryName"].ToString(); node.Value = row["ParentID"].ToString(); BindTree(dtSource, node, row["ID"].ToString()); if (parentNode == null) { this.TreeView1.Nodes.Add(node); } else { parentNode.ChildNodes.Add(node); } } } private DataTable GetMenuToDataTable(string query, DataTable dt) { using (SqlConnection conn = new SqlConnection(ConnString)) { SqlCommand cmd = new SqlCommand(query, conn); SqlDataAdapter ada = new SqlDataAdapter(cmd); ada.Fill(dt); } return dt; } } }
相关文章推荐
- ASP.NET Authentication - Form Authentication
- (转)ASP.NET Identity入门系列教程(一) 初识Identity
- ASP.NET MVC 扩展HtmlHelper类方法
- Asp.Net Mvc4分页,扩展HtmlHelper类
- Aspose.words编程指南之DocumentBuilder二
- 树莓派2B使用360随身WiFi2代连接WiFi
- 树莓派2B安装配置
- 同一Session中的aspx页面的并发限制
- 基于ASP.NET的comet简单实现
- Asp.net笔记
- JS编码和Asp.net编码
- asp.net Cache
- ASP.NET MVC 3 loginUrl自动变成Account/Login,并且发生404错误的解决方法
- asp.net单文件带进度条上传的解决方案
- asp.net上传文件到数据库的解决方案
- asp将本地的文件上传到服务器
- ASP.NET设计FTP文件上传的解决方案
- 初探ASP.NET Mvc5+EF7的美妙旅程
- Asp.Net上传图片同时生成高清晰缩略图
- ASP.NET MVC5添加验证(4)