ASP.NET - 无限极分类

 

下拉列表--------

数据库设计:

-- 无限分类 --

-- 数据库:DB_InfiniteCategory

-- 数据表:Tb_Infinite

---------------------------------------------------------------

-- 创建数据库
CREATE DATABASE DB_InfiniteCategory

-- 创建数据表
USE DB_InfiniteCategory
CREATE TABLE Tb_Infinite
(
    id int not null,                        --逻辑主键
    pid int not null,                        --父级
    categoryName varchar(10) not null        --分类名称
)


--使用语句
select id, pid, categoryName from Tb_Infinite where pid = 0

 

代码:

 1 using System;
 2 using System.Web.UI;
 3 using System.Data;
 4 using System.Data.SqlClient;
 5 
 6 using DAL;
 7 using System.Web.UI.WebControls;
 8 
 9 namespace InfiniteCategory
10 {
11     public partial class Default : System.Web.UI.Page
12     {
13         string toadd = "";
14 
15         protected void Page_Load(object sender, EventArgs e)
16         {
17             if (!Page.IsPostBack)
18             {
19                 GetArticleCategory("0");
20             }
21         }
22 
23         public void GetArticleCategory(string pid)
24         {
25             SqlConnection conn = new SqlConnection(" server = HUANGFU-PC; database = DB_InfiniteCategory; integrated security = true");
26             string sql = "select id,categoryName from Tb_Infinite where pid=@pid";
27             SqlCommand cmd = new SqlCommand(sql, conn);
28             SqlParameter Pid = new SqlParameter("@pid", SqlDbType.Int);
29             Pid.Value = pid;
30             cmd.Parameters.Add(Pid);
31             conn.Open();
32             SqlDataReader sdr = cmd.ExecuteReader();
33             while (sdr.Read())
34             {
35                 this.DropDownList1.Items.Add(new ListItem(toadd + " " + sdr[1].ToString(), sdr[0].ToString()));
36                 toadd += "─┴";
37                 this.GetArticleCategory(sdr[0].ToString());
38                 toadd = toadd.Substring(0, toadd.Length - 2);
39             }
40             sdr.Close();
41             conn.Close();
42         }
43     }
44 }

最终效果:

 

 

 

===============================================================

 

 

 

 

导航--------

代码:

 1 using System;
 2 using System.Web.UI;
 3 using System.Data;
 4 using System.Data.SqlClient;
 5 using System.Text;
 6 
 7 using DAL;
 8 using System.Web.UI.WebControls;
 9 
10 namespace InfiniteCategory
11 {
12     public partial class Default : System.Web.UI.Page
13     {
14         StringBuilder s = new StringBuilder();
15 
16         protected void Page_Load(object sender, EventArgs e)
17         {
18             if (!Page.IsPostBack)
19             {
20                 GetArticleCategory("16");
21             }
22         }
23 
24         public void GetArticleCategory(string id)
25         {
26             SqlConnection conn = new SqlConnection(" server = HUANGFU-PC; database = DB_InfiniteCategory; integrated security = true");
27             string sql = "select id,pid,categoryName from Tb_Infinite where id=@id";
28             SqlCommand cmd = new SqlCommand(sql, conn);
29             SqlParameter Pid = new SqlParameter("@id", SqlDbType.Int);
30             Pid.Value = id;
31             cmd.Parameters.Add(Pid);
32             conn.Open();
33             SqlDataReader sdr = cmd.ExecuteReader();
34             while (sdr.Read())
35             {
36                 s.Append(sdr[2].ToString()+">");
37                 this.GetArticleCategory(sdr[1].ToString());
38             }
39             this.Label1.Text = s.ToString();
40             sdr.Close();
41             conn.Close();
42         }
43     }
44 }

 

最终效果:

转载于:https://www.cnblogs.com/KTblog/p/4638557.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值