Asp.Net實現無限分類產生表格的方法(後台自訂輸出table)_實用技巧

來源:互聯網
上載者:User

本文執行個體講述了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     Proxy 伺服器                      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程式設計有所協助。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.