Repeater递归
前台代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<asp:Repeater ID="Repeater1" runat="server">
<HeaderTemplate>
<table border="0" align="center" cellpadding="3" cellspacing="1" class="table_b">
<tr>
<td width="30" class="title_bg">ID</td>
<td class="title_bg">名称</td>
<td class="title_bg">排序</td>
<td class="title_bg">管理</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<%#((DataRowView)Container.DataItem)["Colum"]%>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
public void ListControl_Bind(int n)
{
Cy.BLL.Class bll_Class = new Cy.BLL.Class();
DataTable dt = bll_Class.GetList("followid=0").Tables[0];
dt.Columns.Add("Operate", typeof(string));//操作
dt.Columns.Add("Colum", typeof(String));//在dt中增加字段名为Colum的列
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DataRow dr = dt.Rows[i];
string strchar = "";
dr["operate"] = "<a href='" + dr["id"].ToString() + "'>添加下级分类</a> <a href=''>编辑</a> <a href='' onclick='return confirm(\"真的要删除?不可恢复!\");'>删除</a> <a href='?action=sc&&classid =" + dr["id"].ToString() + " '>生成</a>";
strchar += "<tr onmouseover=this.bgColor='#EBFFDC'; onmouseout=this.bgColor='#ffffff'; bgcolor='#ffffff'>";
strchar += "<td align=\"center\">" + dr["id"].ToString() + "</td> ";
strchar += "<td align=\"left\" >" + dr["title"].ToString() + "</td>";
strchar += "<td align=\"center\" height=\"25\">上升 下降</td>";
strchar += "<td align=\"center\" >" + dr["Operate"].ToString() + "</td>";
strchar += "</tr>";
Cy.BLL.Class bll_Classq = new Cy.BLL.Class();
strchar = InitChild(dr, strchar, bll_Classq, 2);
dr["Colum"] = strchar;
}
}
Repeater1.DataSource = dt;
Repeater1.DataBind();
}
private string InitChild(DataRow dr, string strchar, Cy.BLL.Class bll_Classq,int n)
{
DataTable dtt = bll_Classq.GetList("followid='" + dr["id"] + "'").Tables[0];
dtt.Columns.Add("Operate", typeof(string));
dtt.Columns.Add("Colum", typeof(String));
if (dtt.Rows.Count > 0)
{
string s = " ";
for (int j = 1; j <= n; j++)
{
s += " ";
}
for (int k = 0; k < dtt.Rows.Count; k++)
{
DataRow dro = dtt.Rows[k];
string flag = "├";
if (dtt.Rows.Count == 1)
{
flag = "├";
}
else
{
if (k == 0)
{
flag = "┌";
}
if (k == dtt.Rows.Count - 1)
{
flag = "└";
}
}
dro["operate"] = "<a href='" + dro["id"].ToString() + "'>添加下级分类</a> <a href=''>编辑</a> <a href='' onclick='return confirm(\"真的要删除?不可恢复!\");'>删除</a> <a href='?action=sc&&classid =" + dro["id"].ToString() + " '>生成</a>";
strchar += "<tr onmouseover=this.bgColor='#EBFFDC'; onmouseout=this.bgColor='#ffffff'; bgcolor='#ffffff'>";
strchar += "<td align=\"center\">" + dro["id"].ToString() + "</td> ";
strchar += "<td align=\"left\" >" + s + flag + dro["title"].ToString() + "</td>";
strchar += "<td align=\"center\" height=\"25\">上升 下降</td>";
strchar += "<td align=\"center\" >" + dro["Operate"].ToString() + "</td>";
strchar += "</tr>";
strchar = InitChild(dro, strchar, bll_Classq, n + 8);
}
}
return strchar;
}