看看效果
无限递归调用 前台代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.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>
<!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 页面
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.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();//更新列表
}
}
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();//更新列表
}
}