[C#] 树形数据处理和输出 →→→→→进入此内容的聊天室

来自 , 2019-08-13, 写在 C#, 查看 92 次.
URL http://www.code666.cn/view/c4f796af
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using EVTBT.DAL;
  8. using EVTBT.Core;
  9. using System.Data;
  10. using System.Text;
  11.  
  12.  
  13. namespace Web
  14. {
  15.     public partial class WebForm1 : System.Web.UI.Page
  16.     {
  17.         static int country = -1;
  18.         static int product = -1;
  19.         public string productName;
  20.         static string result;
  21.         static StringBuilder divresult = new StringBuilder();
  22.         protected void Page_Load(object sender, EventArgs e)
  23.         {
  24.             try
  25.             {
  26.                 divresult.Clear();
  27.                 country = Convert.ToInt16(Request.QueryString["country"]);
  28.                 product = Convert.ToInt16(Request.QueryString["product"]);
  29.  
  30.  
  31.             }
  32.             catch
  33.             {
  34.             }
  35.             if (country != -1 || product != -1)
  36.             {
  37.                 //获取产品表
  38.                 DataTable table = GetProductTable();
  39.                 DataSet ds = new DataSet();
  40.                 //拷贝表格准备处理
  41.                 ds.Tables.Add(table.Copy());
  42.                 //表格加入关系
  43.                 ds.Relations.Add("TreeRelation", ds.Tables[0].Columns["Id"], ds.Tables[0].Columns["ParentId"]);
  44.                 //选择因子条目
  45.                 DataRow[] top = ds.Tables[0].Select("ID='" + product + "'");
  46.                 //判断因子是否为最底层因子
  47.                 judgement(top[0]);
  48.                 if (divresult.ToString() == null || divresult.ToString()=="")
  49.                 {
  50.                     productName = Common.NoPager2("adm_factorManagement", "", "sort_name", "", "ID='" + product + "'AND Valided=1").Tables[0].Rows[0]["sort_name"].ToString();
  51.                 }
  52.                 else
  53.                 {
  54.                     header.InnerHtml = "";
  55.                     compareCenter.InnerHtml = divresult.ToString();
  56.  
  57.                 }
  58.  
  59.             }
  60.  
  61.             else
  62.             {
  63.  
  64.             }
  65.  
  66.         }
  67.  
  68.         /// <summary>
  69.         /// 最底层因子输出HTML
  70.         /// </summary>
  71.         /// <param name="row"></param>
  72.         private void HTMLADD(DataRow row)
  73.         {
  74.             string PN = row["sort_name"].ToString();
  75.             DataTable DTfactor = Common.NoPager2("adm_Factor", "", "ID,ProductID,FacCName", "", "ProductID='" + row["ID"].ToString() + "'AND IsPublic=1").Tables[0];
  76.             if (DTfactor.Rows.Count > 0)
  77.             {
  78.                 divresult.Append("<div class=\"comp_section no_border no_m_b clearfix\">  <div class=\"comparison_row block head list_head clearfix\" runat=\"server\" id=\"compareTop\">  <div style=\"width: 979px;\"> <div class=\"col-xs-4 first\" style=\"width: 195.8px;\">" + PN + "</div>  <div class=\"col-xs-4\" style=\"width: 195.8px;\">   <div>  <a >标准</a><i class=\"arrows\"></i> </div> </div> <div class=\"col-xs-4\" style=\"width: 195.8px;\">     <div>     <a >法规</a><i class=\"arrows\"></i>  </div>   </div>  <div class=\"col-xs-4\" style=\"width: 195.8px;\">  <div> <a >认证</a><i class=\"arrows\"></i> </div>  </div> </div></div></div><div class=\"comp_section clearfix\"> </div>");
  79.  
  80.                 DataTable[,] factor = new DataTable[DTfactor.Rows.Count, 3];
  81.                 for (int i = 0; i < DTfactor.Rows.Count; i++)
  82.                 {
  83.                     factor[i, 0] = Common.NoPager2("adm_Standard", "", "ID,StaNumber,StaCName AS CName,CountryID", "", "CountryID='" + country + "'AND FactorID='" + DTfactor.Rows[i]["ID"].ToString() + "'AND IsPublic=1").Tables[0];
  84.                     factor[i, 1] = Common.NoPager2("adm_Regulation", "", "ID,RegNumber,RegCName AS CName,RegEName,CountryID", "", "CountryID='" + country + "'AND FactorID='" + DTfactor.Rows[i]["ID"].ToString() + "'AND IsPublic=1").Tables[0];
  85.                     factor[i, 2] = Common.NoPager2("adm_Certification", "", "ID,CerName AS CName,CerIntroduction,CerOrganization,CountryID", "", "CountryID='" + country + "'AND FactorID='" + DTfactor.Rows[i]["ID"].ToString() + "'AND IsPublic=1").Tables[0];
  86.                 }
  87.  
  88.  
  89.  
  90.                 for (int i = 0; i < DTfactor.Rows.Count; i++)
  91.                 {
  92.                     divresult.Append("<div class=\"comp_section clearfix\" >  <div class=\"clearfix nav_btn\"> <div><div class=\"col-xs-2 name\">" + DTfactor.Rows[i]["FacCName"].ToString() + "</div> <div class=\"col-xs-9 action\"> <div class=\"pull-right\"><a href=\"#\" style=\"width: 31px; height: 31px;\"><i>-</i></a></div></div> </div> </div>  <div class=\"comparison_row block centered clearfix\"> <div class=\"col-xs-12 mobile_title visible-xs\" style=\"width: 979px;\"> 111</div><div style=\"width: 979px;\"><div class=\"col-xs-4 first\" style=\"width: 195.8px;\">" + DTfactor.Rows[i]["FacCName"].ToString() + "</div>");
  93.                     for (int j = 0; j < 3; j++)
  94.                     {
  95.                         divresult.Append("<div class=\"col-xs-4\" style=\"width: 195.8px;\">");
  96.                         foreach (DataRow dr in factor[i, j].Rows)
  97.                         {
  98.                             switch (j)
  99.                             {
  100.                                 case 0: divresult.Append("<a class=\"fancybox1 fancybox.iframe\" href=\"resultdetail1.aspx?ID=" + dr["ID"].ToString() + "\"><i>" + dr["CName"].ToString() + "</a></i><br>"); break;
  101.                                 case 1: divresult.Append("<a class=\"fancybox2 fancybox.iframe\" href=\"resultdetail2.aspx?ID=" + dr["ID"].ToString() + "\"><i>" + dr["CName"].ToString() + "</a></i><br>"); break;
  102.                                 case 2: divresult.Append("<a class=\"fancybox3 fancybox.iframe\" href=\"resultdetail3.aspx?ID=" + dr["ID"].ToString() + "\"><i>" + dr["CName"].ToString() + "</a></i><br>"); break;
  103.                             }
  104.  
  105.                         }
  106.                         divresult.Append("</div>");
  107.  
  108.  
  109.                     }
  110.                     divresult.Append("</div></div></div>");
  111.                 }
  112.  
  113.             }
  114.         }
  115.  
  116.         /// <summary>
  117.         /// 判断因子是够为最底层因子
  118.         /// </summary>
  119.         /// <param name="DR">因子DataRow</param>
  120.         private void judgement(DataRow DR)
  121.         {
  122.  
  123.             DataRow[] rows = DR.GetChildRows("TreeRelation");
  124.             //不是最底层
  125.             if (rows.Length > 0)
  126.             {
  127.                 //遍历非最底层因子
  128.                 ResolveSubTree(DR);
  129.             }
  130.             //最底层因子
  131.             else
  132.             {
  133.                 //最底层因子数据输出
  134.                 HTMLADD(DR);
  135.             }
  136.  
  137.         }
  138.  
  139.         /// <summary>
  140.         /// 非最底层因子遍历
  141.         /// </summary>
  142.         /// <param name="dataRow"></param>
  143.         private void ResolveSubTree(DataRow dataRow)
  144.         {
  145.             DataRow[] rows = dataRow.GetChildRows("TreeRelation");
  146.  
  147.             if (rows.Length > 0)
  148.             {
  149.  
  150.                 foreach (DataRow row in rows)
  151.                 {
  152.                     DataRow[] ChlidRows = row.GetChildRows("TreeRelation");
  153.                     if (ChlidRows.Length > 0)
  154.                     {
  155.                         //遍历
  156.                         ResolveSubTree(row);
  157.                     }
  158.                     else
  159.                     {
  160.                         //输出
  161.                         HTMLADD(row);
  162.                     }
  163.  
  164.                 }
  165.             }
  166.  
  167.  
  168.         }
  169.  
  170.  
  171.         /// <summary>
  172.         /// 获取产品表
  173.         /// </summary>
  174.         /// <returns></returns>
  175.         public DataTable GetProductTable()
  176.         {
  177.  
  178.             DataTable ProductTable = Common.NoPager2("adm_factorManagement", "", "ID,Valided,sort_name,sort_parentID AS ParentId", "ID", "sortType=1 AND Valided=1").Tables[0];
  179.             ProductTable.Rows[0]["ParentId"] = DBNull.Value;
  180.             return ProductTable;
  181.         }
  182.  
  183.     }
  184. }

回复 "树形数据处理和输出"

这儿你可以回复上面这条便签

captcha