实现无限 递归 操作(可用作盖楼)

 看看效果

     无限递归调用 前台代码

ExpandedBlockStart.gif 代码
<% @ Page Language = " C# "  AutoEventWireup = " true "   CodeFile = " Default.aspx.cs "  Inherits = " _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 > ASP.NET 无限级分类 示例 </ title >
</ head >
< body >
    
< form  id ="form1"  runat ="server" >
    
< div >
    注意:本页面仅演示 无限极分类 (表格、下拉列表)的操作方法,具体功能请自行填充!    
    
</ div >
       
< table  width ="100%"     border ='1'  cellspacing ="0"  style ="border-collapse: collapse;"   bgcolor ="#ffffff"  bordercolor ="#cccccc" >
                   
< tr  align ="center"  valign ="middle" >  
                     
< th  height ="22"  colspan ="88" > 分 类 管 理  </ th >
                   
</ tr >
                  
< tr >< td > 添加根分类 </ td >
                  
< td >
                                     
< asp:TextBox  runat ="server"  ID ="txbroot" ></ asp:TextBox >
                   
< asp:Button  runat ="server"   ID ="btnroot"  Text ="确定添加"  onclick ="btnroot_Click"   />

                  
</ td >
                  
</ tr >
                  
< tr >
                  
< td > 添加子分类 </ td >
                  
< td  >
                  在
                  
< asp:DropDownList  runat ="server"  ID ="ddllm"     ></ asp:DropDownList >
                          下添加子分类
                         
                   
< asp:TextBox  runat ="server"  ID ="txbzifl" ></ asp:TextBox >
                   
< asp:Button  runat ="server"   ID ="btnziok"  Text ="确定添加"  onclick ="btnziok_Click"   />
                    
                   
</ td >
                  
</ tr >
                  
                  
< tr >< td > 名称 </ td >< td > 操作 </ td ></ tr >
                  
< asp:Literal  runat ="server"  ID ="litlist" ></ asp:Literal >
      
</ table >
    
</ form >
</ body >
</ html >

 

cs 页面

 

ExpandedBlockStart.gif 代码
using  System;
using  System.Configuration;
using  System.Data;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.HtmlControls;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  FTchina;


public   partial   class  _Default : System.Web.UI.Page
{

    
     
protected   void  Page_Load( object  sender, EventArgs e)
    {
        
if  ( ! IsPostBack)
        {
            LoadList();
        }
    }

    
///   <summary>
    
///  加载整个分类表
    
///   </summary>
     private   void  LoadList()
    {
        litlist.Text 
=   "" ;

        
int  MTcount  =   0 ; // 执行遍历的次数
        
// 开始迭代加载
        MakeTr( " 1 " , MTcount);

    }
    
///   <summary>
    
///  装配每一行
    
///   </summary>
    
///   <param name="id"> 上级ID </param>
    
///   <param name="count"> 迭代次数 </param>
     private   void  MakeTr( string  id,  int  count)
    {

        DataView dv 
=   new  DataView(shujuku.GetDataTable( " select * from lanmu where parentid= "   +  id  +   "  order by id asc " ));
        
foreach  (DataRowView drv  in  dv)
        {
            
// 形成表格
            litlist.Text  +=   " <tr><td> "   +  MakeFelgefu(count)  +  drv[ " context " ].ToString()  +   " </td><td>自身编号 "   +  drv[ " id " ].ToString()  +   " ,上级编号 "   +  id  +   " </td></tr> " ;

            
// 形成下拉列表
            ListItem li  =   new  ListItem(MakeFelgefu(count)  +  drv[ " context " ].ToString(), drv[ " id " ].ToString());
            ddllm.Items.Add(li);

            
// 再次遍历
            MakeTr(drv[ " id " ].ToString(), count  +   1 );

        }

    }
    
///   <summary>
    
///  Make分隔符
    
///   </summary>
    
///   <param name="count"></param>
    
///   <returns></returns>
     private   string  MakeFelgefu( int  count)
    {
        
string  Returnwords  =   string .Empty;
        
if  (count  ==   0 )
        {
            Returnwords 
=   "" ;
        }

        
else
        {
            Returnwords 
=  ( " " ).PadLeft(count,  '   ' );
        }
        
return  Returnwords;
    }

    



    
protected   void  btnziok_Click( object  sender, EventArgs e)
    {
        
string  parentid  =  ddllm.SelectedValue.ToString();
        
string  context  =  txbzifl.Text;
        
string  sql  =   " insert into lanmu (parentid,context) values( "   +  parentid  +   " ,' "   +  context  +   " ')  " ;
        shujuku.ExecuteSql(sql);
        LoadList();
// 更新列表

    }
    
protected   void  btnroot_Click( object  sender, EventArgs e)
    {
        
string  context  = txbroot.Text;
        
string  sql  =   " insert into lanmu (parentid,context) values(1,' "   +  context  +   " ')  " ;
       shujuku.ExecuteSql(sql);
       LoadList();
// 更新列表

    }
}

 

 

完整代码下载

转载于:https://www.cnblogs.com/caoqichen/archive/2010/05/12/1733763.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值