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

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

程序员文章站 2023-12-16 10:11:04
本文实例讲述了asp.net实现无限分类生成表格的方法。分享给大家供大家参考,具体如下: 数据结构 monitor_group monitor_grp_id ...

本文实例讲述了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>  ";
          returnstring += "<a href='class_modi.aspx?monitor_grp_id=" + row["monitor_grp_id"] + "'>修改</a>  ";
          returnstring += "<a href='class_del.aspx?monitor_grp_id=" + row["monitor_grp_id"] + "' onclick=\"javascript:{if(!confirm('确删要删除该类及其子类吗?'))return false;}\">删除</a>  ";
          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程序设计有所帮助。

上一篇:

下一篇: