欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C#递归方法实现无限级分类显示效果实例

程序员文章站 2023-11-21 23:17:46
本文实例讲述了c#递归方法实现无限级分类显示效果。分享给大家供大家参考,具体如下: 运行效果如下图所示: 具体代码如下: <%@ page lang...

本文实例讲述了c#递归方法实现无限级分类显示效果。分享给大家供大家参考,具体如下:

运行效果如下图所示:

C#递归方法实现无限级分类显示效果实例

具体代码如下:

<%@ page language="c#" autoeventwireup="true" codefile="roledemo20150305.aspx.cs" inherits="roledemo20150305" %>
<!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>c#无限级分类递归显示示例</title>
  <style type="text/css">
  span { float:left; clear:both;}
  </style>
  <script src="js/jquery-1.9.1.js" type="text/javascript"></script>
  <script type="text/javascript">
    $(function () {
      $("span").eq(0).show();
      $("span").each(function (i) {
        var curr = $(this).attr("data-id");
        $(this).click(function () {
          $("span").each(function (i) {
            var father = $(this).attr("data-father");
            if (father == curr) {
              $(this).show();
            }
          });
        });
      });
    });
  </script>
</head>
<body>
  <form id="form1" runat="server">
  <div>
    <asp:gridview id="gridview1" runat="server">
    </asp:gridview>
    <br/><br/><br/><br/>
    <asp:literal id="lt1" runat="server"></asp:literal>
  </div>
  </form>
</body>
</html>

using system;
using system.collections.generic;
using system.linq;
using system.web;
using system.text;
using system.data;
using system.web.ui;
using system.web.ui.webcontrols;
public partial class roledemo20150305 : system.web.ui.page
{
  protected void page_load(object sender, eventargs e)
  {
    datatable dt = createdt();
    datatable dtfinal = dt.clone();
    list<string> list = new list<string>();
    string userstr = getuserdata();
    if (userstr.contains(","))
    {
      string[] strs = userstr.split(',');
      for (int i = 0; i < strs.length; i++)
      {
        list.add(strs[i]);
      }
    }
    else
    {
      list.add(userstr);
    }
    for (int i = 0; i < list.count; i++)
    {
      string module_id = list[i];
      datarow[] drsource = dt.select(" module_id = '" + module_id + "'"); //最终dt是否存在该数据
      datarow[] drexist = dtfinal.select(" module_id = '" + module_id + "'"); //最终dt是否存在该数据
      if (drexist.length == 0)
      {
        dtfinal.rows.add(drsource[0]["module_id"], getpretag(drsource[0]["module_level"]) + drsource[0]["module_name"], drsource[0]["module_fatherid"],
          drsource[0]["module_url"], drsource[0]["module_order"], drsource[0]["module_level"]); //添加记录
      }
      findfatherfromdt(dt, drsource, dtfinal);
    }
    //显示方式一
    dataview dv = dtfinal.defaultview;
    dv.sort = "module_id asc";
    datatable dtneed = dv.totable();
    gridview1.datasource = dtneed;
    gridview1.databind();
    //显示方式二
    stringbuilder builder = new stringbuilder();
    for (int i = 0; i < dtneed.rows.count; i++)
    {
      datarow dr = dtneed.rows[i];
      builder.appendline("<span data-id='" + dr["module_id"] + "' data-father='" + dr["module_fatherid"] + "' style='display:none'>"
       + dr["module_name"] + "</span>");
    }
    lt1.text = builder.tostring();
  }
  /// <summary>
  /// 继续追溯添加父类记录
  /// </summary>
  /// <param name="dt"></param>
  /// <param name="dtfinal"></param>
  protected void findfatherfromdt(datatable dt, datarow[] drfather, datatable dtfinal)
  {
    datarow[] drfatherup = dt.select(" module_id = '" + drfather[0]["module_fatherid"] + "'");
    datarow[] drfatherupexist = dtfinal.select(" module_id = '" + drfather[0]["module_fatherid"] + "'");
    if (drfatherup.length > 0 && drfatherupexist.length == 0) //该记录存在父类且父类未添加到最终dt
    {
      dtfinal.rows.add(drfatherup[0]["module_id"], getpretag(drfatherup[0]["module_level"]) + drfatherup[0]["module_name"], drfatherup[0]["module_fatherid"],
          drfatherup[0]["module_url"], drfatherup[0]["module_order"], drfatherup[0]["module_level"]); //继续追溯添加父类记录
      findfatherfromdt(dt, drfatherup, dtfinal);
    }
  }
  #region 添加前缀
  protected string getpretag(object num)
  {
    string pre = string.empty;
    int i = convert.toint32(num);
    if (i < 2)
    {
    }
    else if (i == 2)
    {
      pre += "└";
    }
    else
    {
      pre += "└";
      for (int j = 0; j < i - 2; j++)
      {
        pre += "─";
      }
    }
    return pre;
  }
  #endregion
  #region 用户测试数据
  protected static string getuserdata()
  {
    datatable dt = new datatable();
    dt.columns.add("userid");
    dt.columns.add("usermoduleoption");
    dt.rows.add("张三", "m01010305,m010205,m0101030301");
    return dt.rows[0]["usermoduleoption"].tostring();
  }
  #endregion
  #region 创建数据
  protected static datatable createdt()
  {
    datatable dt = new datatable();
    dt.columns.add("module_id");
    dt.columns.add("module_name");
    dt.columns.add("module_fatherid");
    dt.columns.add("module_url");
    dt.columns.add("module_order");
    dt.columns.add("module_level");
    dt.rows.add("c1", "全国", "0", "", "1", "1");
    dt.rows.add("m01", "广东", "c1", "", "1", "2");
    //深圳
    dt.rows.add("m0101", "深圳", "m01", "3.aspx", "100", "3");
    dt.rows.add("m010101", "南山区", "m0101", "4.aspx", "1000", "4");
    dt.rows.add("m010102", "罗湖区", "m0101", "", "1001", "4");
    dt.rows.add("m010103", "福田区", "m0101", "", "1002", "4");
    dt.rows.add("m010104", "宝安区", "m0101", "", "1003", "4");
    dt.rows.add("m010105", "龙岗区", "m0101", "", "1004", "4");
    //深圳 - 福田区
    dt.rows.add("m01010301", "上梅林", "m010103", "", "1002001", "5");
    dt.rows.add("m01010302", "下梅林", "m010103", "", "1002002", "5");
    dt.rows.add("m01010303", "车公庙", "m010103", "", "1002003", "5");
    dt.rows.add("m01010304", "竹子林", "m010103", "", "1002004", "5");
    dt.rows.add("m01010305", "八卦岭", "m010103", "", "1002005", "5");
    dt.rows.add("m01010306", "华强北", "m010103", "", "1002006", "5");
    //深圳 - 福田区 - 车公庙
    dt.rows.add("m0101030301", "天安数码城", "m01010303", "", "100200301", "6");
    //广州
    dt.rows.add("m0102", "广州", "m01", "", "101", "3");
    dt.rows.add("m010201", "越秀区", "m0102", "", "1105", "4");
    dt.rows.add("m010202", "海珠区", "m0102", "", "1106", "4");
    dt.rows.add("m010203", "天河区", "m0102", "", "1107", "4");
    dt.rows.add("m010204", "白云区", "m0102", "", "1108", "4");
    dt.rows.add("m010205", "黄埔区", "m0102", "", "1109", "4");
    dt.rows.add("m010206", "荔湾区", "m0102", "", "1110", "4");
    dt.rows.add("m010207", "罗岗区", "m0102", "", "1111", "4");
    dt.rows.add("m010208", "南沙区", "m0102", "", "1112", "4");
    return dt;
  }
  #endregion
}

更多关于c#相关内容感兴趣的读者可查看本站专题:《c#常见控件用法教程》、《winform控件用法总结》、《c#数据结构与算法教程》、《c#面向对象程序设计入门教程》及《c#程序设计之线程使用技巧总结

希望本文所述对大家c#程序设计有所帮助。