Asp.Net实现无限分类生成表格的方法(后台自定义输出table)

本文实例讲述了Asp.Net实现无限分类生成表格的方法。分享给大家供大家参考,具体如下:

数据结构 monitor_group

monitor_grp_id      monitor_grp_name     parent_id       level               childCount       orderby
[int,自动递增]           [nvarchar,not null]      [int,not null]  [int,not null]   [int,not null]     [int ,null]
      1       数据库服务器                 0                    1                   2
      2       应用服务器                    0                    1                   2
      3       系统服务器                    0                    1                   0
      4       WEB服务器                   1                    2                   0
      5      邮件服务器                     1                    2                   0
      6     代理服务器                      2                    2                   0
      7     Ftp服务器                       2                    2                   0

\App_code\data.cs

using System;
using System.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Linq;
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 System.Xml.Linq;
/// <summary>
///common 的摘要说明
/// </summary>
/// 
namespace yihan
{
  namespace Data
  {
    public class myDataBind
    {
      public myDataBind()
      {
        //
        //TODO: 在此处添加构造函数逻辑
        //
      }
      public static string GetTree_monitor_grp_id(DataTable dt, int parent_id, ref string returnString)
      {
        //绑定目录树
        //dt:DataTable对象;parent_id:父ID;returnString:输出引用变量;
        DataRow[] dr = dt.Select("parent_id=" + parent_id);
        int currentLenght = 0;     //当前次数
        foreach (DataRow row in dr)
        {
          string nodeImg = "";    //节点图片
          string treeLineImg = "";  //树线
          currentLenght += 1;
          if (Convert.ToInt32(row["childCount"]) > 0)
          {nodeImg = "<img src='images/treeExpand.gif' align='absmiddle'>";}
          else
          {nodeImg = "<img src='images/treeNode.gif' align='absmiddle'>";}
          for (var i = 1; i <= Convert.ToInt32(row["level"]); i++)
          {
            //计算treeLineImg
            if (i == Convert.ToInt32(row["level"]))
            {
              if (currentLenght == dr.Length)   //判断当前次数是否与本次dr总数量相等
              { treeLineImg += "└ "; }
              else 
              { treeLineImg += "├ "; }
            }
            else
            {
              treeLineImg += "│ ";
            }
          }
          returnString += "<tr>\n";
          returnString += "<td align='left'>" + treeLineImg + nodeImg + " " + row["monitor_grp_name"] + "</td>\n";
          returnString += "<td align='center'>" + row["level"] + "</td>\n";
          returnString += "<td align='center'>" + row["childCount"] + "</td>\n";
          returnString += "<td align='center'>";
          returnString += "<a href='class_add.aspx?monitor_grp_id=" + row["monitor_grp_id"] + "'>添加子类</a> &nbsp;";
          returnString += "<a href='class_modi.aspx?monitor_grp_id=" + row["monitor_grp_id"] + "'>修改</a> &nbsp;";
          returnString += "<a href='class_del.aspx?monitor_grp_id=" + row["monitor_grp_id"] + "' onclick=\"javascript:{if(!confirm('确删要删除该类及其子类吗?'))return false;}\">删除</a> &nbsp;";
          returnString += "</td>\n";
          returnString += "</tr>\n";
          GetTree_monitor_grp_id(dt, Convert.ToInt32(row["monitor_grp_id"]), ref returnString);
        }
        return returnString;
      }//GetCatalogTree End
     }//myDataBind End
   }
}

class_list.aspx.cs

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
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 System.Xml.Linq;
using yihan.Data;
public partial class monitor_monitor_group_class_list : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    if (!IsPostBack)
    {
      DataTable dt = new DataTable();
      string resultString = "";
      string sql = "select * from monitor_group order by orderby desc,monitor_grp_id";
      DbConn conn = new DbConn();
      dt = conn.DataTable(sql);
      Literal1.Text = myDataBind.GetTree_monitor_grp_id(dt, 0, ref resultString); //调用
      dt.Dispose();
      conn.Close();
    }
  }
}

class_list.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="class_list.aspx.cs" Inherits="monitor_monitor_group_class_list" %>
<body> 
  <form id="form1" runat="server">
  <table class="conBox" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#66AADD">
   <tr align="center" bgcolor="#999999">
    <th width="36%" bgcolor="#BAD8EF">监视器组名称</th>
    <th width="9%" bgcolor="#BAD8EF">级别</th>
    <th width="15%" bgcolor="#BAD8EF">子节点总数</th>
    <th width="29%" bgcolor="#BAD8EF">操作</th>
   </tr>
   <tr>
    <td colspan="5" style="padding-left:6px;background:#DBDBDB;">监视器组</td>
   </tr>
    <asp:Literal ID="Literal1" runat="server"></asp:Literal>
  </table>
  </form>
</body>

手写Table

string s="<table>"
s+="<tr><td>";
s+=变量值;
s+="</td></tr></table>";
ResPonse.Write(s);

至于循环及其其他的方法自己构造

更多关于asp.net相关内容感兴趣的读者可查看本站专题:《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。

声明:本文内容来源于网络,版权归原作者所有,内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎发送邮件至:notice#cainiaojc.com(发邮件时,请将#更换为@)进行举报,并提供相关证据,一经查实,本站将立刻删除涉嫌侵权内容。