Treeview的数据库结构输出为分级缩进的DropdownList

本文介绍了一种将TreeView控件转换为分级缩进的DropDownList的方法,通过生成分类的代及代序列,实现无需PostBack即可完成分类选择的功能,提高了页面执行效率。
我们在使用Treeview控件实现文章编辑的分类选择的时候, 点击分类整个页面会PostBack,当然采用AJAX可以实现局部刷新。但是无论怎样,页面的执行效率会降低。所以本代码的目标是将Treeview输出为分级缩进的DropDownlist。

其思路是:生成分类的代及代序列,以代形成缩进并以代序列排序。
以下是返回以代序列排序的分类列表。以代形成缩进略。


 1 
 2 -- =============================================
 3 -- Author:        <Author,,Name>
 4 -- Create date: <Create Date,,>
 5 -- Description:    <Description,,>
 6 -- =============================================
 7 ALTER PROCEDURE [dbo].[CategoryTreeGeneration]
 8     -- Add the parameters for the stored procedure here
 9 AS
10 BEGIN
11     -- SET NOCOUNT ON added to prevent extra result sets from
12     -- interfering with SELECT statements.
13     Set NOCOUNT ON
14 
15     Create Table #CategoryTree(
16     CategoryID Nvarchar(10),
17     Generation int,
18     GenerationLine Varchar(25Default ''
19     )   
        //构建临时表,存储每一分类的ID,代,代序列
20
21     Declare
22     @Generation int,
23     @Root Nvarchar(10)
24 
25     set @Generation = 1
26     set @Root = '0'
27     //根的分类ID

28     insert #CategoryTree (CategoryID, Generation,GenerationLine)
29         select @Root,@Generation,@Root
       //填充根分类

30     WHile @@RowCount >0
31     Begin
32         set @Generation = @Generation+1
33             insert #CategoryTree(CategoryID,Generation,GenerationLine)
34                 SELECT ArticleCategory.CategoryID,@Generation,#CategoryTree.GenerationLine
35                 +' '+ substring(ArticleCategory.CategoryID,len(ArticleCategory.CategoryID)-1,2)
       //分类ID以两位分级,所以取ID后两位作为代序列取值
36                 from ArticleCategory
37                     join #CategoryTree
38                     on #CategoryTree.Generation = @Generation-1
39                         And
40                         ArticleCategory.ParentID = #CategoryTree.CategoryID
41     end
42     
43     select ID,#CategoryTree.CategoryID as CategoryID,Generation, CategoryName from #CategoryTree join ArticleCategory 
44         on #CategoryTree.CategoryID = ArticleCategory.CategoryID
45         order by GenerationLine
       //以代序列排序输出
46 
47     Set NOCOUNT Off
48
49     
50 
51 END


52 
53 
54 
55 
56 
57 

转载于:https://www.cnblogs.com/robinsmile/archive/2007/09/21/900920.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值