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 }

最终效果:

ASP.NET - 无限极分类

 

 

 

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

 

 

 

 

导航--------

代码:

 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 }

 

最终效果:

你可能感兴趣的:(asp.net)